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.
- Update Ubuntu repository with its package.
# sudo apt update
# sudo apt upgrade
- Let us reboot the server once.
- Once server is up, Install Nginx using below command.
# sudo apt install nginx -y
- 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
- Start and Enable php 7.2
# systemctl start php7.2-fpm
# systemctl enable php7.2-fpm
- You can verify PHP-FPM running under socke file with the below command.
# netstat -pl | grep php7.2-fpm
- Install MariaDB using the below Command.
# sudo apt install mariadb-server mariadb-client -y
- Start and Enable MariaDB.
# service mysql start
# service enable mysql
- Here, we will configure MariaDB root password with the '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
- Install PHP Composer with below Command.
# sudo apt install composer -y
- Once you run the composer command, it will give the same screen as below. This means the composer is PHP Composer is installed.
- 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
- 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
- Restart the nginx service.
# service nginx restart
- Go to the folder laravel_test which we have defined earlier.
# cd /var/www/laravel_test
- Run the following composer command to install Laravel.
# composer create-project laravel_test/laravel_test
- 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
- 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.