Install Laravel Nginx Ubuntu 18.04


Install Laravel Nginx on Ubuntu 18.04

Introduction

In this tutorial, we will look at how to install Laravel with Nginx on an Ubuntu 18.04 server. Laravel is a powerful PHP web framework designed for rapid application development. It provides a robust set of tools and resources to create modern web applications. Nginx is an open source web server that offers high performance and stability. In this guide, we will install Laravel and configure Nginx to serve the application.

Prerequisites

Before continuing with this tutorial, make sure you have a non-root user with sudo privileges set up on your system. To learn how to set up a user with these privileges, follow our guide on How to Use Sudo. This guide also assumes that you have a domain name pointing to your Ubuntu 18.04 server. If you do not have a domain name yet, we recommend following our guide on How To Point to DigitalOcean Nameservers From Common Domain Registrars.

Step 1 – Installing Nginx

Let’s start by installing Nginx on your server. You can install it by running the following command:

sudo apt update
sudo apt install nginx

Once Nginx is installed, you can start and enable it by running the following command:

sudo systemctl start nginx
sudo systemctl enable nginx

It is also recommended that you tweak a few settings in Nginx’s configuration file. This file is located at /etc/nginx/nginx.conf. Open it in your text editor:

sudo nano /etc/nginx/nginx.conf

Once you have opened the file, look for the following line:

server_names_hash_bucket_size 64;

This line controls the size of buckets used to store server names. By increasing the size of the bucket, you can prevent HTTP header overflows. We recommend increasing the value to 128:

server_names_hash_bucket_size 128;

Once you are done, save and close the file, and then test your Nginx configuration for any syntax error by running the following command:

sudo nginx -t

If you have any syntax errors in your configuration file, you will see an output like this:

nginx: [emerg] "server_names_hash_bucket_size" directive is duplicate in /etc/nginx/nginx.conf:68
nginx: configuration file /etc/nginx/nginx.conf test failed

This means that you have used the same server_names_hash_bucket_size directive more than once. Remove any duplicate directive, save the file and test for syntax errors again.

Step 2 – Installing MySQL

Laravel uses a database to store its data. In this guide, we will use MySQL. You can install it by running the following command:

sudo apt install mysql-server

Once the installation is complete, you can secure the installation by running the following command:

sudo mysql_secure_installation

This command will guide you through the process of setting a secure root password, removing anonymous users, and disallowing remote root login. Once you are done, you can log in to the MySQL shell by running the following command:

mysql -u root -p

Next, create a database for Laravel by running the following command:

CREATE DATABASE laravel;

Step 3 – Installing PHP

Next, you will need to install PHP and several PHP extensions. We will use the FPM version of PHP. You can install it and the necessary extensions by running the following command:

sudo apt install php-fpm php-mysql

Once PHP is installed, you can proceed to the next step.

Step 4 – Installing Composer

Composer is a dependency manager for PHP applications. It is used to install and manage the libraries and dependencies required by the Laravel application. You can install composer by running the following command:

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

Once the installation completes, you can check the install version of composer by running the following command:

composer --version

Step 5 – Installing Laravel

Once you have all of the components installed, you can now proceed to downloading and installing Laravel. First, change the current directory to where you want to install Laravel:

cd /var/www

Next, download the Laravel installer using the following command:

composer create-project --prefer-dist laravel/laravel laravel

This will download the latest version of Laravel to the laravel directory. Next, give ownership of the laravel directory to the www-data user by running the following command:

sudo chown -R www-data:www-data laravel/

Finally, adjust the permissions of the directory by running the following command:

sudo chmod -R 755 laravel/

Step 6 – Configuring Nginx

Now that all of the components are installed, you can configure Nginx to serve the Laravel application. You can do this by creating a Virtual Host file stored in the sites-available directory:

sudo nano /etc/nginx/sites-available/example.com

Add the following lines to the file:

server {
listen 80;
root /var/www/laravel/public;
index index.php index.html index.htm;
server_name example.com www.example.com;

location / {
try_files $uri $uri/ /index.php?$query_string;
}

location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}

Save and close the file and then enable the site by creating a symbolic link from the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

Test Nginx for any syntax errors and then restart the service for the changes to take effect:

sudo nginx -t
sudo systemctl restart nginx

Once you are done, you can now access your Laravel application in your web browser by visiting http://your_domain.

Conclusion

In this tutorial, we have shown you how to install Laravel and configure Nginx on Ubuntu 18.04. Using the above steps, you can now easily install and configure a Laravel application on your server.

Frequently Asked Questions (FAQ)

Q: What is Laravel?

A: Laravel is a powerful open source PHP web framework designed for rapid application development.

Q: What is Nginx?

Leave a Reply

Your email address will not be published. Required fields are marked *