×
Community Blog Deploy a Laravel 5.6 application on Alibaba ECS instance

Deploy a Laravel 5.6 application on Alibaba ECS instance

In this tutorial, we will deploy a production ready Laravel 5.6 application on Alibaba ECS instance with LEMP stack (Laravel, Nginx, MySQL and PHP).

By Himanshu Gupta, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud's incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

Laravel is one of the popular PHP frameworks which is being used to develop simple and complex web applications. Laravel has turned out to be a good frameworks for API development and generating dashboards.

In this tutorial, we will deploy a production ready Laravel 5.6 application on Alibaba Cloud Elastic Compute Service (ECS) instance with LEMP stack i.e Laravel, Nginx, MySQL and PHP.

Prerequisites

  1. An active Alibaba Cloud Account. You can sign up for an Alibaba Cloud account and enjoy the New User Free Credit. If you are not sure how ot set up an account, you can follow this step-by-step tutorial.
  2. A User account with sudo access for SSH login (Root user will work too)
  3. This tutorial requires LEMP stack to run a Laravel 5.6 application. If you are not sure how you can set up a LEMP stack, follow the steps in this article: Setting up LEMP stack on Alibaba ECS instance.

    Note: However, to run a Laravel 5.6 Application, you are required to use PHP 7.2 (the above article helps you install PHP 7.0). You can install PHP 7.2 by adding PPA repository on Ubuntu and then update the packages index on Ubuntu using the update command. Run these commands before following the tutorial.

    $ sudo apt-get install -y software-properties-common 
    $ sudo add-apt-repository ppa:ondrej/php
    $ sudo apt-get update

    Install PHP along with other required PHP modules

    $ sudo apt install php7.2-fpm php7.2-mysql php7.2-common php7.2-mbstring php7.2-xmlrpc php7.2-xml php7.2-intl php7.2-cli php7.2-zip php7.2-curl
    $ php -v

    To serve only PHP files through fpm, open the php-fpm config file with root privileges

    $ sudo nano /etc/php/7.2/fpm/php.ini

    Look for the parameter cgi.fix_pathinfo and uncomment it by removing the prefixed semicolon and change its value to 0

    $ cgi.fix_pathinfo=0

    Restart php-fpm to reflect the configuration settings.

    $ sudo systemctl restart php7.2-fpm

    Now, you can continue with the above article to configure Nginx Server for PHP requests. Make sure you change the line in location block with php 7.2 version as php7.2-fpm.sock


  4. A Laravel application ready to be deployed on a server. You can use any Laravel app, but we will be using this Quickstart Laravel application to deploy on the server.

Step 1: Installing Dependencies

We will install some dependencies to run Laravel 5.6 application on our stack. Execute the below commands on your Alibaba Cloud ECS instance:

SSH login to the server instance with a user account having root priveleges (Use Putty/Mobaxterm for Windows to login into your server instance)

$ ssh root@server_ip_address

Update the package manager cache for your instance

$ sudo apt-get update

Install Composer to manage PHP packages in Laravel along with package for multi-byte string, XML etc which are required for Laravel application. Composer is used for package dependencies management in PHP applications.

You can check the server requirements for Laravel 5.6 in the official documentation.

$ sudo apt-get install composer unzip zip php7.2-mbstring php7.2-xml php7.2-common php7.2-curl

Step 2: Configure MySQL server

During the LEMP stack installation, you must have a root administrative account created by MySQL which have complete privileges to do any kind of operations with all databases. Therefore, we will be creating a new MySQL user specifically for our Laravel application with required privileges.

Log into the MySQL administrative root account. You would require MySQL root password for access.

$ mysql -u root -p

Create a new Database for the application

$ mysql> CREATE DATABASE laravel_db DEFAULT CHARACTER SET utf COLLATE utf8_unicode_ci;

Create a new user and grant privileges for our database. We are creating user with name as 'laravel_user' and password 'password123'

$ mysql> GRANT ALL ON laravel_db.* TO 'laravel_user'@'localhost' IDENTIFIED BY 'password123';

Flush privileges and exit

$ mysql>  FLUSH PRIVILEGES;
$ mysql>  Exit;

However, take a note of the credentials for our database as:

• Database Name: laravel_db
• Database User: laravel_user
• Database Host: Host for DB server (localhost in case of same server)
• Database Password: password123

Step 3: Upload Laravel Application Code on Server

