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?
Related Posts:
- How To Install Phpmyadmin On Nginx How To Install Phpmyadmin On Nginx Introduction PhpMyAdmin is an open source software program which is used to manage MySQL and MariaDB databases. It provides a graphical interface to execute…
- Digitalocean Letsencrypt Nginx Ubuntu 18.04 Digitalocean Letsencrypt Nginx Ubuntu 18.04 Introduction Ubuntu 18.04 is the latest version of the popular Linux operating system. Digitalocean is a cloud hosting provider that specializes in hosting and managing…
- Laravel In Local Nginx Windows Laravel In Local Nginx Windows Introduction Laravel is an open-source PHP framework that allows you to quickly create robust web applications. A large part of the development process for any…
- Install Nginx Phpmyadmin Ubuntu 18.04 Install Nginx Phpmyadmin Ubuntu 18.04 Introduction to Nginx, PHP, and Ubuntu Nginx is an open-source, high-performance web server written in C and used to serve static and dynamic webpages. It…
- Install Nginx 1.16 Ubuntu Install Nginx 1.16 Ubuntu Introduction to Nginx 1.16 Nginx 1.16 is a web server that helps you to serve web content quickly, efficiently and securely. It is especially popular with…
- Php-Fpm Nginx Ubuntu 18.04 PHP-FPM & Nginx on Ubuntu 18.04 Introduction to PHP-FPM PHP-FPM (FastCGI Process Manager) is an implementation of FastCGI, which is a standard protocol for interfacing external applications with web servers.…
- How To Install Nginx In Ubuntu How To Install Nginx In Ubuntu Introduction to Nginx Nginx is a very powerful web server for hosting websites and applications. It is a fast and reliable server, and is…
- Ubuntu Install Nginx Php7.2 Mysql Ubuntu Install Nginx Php7.2 Mysql What is Nginx? Nginx is an open source web server that is very popular for powering web applications such as WordPress and Drupal. It is…
- Hhvm Nginx Ubuntu 16.4 HHVM Nginx Ubuntu 16.4 What is HHVM? HHVM, also known as HipHop Virtual Machine, is a virtual machine developed by Facebook to speed up the execution of PHP code. It…
- Nginx Reverse Proxy Subdirectory Laravel Nginx Reverse Proxy Subdirectory Laravel What is Nginx? Nginx is a popular open-source web server used for running web applications. It is fast and can handle large amounts of traffic.…
- Install Php Nginx Ubuntu 12.04 Install Php Nginx Ubuntu 12.04 Introduction Welcome to our guide on How to install PHP, Nginx & MySQL on Ubuntu 12.04 LTS. This guide should work on other Linux VPS…
- Setting Serverblock For Domain Using Nginx On Ubuntu 18.04 Setting ServerBlock For Domain Using Nginx On Ubuntu 18.04 Introduction Nginx is a powerful open-source web server that can be used for serving static, dynamic websites and applications. Nginx is…
- How To Configure Websocket Nginx Fpm How To Configure Websocket Nginx Fpm Introduction Websocket is a modern web technology that provides bidirectional communication between a web server and a web client. The websocket protocol allows for…
- Laravel 5.4 Vps Nginx Config File Centos 7 Laravel 5.4 VPS Nginx Config File Centos 7 What is a Nginx Config File? A config file is a settings file used by Nginx server to configure how it behaves…
- Laravel Nginx Config Ubuntu 18 Laravel Nginx Config Ubuntu 18 Introduction Laravel is a powerful web-based MVC (Model-View-Controller) framework used by developers to create web applications, websites and APIs. It is based on the popular…
- Install Wordpress On Ubuntu 18.04 Nginx Install WordPress On Ubuntu 18.04 Nginx Introduction To Ubuntu And Nginx Ubuntu is a popular open-source operating system which has gained immense popularity over the years. It is easy to…
- Move On Nginx Web Root To A New Location Laravel Move On Nginx Web Root To A New Location Laravel Introduction Nginx is a web server and reverse proxy for sites running on the web. It’s fast and efficient, and…
- Laravel Nginx Default Multiple Site Laravel Nginx Default Multiple Site What is Nginx? Nginx is a popular open source web server used for hosting websites on the internet. It is designed for high-traffic websites and…
- Install Pdo_Mysql Ubuntu Nginx Install Pdo_Mysql Ubuntu Nginx What is Pdo_Mysql? PDO_Mysql is a driver for the PHP Data Objects (PDO) extension that provides a database abstraction layer for working with MySQL databases.PDO_Mysql provides…
- Check Ok For Nginx Confgiruration On Ubuntu Check OK for Nginx Confgiruation On Ubuntu What is Nginx? Nginx is an open-source web server that is renowned for its scalability and agility. It was originally designed as an…
- Add Root Password Mysql Nginx Ubuntu 16.04 Add Root Password Mysql Nginx Ubuntu 16.04 What is a root password? In the context of computer security, a root password is a user account that is given access to…
- Nginx Laravel 5.5 500 NGINX Laravel 5.5 500 What is NGINX Laravel? NGINX Laravel is an open source web server and reverse proxy software that is designed to provide robust web hosting services. It…
- Ubuntu Server18 How To Enable Nginx Pdo Mysql Ubuntu Server18: How to Enable Nginx Pdo Mysql As a developer, you may have heard of Nginx, PDO, and MySQL – all are essential components of web applications. Nginx is…
- Deploy Laravel Nginx Ubuntu 18 Deploy Laravel Nginx Ubuntu 18 Intro to Laravel Laravel is a free, open-source, Model-View-Controller (MVC) web framework written in PHP. It has become one of the most popular web development…
- Ubuntu Server Postgresql Nginx Php Digitalocean Laravel Ubuntu Server Postgresql Nginx Php Digitalocean Laravel Introduction To The Stack Ubuntu Server, Postgresql, Nginx, PHP, Digitalocean and Laravel are an exceptional combination of elements that, when put together, make…
- Change Env Laravel Not Affecting In Nginx Server Change Env Laravel Not Affecting In Nginx Server What is Nginx? Nginx is a web server that is developed for high performance and scalability on a host. It can be…
- Install Nginx-Naxsi Ubuntu 16.04 Install Nginx-Naxsi on Ubuntu 16.04 Understanding Nginx and Naxsi Nginx is a high-performance web server which can also be used as a reverse proxy or load balancer. Naxsi is a…
- Install Phpmyadmin On Nginx Ubuntu 18.04 Install Phpmyadmin On Nginx Ubuntu 18.04 Introduction PhpMyAdmin is one of the most popular and widely used web-based database management tools available. It is used for administering, managing and maintaining…
- Deploy Laravel In Local Nginx Windows Deploy Laravel In Local Nginx Windows 1. Introduction To Nginx Nginx is a web server that is primarily used to handle web traffic. It is open source, meaning it is…
- Install Nginx Passenger Ubuntu 16.04 Install Nginx Passenger Ubuntu 16.04 Installing Nginx On Ubuntu 16.04 Nginx is an open source web server that can be used to create web and application servers. It is a…