Today I will tell you how to deploy a Laravel application on ubuntu using LEMP sever. In order to do that we need several things to setup. Which are followings.
- Install Nginx server
- Install PHP and required libraries
- Install MySql and phpmyamdin(optional)
- Install Git
- Install composer
- Install/Deploy Laravel
Let’s start setting up everything. I am using clean/fresh ubuntu 22.04 installation for this tutorial. However You are free to continue with your existing installation.
1. Install Nginx server
let’s start the setup. Run following commands in your terminal to install nginx server.
sudo apt update
sudo apt install nginx
Once command is finished. Lets check nginx status by running this command.
Note
We will run sudo apt update command frequently to keep update the system.
sudo systemctl status nginx
It should show active (running)
status in green color. Output should be look like following picture.
Now if you open you browser and type localhost
and hit enter you browser should be look like this.
Congratulation! It means the nginx server is running on you system.
2. Install PHP and required libraries
2. Now Let’s install the PHP. Run following commands to install PHP8.1
sudo apt update
sudo apt install --no-install-recommends php8.1
Upon running it will show a prompt like Do you want to continue? [Y/n]
. Hit Y
here.
Once above command is done; we need to install required extensions for the PHP. So run the following command to install required libraries.
sudo apt-get install php8.1-cli php8.1-common php8.1-mysql php8.1-zip php8.1-gd php8.1-mbstring php8.1-curl php8.1-xml php8.1-bcmath php8.1-fpm
3. Install MySql and phpmyamdin(optional)
Now we have successfully installed all necessary libraries for the PHP. Let’s move forward and install mysql server. To install mysql-server
run following command in your terminal.
sudo apt update
sudo apt install mysql-server
Again Upon running above it will show a prompt like Do you want to continue? [Y/n]
. Hit Y
here. Once command is finished. it means mysql server has been installed and here we need to set password for the mysql server. Let’s login to the mysql server by running this command.
sudo mysql
Once you are logged in run following sql in your terminal. You should replace your secret password
with your desired password.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your secret password';
Output will look like this.
Now type exit and hit enter
.
So mysql server has been installed. However it’s optional let’s move forward and install phpmyadmin by running below command. It will show a prompt like Do you want to continue? [Y/n]
. Hit y
here.
sudo apt install phpmyadmin
While this command is in progress you will see a pop-up like below.
Here You should not select any server. Since we have installed nginx server and we will configure it for phpmyadmin. Just use tab
to select ok and press enter. After that another pop-up will show up you should select yes
option here and enter the password you used to setup after mysql-server
installation. Now phpmyadmin installation is completed.
Now let’s configure it on nginx. Run below command.
sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin
Now if you try to access phpmyadmin by opening localhost/phpmyadmin
. It will show you 404 page. Now create a symbolic link to phpmyadmin
in /var/www/html/phpmyadmin
. Here again if you refresh localhost/phpmyadmin
page it will show you 403 page. So it’s means we need to adjust permissions. Let’s do it.
sudo chown -R www-data:www-data /var/www/html/phpmyadmin
sudo chmod -R 755 /var/www/html/phpmyadmin
sudo chown -R www-data:www-data /var/www/html
Noe we will configure our virtual host file. Run this command.
sudo nano /etc/nginx/sites-enabled/default
It will open default file like above image. Now you need to copy and paste following content right before the line which shows # deny access to .htaccess files, if Apache’s document root
location /phpmyadmin { root /var/www/html; index index.php index.html index.htm; location ~ ^/phpmyadmin/(.+\.php)$ { try_files $uri =404; root /var/www/html; fastcgi_pass unix:/run/php/php8.1-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /etc/nginx/fastcgi_params; } location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ { root /var/www/html; } }
Now run a command to restart nginx server.
sudo systemctl restart nginx
Go to your browser and type localhost/phpmyadmin
. It will show you the admin panel like below if you have done all above steps correctly.
4. Install Git
Now we are going to install Git. To do that run following commands.
sudo apt update
sudo apt install git
Allow the process to complete. Hit y
and enter once command is done. Verify the installation and version by running:
git --version
The output states the program version if it has been installed correctly.
5. Install composer
So Now we need to install composer
by running following commands. Hit y
wherever it ask for permission.
sudo apt install curl
sudo curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
HASH=`curl -sS https://composer.github.io/installer.sig`
echo $HASH
php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
Above command will install composer
on your machine. Let’s verify it by running:
composer
So you should see output like this.
6. Install/Deploy Laravel
To create new Laravel application; Inside the home directory run this command. I am creating this application using name first-app
. however you are open to use any name.
composer create-project --prefer-dist laravel/laravel first-app
The above command will create a new first-app
directory containing a barebones Laravel application based on default settings. You will see output similar to this:
Now It’s time to configure nginx
for this application. We have installed Laravel on a local folder of your remote user’s home directory. We’ll move the application folder to /var/www
, which is the usual location for web applications running on nginx.
Use the mv
command to move the application folder with all its contents to /var/www/first-app
sudo mv ~/first-app /var/www/first-app
We need to give the web server user write access to the storage
and cache
folders, where Laravel stores application-generated files. So run following commands.
sudo chown -R www-data:www-data /var/www/first-app/storage
sudo chown -R www-data:www-data /var/www/first-app/bootstrap/cache
Now we need to configure Nginx to serve the content. To do this, we’ll create a new virtual host configuration file at /etc/nginx/sites-available
sudo nano /etc/nginx/sites-available/first-app
The following configuration file contains the recommended settings for Laravel applications on nginx
.
server {
listen 80;
server_name 127.0.0.1;
root /var/www/first-app/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Copy this content to your /etc/nginx/sites-available/first-app
To save this file hit ctrl+o
, press enter
and ctrl+x
.
Now to activate the new virtual host configuration file, create a symbolic link to first-app
in sites-enabled
sudo ln -s /etc/nginx/sites-available/first-app /etc/nginx/sites-enabled/
To verify that the configuration doesn’t contain any syntax errors, you need to run following command.
sudo nginx -t
You should see output like this
To apply the changes, reload nginx
with
sudo systemctl reload nginx
Now go to your browser and access the application using the server’s 127.0.0.1
, as defined by the server_name
directive in your configuration file.
You will see a page like this.
That’s it. Now you have successfully deployed new Laravel application on you system. Isn’t so easy?