How to Deploy Symfony on Alibaba Cloud

Image for post
Image for post

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.

Then install NGINX by running the following command:

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.

Test if the PHP FPM service is active

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

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.

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

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

You should see something like the following result:

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

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

The terminal should change to the MySQL prompt.

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

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

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

You can now exit the MYSQL terminal

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

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

Next, install the project dependencies using composer:

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

Then start creating entities for the database.

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

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:

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:

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

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:

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

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

Original Source

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store