Hello artisan,
in this laravel nginx tutorial, I will show you how to deploy laravel application in nginx ubuntu and mysql server. So from this tutorial, you will learn a complete tutorial of deploy laravel nginx ubuntu server. To create this example of laravel deployment in vps server, i am going to use digitalocean laravel nginx. I will teach you how to deploy laravel nginx on ubuntu server.
In this install laravel ubuntu 20 04 nginx tutorial, I am going to use Digital Ocean Droplet(VPS) to deploy my Laravel Application. There are various alternatives to Digital Ocean like Vultr, Linode, Interserver, and Contabo. If you prefer the pay-as-you-go platforms, then AWS, Google Cloud, or Azure could be your options.
Let's start the tutorial from the beginning:
Step 1: Login with SSH
In this step, we have to log in to our server via the ssh key. So login like:
ssh root@100.100.100.101
Provide your password and you are successfully logged in to your server.
Read also: Dockerizing Laravel Application with Nginx and MySQL
Step 2. Update the package manager
Ubuntu uses the apt-get
package manager, I will need to ensure it is up to date before installing any packages.
sudo apt-get update
Step 3. Install Nginx
In this step, we need to install nginx via the below command. So install it like:
sudo apt-get install nginx
Now if you visit the url like:
http://100.100.100.101
Then you will see the welcome page of nginx.
Step 4: Install MySQL
The next step is to install the MySQL server which will contain our database. We can install using the following command:
sudo apt-get install mysql-server
Now in this step, we have to secure our database server in production. Luckily for us, there is a command we can run to help us secure MySQL.
sudo mysql_secure_installation
After pressing this command, we will be asked to install the VALIDATE PASSWORD plugin. I personally don’t install the plugin since I always want to have control over my passwords. In this part, I choose N
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No:
Next question asked is if I want to change the existing password for root. I personally choose N Next question is about removing anonymous users. I choose Y. Next, it will ask you to disallow root login remotely. I normally choose Y. You may get another prompt asking to remove the test database. Choose Y in this step. You may also be asked to reload the privileges table. Also, choose Y. With that, you are done with the installation of MySQL.
Setp 5. Install PHP
In this step, we have to install php in our server by the following command:
sudo add-apt-repository -y ppa:ondrej/php
sudo apt-get update
sudo apt-get install php-fpm php-mysql php-mbstring unzip php-json php-bcmath php-zip php-gd php-tokenizer php-xml
If you get the error apt add repository command not found, you can run this command.
sudo apt-get install software-properties-common
Step 6. Install Composer
In this we will install composer, a php dependency manager by the following command:
curl -sS https://getcomposer.org/installer | php
Move the downloaded binary to the system directory using this command:
sudo mv composer.phar /usr/local/bin/composer
Now verify the version by typing the command:
composer --version
Step 7. Configure PHP
Run the below command to open php.ini
file and make changes like the below:
sudo nano /etc/php/8.1/fpm/php.ini
Ensure you replace 8.1 with the correct version of PHP installed on your server.
Now the next step is to find the cgi.fix_pathinfo
configuration in the php.ini file. You can use the search functionality in nano by pressing ctrl+w and inserting cgi.fix_pathinfo
= on the search field and pressing enter. This will take you directly to the line. Uncomment the line by deleting the semicolon and changing it from 1 to 0. Once done, save the changes by pressing ctrl+s or ctrl+x.
Read also: How to Deploy Laravel Project with Apache and Linux Server
The last step is we have to restart php-fpm for the changes to take effect.
sudo systemctl restart php8.1-fpm
Step 8. Configure Nginx
Now we have to configure Nginx to enable it to serve our laravel application. Run the below command:
sudo nano /etc/nginx/sites-available/default
The first step is to instruct Nginx to recognize index.php files.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
Index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
}
We will add the index.php file to the Index part.
The next step is adding our IP address to the server_name line like the below:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name 100.100.100.101;
location / {
try_files $uri $uri/ =404;
}
}
By default, laravel gives us a .htaccess
file which can be found in the public folder. We can ignore it and let Nginx serve our application by default. This is how our new Nginx config file should look like:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name 100.100.100.101;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ /\.ht {
deny all;
}
}
The next step is specifying the default folder where our laravel application index file will be located. As a result, we need to tell Nginx where to find the file.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/laravel/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name 100.100.100.101;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Now run the below command to check the nginx configuration is by running the command:
sudo nginx -t
This should output the following:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Step 9. Configure MySQL
In this step, we have to configure mysql for our database connection. So run below command:
mysql -u root -p 'yourpassword'
Here user root
and your password is "password
". Now run the below command to create database:
CREATE DATABASE yourdatabasename;
Step 10. Install Laravel
Almost all are set to go. In this step, we can install our laravel application now. So run the below command: We will create a directory that will host our application. We had to specify it in the Nginx configuration file.
sudo mkdir /var/www/laravel && cd /var/www/laravel
Once we are in this directory, we can clone our application onto the server using git
git clone https://github.com/laravel/laravel.git
Now install composer in your project like:
composer install --no-dev
Now our application is ready to live. But we have to give proper permissions in our application's directory like:
sudo chown -R www-data:www-data /var/www/laravel
Give permissions to the storage folder:
sudo chmod -R 775 /var/www/laravel/storage
Now configure .env
file and connect your database with your configuration:
cp .env.example .env
We need to generate the encryption key for the application.
php artisan key:generate
Now open .env
by running the below command:
sudo nano .env
If your application is storing files on the server, you can create a symlink to the public folder in order to display the files
php artisan storage:link
Let’s not forget to migrate our database
php artisan migrate --force
We can now access and view our application at http://your-server-ip and Your application is now ready and fully deployed to production.
Recommended : Getting Started with Git and GitHub | Complete Beginner's Guide
Hope it can help you.
#laravel #nginx #mysql #ubuntu #deploy-laravel #deployment #linux-server #vps-server