Now, the database for our Laravel application is set and we will upload our application code in the server directory. We can use git to clone a repository from Github or Bitbucket for private repositories.

We will be using a demo Laravel application provided by Laravel to install on our Alibaba ECS instance. This demo application is a simple note taking application which is used for the learning purpose.

Create a directory in the html folder with the name laravelapp

$ sudo mkdir -p /var/www/html/laravelapp

We will change the ownership of the new directory laravelapp to our user such that, our user john will be able to manage and manipulate files and folders inside project folder.

$ sudo chown john:john /var/www/html/laravelapp

Change to the laravelapp directory and clone the sample Laravel application from the git repository.

$ cd /var/www/html/laravelapp
$ git clone https://github.com/laravel/quickstart-basic .

You would see the output as shown below in the console where your project files and folders have been copied in the laravelapp folder.

------- Git Output-----
Cloning into '.'...
remote: Counting objects: 263, done.
remote: Total 263 (delta 0), reused 0 (delta 0), pack-reused 263
Receiving objects: 100% (263/263), 92.75 KiB | 0 bytes/s, done.
Resolving deltas: 100% (72/72), done.
Checking connectivity... done.

Next, run the composer install command inside the project folder to install various dependencies by our project. Composer is being used by various PHP frameworks to manage the dependencies.

$ composer install

This will install the required packages and might take a minute to complete. You will see an output showing installation of all the packages mentioned in the composer.json file.

Now, the Laravel application is deployed and we need to setup the application environment such as Production, Staging or Development.

Step 4: Configure Application Environment

We will configure the application environment for Laravel such that it will allow the application to connect to the database and manage various other environment variables.
Open the environment file in the Laravel application

$ sudo nano /var/www/html/laravelapp/.env

Make the changes as depicted below (Changes are present in Color and italic)

APP_ENV=production
APP_DEBUG=false
APP_KEY=b809vCwvtawRbsG0BmP1tWgnlXQypSKf
APP_URL=http://yourdomain.com

DB_HOST=127.0.0.1
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=password123
...
...

Press CTRL+X, Press Y and then Enter to save file and exit.
Run the database migration to create tables and indexes for our application. Since, the environment is production, it will prompt you for your confirmation.

$ php artisan migrate

Type yes when it will ask to confirm about running the migration

Artisan output
**************************************
*     Application In Production!     *
**************************************

Do you really wish to run this command? [y/N](yes/no) [no]:> yes

Migration table created successfully.
Migrated: 2014_10_12_000000_create_users_table
Migrated: 2014_10_12_100000_create_password_resets_table

Setup permissions for files and folders of Laravel application which involves storage, cache, sessions etc.

We will be changing ownership to www-data which is a default user group to manage web server.

$ sudo chgrp -R www-data storage bootstrap/cache
$ sudo chmod -R ug+rwx storage bootstrap/cache

At last, we just need to tweak nginx configuration to server the files and folders present in the application.

Step 5: Configure Nginx server for application

Case 1: If you have a domain name present with you, then you can create a new server block in the /etc/nginx/sites-available folder by creating a new file and overriding some defaults.

Case 2: On the other hand, if you don' have a domain name, we will do the changes in Nginx's default configuration file for Laravel application.

Open the default nginx config file

$ sudo nano /etc/nginx/sites-available/default

Do the changes in default file as shown below in italic and red color

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html/laravelapp/public;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Test the nginx configuration using:

$ sudo nginx -t

Restart Nginx configuration to reflect the changes.

$ sudo service nginx restart

Congratulations, you have successfully deployed a Laravel 5.6 application on an Alibaba Cloud ECS instance!

Visit the IP address in your browser to see a web page having option of adding tasks. The Quickstart application is a Task application where you can add and delete tasks.

3

2 2 1
Share on

Alibaba Clouder

2,605 posts | 747 followers

You may also like

Comments

5316206321858932 July 24, 2018 at 11:10 am

I have never used Alibaba cloud. How does it compare again other giants like AWS? I have Laravel server (https://www.cloudways.com/en/laravel-hosting.php ) running on AWS through a managed PaaS. I am content with its performance and uptime.

5652561711849457 July 3, 2019 at 2:12 am

Do you know how to configure alibaba ecs and bitbucket pipeline to make them work together? so when I update my bitbucket repo, it'd deploy to my alibaba ecs automatically.