This post shows students and new users how to install Laravel framework on Ubuntu Linux with Nginx. Laravel is an open source PHP web application framework with cool built-in features like routing, authentication, sessions, caching, and unit testing.

Laravel can be used as an alternative to the CodeIgniter framework. It is designed to be easy to use and to allow developers to create great applications. If you are looking for a simple and straightforward PHP framework to design your next application, you will find Laravel useful.

Also, for students and new users learning Linux, the easiest place to start learning is Ubuntu Linux. Ubuntu is the modern open source Linux operating system for desktops, servers, and other devices.

To start installing Laravel on Ubuntu Linux, follow the steps below:

How to install Nginx on Ubuntu Linux

If you are not using Laravel on a production server, then a web server is not needed. If you want to run Laravel with a web server, Nginx is a great option as it is the most popular web server in use today.

To install Nginx on Ubuntu, run the following commands.

sudo apt update
sudo apt install nginx

After installation, the following commands can be used to stop, start, and enable the Nginx service to always start with the server starting.

sudo systemctl stop nginx.service
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

How to install PHP on Ubuntu Linux

Laravel is based on PHP, so you will need to install PHP and related modules. At the time of writing, PHP 7.4 is the default in Ubuntu repositories.

YOU CAN ALSO READ:   How to install Joomla on Ubuntu Linux with Nginx

To install PHP and related modules, run the following commands

sudo apt install php7.4-fpm php7.4-intl php7.4-imap php7.4-mbstring php7.4-xmlrpc php7.4-soap php7.4-gd php7.4-xml php7.4-cli php7.4-zip curl

PHP must be installed and ready to use.

How to install Composer to download Laravel

Now that PHP is installed, you can download Laravel through Composer. Composer is a dependency manager for PHP that we will use to download the Laravel core and related Laravel components.

To do that, run the following commands to install composer.

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

After installing Composer, change to the root directory and run Composer to download the Laravel code packages and create a Laravel project folder called My project.

Laravel will automatically create a file called .env. These files include custom configuration variables, including database credentials. If you are using a database server, use this file to add your name and password.

cd /var/www/
sudo composer create-project laravel/laravel MyProject --prefer-dist

How to configure Nginx to be compatible with Laravel

Finally, configure the Apahce2 site configuration file for Laravel. This file will control how users access Laravel content. Run the following commands to create a new configuration file called laravel.conf

sudo nano /etc/nginx/sites-available/laravel.conf

Then copy and paste the content below into the file and save it. Replace the highlighted line with your own domain name and directory root location.

server {
    listen 80;
    server_name example.com;
    root /var/www/MyProject/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index 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/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.(?!well-known).* {
        deny all;
    }
}

Save the file and close.

YOU CAN ALSO READ:   How to install LAMP on Ubuntu Linux

How to enable Laravel Nginx server block

After configuring the VirtualHost above, enable it by running the following commands

sudo ln -s /etc/nginx/sites-available/laravel.conf /etc/nginx/sites-enabled/

After running the above commands, a new project directory will be created. Run the following commands to set the correct permissions for that directory.

sudo chown -R www-data:www-data /var/www/MyProject/
sudo chmod -R 755 /var/www/MyProject/

Then run the following command to restart Nginx by running the following commands.

sudo systemctl restart nginx.service

Finally, open your browser and find the domain name added in the configuration file above.

http://example.com

You should then see the default Laravel startup screen. You can start building your web applications through Laravel on Ubuntyu.

install laravel on ubuntu linux

You should do that!

Conclusion:

In this tutorial we have seen how to install Laravel framework on Ubuntu Linux with Nginx. If you find any errors above or have something to add, use the comment form below.

Write A Comment