{{announcement.body}}
{{announcement.title}}

How to Deploy Symfony on Alibaba Cloud

DZone 's Guide to

How to Deploy Symfony on Alibaba Cloud

Everything you need to get started with Symfony on Alibaba Cloud.

· Web Dev Zone ·
Free Resource

Symfony is an open-source PHP framework that allows developers to bootstrap applications quickly. The framework consists of reusable components, which you can add on to build virtually any type of application.

Other advantages of it include:

  • It has a lightweight build.
  • It does not restrict you to a specific database system.
  • It is equipped with EasyAdminBundle, which allows you to create administration backends for Symfony applications.
  • Version 4 comes with a simple, scalable framework, which makes it easier to learn, configure, install, and deploy applications.

Before You Begin

Before you continue with this tutorial, you'll need the following:

  • An Alibaba Cloud ECS instance installed with Linux. If you haven't yet set up your Linux instance, this article shows you various ways to set it up.
  • A non-root user with sudo privileges.

Procedure

In this tutorial, you will install the following tools:

Installing the NGINX Web Server

NGINX is a powerful modern web server that can be used to service various types of websites. The main advantage of using NGINX as a web server is that it allows requests to be handled in a single thread, thus offering the overall low memory usage of your website. Apart from these serving requests, NGINX can also be used as a reverse proxy, for load balancing and HTTP caching.

Before you install NGINX, you should start by updating your system packages using the apt package.

sudo apt-get update


Then install NGINX by running the following command:

sudo apt install nginx


Now, navigate to your server's IP address on the browser, and you should see NGINX's default page similar to the one below:

ADD PICTURE

Installing PHP FPM

Unlike Apache, NGINX is not able to process PHP files, so you need to install PHP FPM service to handle the processing.

sudo apt install php-fpm


Test if the PHP FPM service is active.

systemctl status php7.2-fpm


You should see a result similar to the one that follows:

php7.2-fpm.service: The PHP 7.2 FastCGI Process Manager

Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)

Active: active (running) since Fri 2019-03-22 19:24:51 CST; 12s ago

Docs: man:php-fpm7.2(8)

Main PID: 1087 (php-fpm7.2)

Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 1115)

CGroup: /system.slice/php7.2-fpm.service

├─1087 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)

├─1090 php-fpm: pool www

└─1091 php-fpm: pool www

Mar 22 19:24:51 iZt4nba70pwhw449xie679Z systemd[1]: Starting The PHP 7.2 FastCGI Process Manager...

Mar 22 19:24:51 iZt4nba70pwhw449xie679Z systemd[1]: Started The PHP 7.2 FastCGI Process Manager.


Installing Composer

Composer is a tool for dependency management in PHP. It allows you to declare the libraries on which your project depends, while also managing the installation and maintenance of these project dependencies.

You will first install curl to enable you to download Composer. You will need php-cli for installing and running Composer. Composer also requires the Mbstring PHP Extension, so you will need to install it as well.

sudo apt install curl php-cli php-mbstring git unzip


Now that all the preliminary Composer requirements are satisfied, you can install composer.

sudo apt install composer


To confirm whether Composer is installed, run the following command:

composer


You should see something like the following result:

/ ____/___  ____ ___  ____  ____  ________  _____

/ /  / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/

/ /___/ /_/ / / / / / / /_/ / /_/ (__ )  __/ /

\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/

/_/

Composer version 1.8.4 2019-02-11 10:52:10

Installing MySQL


In order to manage data, you will need to install MySQL by running the following command:

sudo apt install mysql-server


To check if MySQL is properly installed, enter the following command:

mysql -u root -p


The terminal should change to the MySQL prompt.

mysql>


You can now be able to create a user and a database for the Symfony application. To do this, start by creating the database:

mysql> CREATE DATABASE myapplication;

Query OK, 1 row affected (0.00 sec)


Create a user for the database and grant privileges to that user.

mysql> CREATE USER 'vaati'@'localhost' IDENTIFIED BY 'password';

Query OK, 0 rows affected (0.01 sec)

mysql> GRANT ALL PRIVILEGES ON myapplication.* TO 'vaati'@'localhost';

Query OK, 0 rows affected (0.00 sec)


You can confirm if the database has been created by running the following command:

mysql> SHOW DATABASES;

