×
Community Blog How to Deploy Symfony on Alibaba Cloud

How to Deploy Symfony on Alibaba Cloud

In this tutorial, you will learn how to deploy a Symfony application on Alibaba Cloud using an NGINX web server, Composer, and MySQL.

By Esther Vaati, Alibaba Cloud Community Blog author.

Symfony is an open-source PHP framework that allows developers like you 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 back-ends for your Symfony applications.
  • Version 4 comes with a simple scalable framework, which makes it easier to learn, configure, install and deploy.

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 website. The main advantage of using NGINX as a web server is that it allows requests to be handled in a single thread, hence 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 servers IP address on the browser, and you should see NGINX's default page similar to the one below:

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 did 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 filesystem. 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.

1 1 1
Share on

Alibaba Clouder

2,599 posts | 762 followers

You may also like

Comments

5316206321858932 August 2, 2019 at 7:08 am

How is Alibaba cloud compared to other providers out there? I have used Symfony on AWS through Cloudways PHP 7 hosting (https://www.cloudways.com/en/php-cloud-hosting.php ). I am content with its performance and stability.