Saturday, April 20, 2024
HomeTutorialsHow to Install Laravel PHP Framework with Nginx on Ubuntu Linux

How to Install Laravel PHP Framework with Nginx on Ubuntu Linux

Laravel is the latest and most functional PHP framework. When the new Laravel framework was released, it became trendy among web developers. The most attractive feature of the Laravel framework is its folder structure and file implementations. The method injection process is much easier and smooth in the lastest Laravel framework than any previous versions. Another important feature of the Laravel PHP framework is it’s route caching system. When you start developing a hugely complex web designing with enormous visitors, the route caching will definitely help you.

Laravel PHP Framework with Nginx


If you are a web developer, you must know that a PHP framework’s importance is very much high. Laravel works on the Model–view–controller method. Laravel has it’s own built-in security settings that can protect your database. Here is an advantage of Laravel, a web developer does have to do any extra work on the security system of the web framework; Laravel takes care of it. Data call, data input, and data manipulation systems are very secure in the Laravel PHP framework.

To install the Laravel PHP framework in your Ubuntu machine, you will need to have the PHP installed inside your machine. You will also need to have a database and a web server installed. Here, we will be using the Nginx proxy server as the host server of PHP. In this post, we will see how to install the Laravel PHP framework with Nginx on Ubuntu Linux.

Step 1: Install Nginx Server on Ubuntu


Nginx is a popular open-source web server tool that you can install inside your Linux machine to run your website from the localhost. The Nginx server is capable of using the load balancing method to maintain the web server’s visitor ratio. Moreover, we are using the Nginx server because the Laravel PHP framework is very much compatible with it.

To install the Nginx server inside your Ubuntu machine, you can use the following apt command-line in your terminal shell.

sudo apt update && sudo apt upgrade
sudo apt install nginx -y

Once the Nginx server is installed, you can verify the Nginx server by checking it’s version and configuration settings.

nginx -v 
sudo nginx -t

Now run the following terminal command lines to enable and start the Nginx server on your Ubuntu machine.

systemctl start nginx
systemctl enable nginx

Laravel PHP Framework nginx version

Be careful if you have any previous PHP framework or Apache server installed inside your Ubuntu machine; make sure you have cleaned the previous packages correctly. You can check the open the port of Nginx. Normally the open port of Nginx is 80. To check Nginx’s open port, I am using the curl command tool on Ubuntu. If you don’t have the cURL installed inside your machine, install the cURL tool first.

sudo apt install curl 
curl -I localhost

localhost port

Step 2: Install Mysql Database for PHP Framework


Here we have to install a database engine to store and retrieve data of the website. We can use any database we want. Now I am going to choose and install the MySQL server for the Laravel PHP framework. Use the following terminal commands to install the MySQL server inside your Ubuntu machine. You can check the version of the MySQL server to verify the MySQL server.

mysql for Laravel PHP framework

Then start and enable the MySQL server by using the terminal commands given below.

sudo apt-get install mysql-server -y
systemctl start mysql
systemctl enable mysql

Step 3: Install PHP Framework on Ubuntu Linux


Here, we are going to install the PHP framework on our Ubuntu machine. We will install the PHP fast process manager, document object model (DOM), PHP MySQL extension, and multi-byte string functions alongside the PHP framework. All the extension packages are required to run the Laravel PHP framework smoothly on your machine. You can copy and paste the following command line in your terminal shell to install the PHP framework and extension packages.

apt-get install php php-fpm php-mysql php-mbstring php-dom -y

Now we can install the Mcrypt PHP extension to make the PHP framework more encrypted and secure. Use the following command lines to install the Mycrypt PHP extension on your Ubuntu device.

sudo apt-get install php-dev libmcrypt-dev gcc make autoconf libc-dev pkg-config
sudo pecl install mcrypt-1.0.1

In this step, we have to edit the configuration script of the PHP-FPM (Fast Process Manager) to add the new address and remove the default address. Using the Gedit text editor to edit the configuration script, you can use any script editor you want. Use the following command line to edit the configuration script.

sudo gedit /etc/php/7.4/fpm/pool.d/www.conf

Now find the following line inside the www.confi script. Make the line as a comment line by adding a ; before the line.

listen = /run/php/php7.4-fpm.sock

Now add a new line into the script to add a new server address. Here, I am using the localhost address and port 8000.

listen = 127.0.0.1:8000;

Nginx 8000 port Laravel PHP framework

Now save the script file and restart and enable the PHP framework on your Ubuntu machine. Then restart the Nginx server.