+--------------------+

| Database  |

+--------------------+

| information_schema |

| myapplication  |

| mysql  |

| performance_schema |

| sys  |

+--------------------+

5 rows in set (0.00 sec)


You can now exit the MYSQL terminal

mysql> quit


Setting Up the Symfony Application

This part will go over how to create the Symfony application and perform the necessary configurations needed on the application. In this section, we are going to create a simple task management system with two entities, which are name and date.

Navigate to /var/www and create a directory for our application

cd /var/www/

mkdir symfony-app

cd symfony-app

composer create-project symfony/skeleton symfapp


The create command above will create the basic directory structure with all the needed dependencies. Next, you want to install a PHP extension to handle XML in the Symfony application and add the database settings to the env file.

DATABASE_URL=mysql://vaati:password@localhost:3306/myapplication


Next, install the project dependencies using composer:

composer install

Loading composer repositories with package information

Installing dependencies (including require-dev) from lock file

Nothing to install or update

Generating autoload files

Executing script cache:clear [OK]

Executing script assets:install public [OK]

ORM entity


Next, create a Task object to represent the tasks. We are going to use a package called Doctrine, which will enable Symfony to manage databases. Run the following command to add the Doctrine files with composer

composer require symfony/orm-pack

composer require symfony/maker-bundle --dev


Then, start creating entities for the database.

php bin/console make:entity

Class name of the entity to create or update (e.g. TinyKangaroo):

> Task

created: src/Entity/Task.php

created: src/Repository/TaskRepository.php

Entity generated! Now let's add some fields!


You can always add more fields later manually or by re-running this command. You can specifically do the following:

Note: Through this process, you can press to stop adding fields or press enter ? to see all types and then enter [string].

To add a new property name, use > task_name. Next, to add a new due date, use > due_date, and to add a datetime, use > datetime. Last, for the question can this field be null in the database (nullable) (yes/no), you want to enter [no]. After you do this, you should see: updated: src/Entity/Task.php. This creates a file src/Entity/Task.php, and you can now be able to save and query Task objects.

Configure the Database

Update the database

./bin/console doctrine:schema:update --force

Updating database schema...

1 query was executed

[OK] Database schema updated successfully!


To make your application simple, you are going to install the EasyAdminBundle bundle, which is a back-office manager for Symfony applications. It will allow us to perform CRUD operations on doctrine entities.

So, go ahead and install Easyadmin:

composer require admin


Now, you will need to configure your bundle in (config/package/easy_admin.yaml) with the parameters easy_admin: and entities:.

Now, you want to list the entity class name you want to manage:

  • App\Entity\Task
    • App\Entity\Category
    • App\Entity\User

Configuring Nginx to Server Application Files

The most important part of the application is to be able to service files on a web browser. To accomplish this, we will use the NGINX web server to service our application files. We can start by creating a unique server block for our application:

nano  /etc/nginx/sites-available/tasks


Then, you can copy the contents of the default server block and update all the details of our application as follows:

server {

listen 80;

listen [::]:80;

server_name tasks server_ip;

root /var/www/symfapp/symfonytasks/public;

index index.php;

client_max_body_size 100m;

location / {

try_files $uri $uri/ /index.php$is_args$args;

}

location ~ \.php {

try_files $uri /index.php =404;

fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;

fastcgi_split_path_info ^(.+\.php)(/.+)$;

fastcgi_index index.php;

include fastcgi_params;

}

location ~ /\.(?:ht|git|svn) {

deny all;

}

}


Here, we set the listen directives for NGINX at port 80 and the server name to the server's IP address. Since only the public directory should be exposed to the internet, our root directory is /var/www/symfapp/symfonytasks/public.

Now, we can map the sockets for communication between NGINX and PHP-FPM, which is the PHP 7 path on the file system. To do this, enable the server block by creating a symbolic link to sites-enabled by running the following command:

sudo ln -s /etc/nginx/sites-available/tasks /etc/nginx/sites-enabled/


Confirm if the new file is working properly and reload NGINX by using the following:

sudo nginx -t

sudo systemctl reload nginx


Your application is now accessible at http://ip_address/admin/, and you can start adding Tasks to the application.

Topics:
nginix ,mysql ,symfony ,composer ,web dev ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}