How to Deploy Laravel Application with Nginx on Ubuntu?

Laravel is a PHP Framework and Nginx is a Web-Browser which can be installed on Ubuntu. Nginx is more popular nowadays because it is lightweight. Here, we will deploy a Laravel application on Nginx.

First of all, your server should have installed Nginx and PHP-FPM. Please click on this link to install and configure PHP-FPM and Nginx. We believe you have already have installed this so we are proceeding further.

We need to access the server with the root or sudo user because we will edit the Nginx virtual host files to deploy our Laravel Application.

  1. Update Ubuntu repository with its package.

    # sudo apt update
    # sudo apt upgrade
  2. Let us reboot the server once.

  3. Once server is up, Install Nginx using below command.

    # sudo apt install nginx -y
  4. Install PHP 7.2 and PHP-FPM.

    # sudo apt install php7.2 php7.2-curl php7.2-common php7.2-cli php7.2-mysql php7.2-mbstring php7.2-fpm php7.2-xml php7.2-zip -y
  5. Start and Enable php 7.2

    # systemctl start php7.2-fpm
    # systemctl enable php7.2-fpm
  6. You can verify PHP-FPM running under socke file with the below command.

    # netstat -pl | grep php7.2-fpm
  7. Install MariaDB using the below Command.

    # sudo apt install mariadb-server mariadb-client -y
  8. Start and Enable MariaDB.

    # service mysql start
    # service enable mysql
  9. Here, we will configure MariaDB root password with the 'mysql_secure_installation'

    #mysql_secure_installation
    
    Set root password? [Y/n] Y
    Remove anonymous users? [Y/n] Y
    Disallow root login remotely? [Y/n] Y
    Remove test database and access to it? [Y/n] Y
    Reload privilege tables now? [Y/n] Y
    
  10. Install PHP Composer with below Command.

    # sudo apt install composer -y
  11. Once you run the composer command, it will give the same screen as below. This means the composer is PHP Composer is installed.
    #composer
  12. Let us configure the nginx virtual host for Laravel. Here, we will use '/var/www/laravel_tet' directory for our project. First, we will create the same directory.

    # mkdir -p /var/www/laravel_test
  13. We should test nginx to make sure there is no error.

    ln -s /etc/nginx/sites-available/laravel_test /etc/nginx/sites-enabled/nginx -t
    
  14. Restart the nginx service.

    # service nginx restart
    
  15. Go to the folder laravel_test which we have defined earlier.

    # cd /var/www/laravel_test
    
  16. Run the following composer command to install Laravel.

    # composer create-project laravel_test/laravel_test
    
  17. Now, change the ownership of the Laravel project directory to 'www-data' user. Change the permission of the storage directory to 755.

    # chown -R www-data:root /var/www/laravel_test
    # chmod 755 /var/www/laravel_test
  18. Inorder to test the Laravel, we will need to access the Domain name which we have added in the virtual host configuration file. Once you access the Domain name, it should give the laravel hostname.
  • 0 Users Found This Useful

Was this answer helpful?

Related Articles

How to Install and Configure Fail2ban on Ubuntu?

You require to protect your server from a Brute force attack from the internet. You can do it...

How to take Backup of Exim Configuration from WHM?

EXIM comes with cPanel which is responsible for the email tranfer on the Linux server. Users can...

How to Move Accounts From One cPanel Server To Another cPanel server?

cPanel facilitates you with the built-in Transfer Tools which allow you to migrate your existing...

Fix :: Linux SSH error "connection refused"

Problem Statement Can't connect to a Linux server(Debian Flavour) via SSH. Error Message...

How to Setup NTP Server in CentOS?

NTP enables the automatically sync your system time with a remote server. With the help of NTP...