systemctl restart php7.4-fpm
systemctl enable php7.4-fpm
systemctl restart nginx

Step 4: Install Laravel PHP Framework on Ubuntu


Composer is the dependency manager of the PHP framework. If you get the Composer installer inside your PHP framework, you don’t need to download and install related packages individually over and over. It can update, reload, and configure PHP frameworks automatically. To install the Composer for a PHP framework, you can follow the cURL command given below.

curl -sS https://getcomposer.org/installer | php

Now move the Composer files into the bin directory using the mv command in your Ubuntu machine. This will help your PHP framework to get interacted with the Composer settings. Then move the Nginx directories into the read-only /usr/share/ directory. Remind that you must not run the Composer as a root user.

sudo mv composer.phar /usr/local/bin/composer
cd /usr/share/nginx

Now we are going to generate a new Laravel project and deploy it to check whether the Composer can load the Laravel PHP framework or not.

composer create-project laravel/laravel test dev-master --prefer-dist

If you need, you can grant access permission to the Nginx server by using the chown command in the terminal shell.

chown -R www-data:www-data test/

You can also override and replace the available site information in the Nginx web server to ensure that your localhost address is placed perfectly.

mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old

Laravel PHP Framework installing

Check whether the Composer tool is installed correctly or not. Use the following command to check the version of the Composer tool.

composer

composer

Now go to the test directory to access the storage permission. Remember, you must have to grand the storage access to the database server to load your server. Use the following commands-lines in your Ubuntu terminal shell to grand the storage access to the Nginx server.

cd test/
chmod -R 0777 storage/
mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old

Here, we have to edit the Nginx server script according to the PHP framework that we have set previously to make an environment for the Laravel PHP framework. Use the command-line given below to edit the default settings of the Nginx server.

gedit /etc/nginx/sites-available/default

Now copy and paste the following script given below into your Nginx server script. Then save and exit the script.

server {
    listen 80 default_server 
    listen [::]:80 default_server ipv6only=on 
    root /usr/share/nginx/test/public 
    index index.php index.html index.htm 
    server_name 127.0.0.1 
    location / {
        try_files $uri $uri/ /index.php?$query_string 
    }
    location ~ .php$ {
        try_files $uri /index.php =404 
        fastcgi_split_path_info ^(.+.php)(/.+)$ 
    fastcgi_pass 127.0.0.1:8000 
        fastcgi_index index.php 
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name 
        include fastcgi_params 
    }
}

In the script setting, I have kept the server name as localhost (127.0.0.1) and used the port 8000. You can use any address and port you want. Just make sure that you are using an available port to load your server. Moreover, if you are a network and IT expert, you can use the Nginx port forwarding method to redirect the address at the desired port.

Now, restart the Nginx server to refresh the Laravel PHP framework on your Ubuntu machine. If you have done everything right, you will be able to load the Laravel PHP framework on your web browser by entering the IP address and port you set.

systemctl restart nginx

Laravel PHP Framework browser

Remove Laravel PHP Framework from Ubuntu Linux


There are many reasons that you might want to remove the Laravel PHP framework form your Ubuntu machine. You might want a fresh installation of Laravel, or you might want to switch back to your previous PHP framework. Whatever the reason is, here is the command-line that you can use to remove the Laravel PHP framework from your Linux machine.

composer global remove laravel/installer

remove laravel

Final Thoughts


Despite having some minor technical issues, Laravel is developing its service rapidly by updating the framework regularly. The user community of Laravel is also very active and helpful to others. You can build any small, medium, or large projects with Laravel. You can follow the Laravel Stack Overflow community to get help.

In this post, I have described the fundamental thoughts of the PHP Laravel framework. I have also illustrated the steps of how you can install the Laravel PHP framework on your Ubuntu Linux. If you find this post useful and informative, please share this post with your web developer community. And, you can also ask anything related to this post in the comment section.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

1 COMMENT

  1. hi, thanks for the tutorial. I found that the lines in the Nginx server script needed an ending with ;

    server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    root /usr/share/nginx/test/public ;
    index index.php index.html index.htm ;
    server_name 127.0.0.1 ;
    location / {
    try_files $uri $uri/ /index.php?$query_string ;
    }
    location ~ .php$ {
    try_files $uri /index.php =404 ;
    fastcgi_split_path_info ^(.+.php)(/.+)$ ;
    fastcgi_pass 127.0.0.1:8000 ;
    fastcgi_index index.php ;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name ;
    include fastcgi_params ;
    }
    }

LEAVE A REPLY

Please enter your comment!
Please enter your name here

You May Like It!

Trending Now