Install Laravel 5.1 Ubuntu 16.04 Nginx


Install Laravel 5.1 on Ubuntu 16.04 Nginx

Introduction

Laravel is a free, open-source PHP web application framework that is highly popular with modern web developers. It leverages an expressive and elegant syntax to quickly develop robust web applications. In this tutorial, you’ll install Laravel 5.1 on an Ubuntu 16.04 server running Nginx.

Prerequisites

Before you begin with this guide, you should have a separate, non-root user account with sudo privileges set up on your server. To learn how to set up such a user account, follow our initial server setup guide for Ubuntu 16.04.

You’ll also need to make sure that you have Nginx installed and running on your system. If you haven’t installed Nginx yet, you can do so by following steps 1 and 2 of this guide, which explain how to add the Nginx repository to your system and install Nginx from it.

Install PHP and Related Modules

Laravel 5.1 requires PHP 5.5.9 or higher to run, so your first step will be to install it. To do this, type this apt-get command:

sudo apt-get update
sudo apt-get -y install php7.0-fpm php7.0-mbstring php7.0-xmlrpc php7.0-soap php7.0-gd php7.0-xml php7.0-cli php7.0-zip

This will install these required PHP modules:

  • php7.0-fpm – Required for running PHP as an Nginx FastCGI process.
  • php7.0-mbstring – Required for processing strings.
  • php7.0-xmlrpc – Required for server communication over XML-RPC protocol.
  • php7.0-soap – Required for server communication over SOAP protocol.
  • php7.0-gd – Required for creating graphics with the GD library.
  • php7.0-xml – Required for processing XML documents.
  • php7.0-cli – Required for running the PHP command-line interface.
  • php7.0-zip – Required for processing ZIP archives.

After all of these modules are installed, you can proceed.

Install and Configure Composer

Composer is a popular dependency management tool for PHP, created mainly to facilitate installation and updates for project dependencies.

To install Composer on your server, type this curl command:

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

After the installation is complete, you can verify that the installed version is correct by typing:

composer –version

This will output something like this: Composer version 1.6.3 2018-01-31 16:28:17.

Install Laravel

Now that you have composer installed, you’re ready to install Laravel. First, create a directory where the Laravel files will live. To do this, enter the following commands:

sudo mkdir -p /var/www/laravel
sudo chown -R $USER:$USER /var/www/laravel
cd /var/www/laravel

Next, enter the command below to download Laravel 5.1:

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

This command will download all of the necessary files into a “blog” directory inside the “laravel” directory. To finish setting up your Laravel installation, enter the following commands:

cd blog
chmod -R 755 storage
chown -R www-data:www-data *

Create a Nginx Server Block for Laravel

Now that you have Laravel installed, it’s time to configure a server block to serve the files. To do this, create a file called laravel in the /etc/nginx/sites-available directory:

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

Paste these lines into the file:

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

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

location ~ .php$ {
try_files $uri /index.php =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

}

Once you have pasted the configuration into the file, save and close it. To enable the configuration, create a symbolic link from the file to the sites-enabled directory by typing this command:

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

After creating the symbolic link, type this command to check for syntax errors in the Nginx configuration:

sudo nginx -t

If the command returns no errors, restart the Nginx service by typing:

sudo systemctl restart nginx

Conclusion

You should now have Laravel 5.1 installed and running on your Ubuntu 16.04 server. You can access your application by visiting your server’s IP address in your web browser.

FAQs

  • Do I need Apache to run Laravel? – No, you can use Nginx as well. This guide shows how to install Laravel on an Ubuntu 16.04 server running Nginx.
  • What PHP version do I need for Laravel 5.1? – Laravel 5.1 requires PHP 5.5.9 or higher.

Thank you for reading this article. For more information, please read our other articles on Laravel and Ubuntu.