{"id":2118,"date":"2017-12-15T17:22:16","date_gmt":"2017-12-15T14:22:16","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/how-to-install-laravel-5-with-nginx-on-ubuntu-1604\/"},"modified":"2017-12-15T17:22:16","modified_gmt":"2017-12-15T14:22:16","slug":"how-to-install-laravel-5-php-framework-with-nginx-on-ubuntu-16-04","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/how-to-install-laravel-5-php-framework-with-nginx-on-ubuntu-16-04\/","title":{"rendered":"How to Install Laravel 5 PHP Framework with Nginx on Ubuntu 16.04"},"content":{"rendered":"<p>Laravel is a free and open source PHP framework with MVC (Model-View-Controller) design pattern. Created by Taylor Otwell on 2011, it as an attempt to provide an advanced alternative to the CodeIgniter (CI) framework. In\u00a02011, Laravel released version 1 and version 2, and the latest\u00a0version 5.4 comes with more and improved features like Command-Line (CLI) support named &#8216;artisan&#8217;, support for different database systems, Route improvements etc.<\/p>\n<p>In this tutorial, we will guide you step-by-step on how to install Laravel version 5.4 with Nginx as the web server, PHP-FPM 7.0 and MariaDB. Laravel 5.4 will run under LEMP stack with Ubuntu 16.04 as OS version.<\/p>\n<p><strong>What we will do<\/strong><\/p>\n<ol>\n<li>Update Ubuntu<\/li>\n<li>Install Nginx<\/li>\n<li>Install PHP-FPM 7.0<\/li>\n<li>Install MariaDB<\/li>\n<li>Install PHP Composer<\/li>\n<li>Configure Nginx Virtual Host for Laravel<\/li>\n<li>Install Laravel<\/li>\n<li>Testing<\/li>\n<\/ol>\n<p><strong>Prerequisites<\/strong><\/p>\n<ul>\n<li>Ubuntu 16.04 server<\/li>\n<li>Root privileges<\/li>\n<\/ul>\n<h2 id=\"step-update-ubuntu\">Step 1 &#8211; Update Ubuntu<\/h2>\n<p>Before we start with\u00a0the installation, we need to update the repository and then update all packages to the latest version. So to do that, first login to the Ubuntu server using\u00a0ssh.<\/p>\n<p class=\"command\">ssh <a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"deacb1b1aa9eefe7ecf0efe8e6f0ededf0efee\" target=\"_blank\" rel=\"noopener\">[email\u00a0protected]<\/a><\/p>\n<p>Now, update your Ubuntu repository and upgrade all packages on your system to the latest version.<\/p>\n<p class=\"command\">sudo apt-get update<br \/>sudo apt-get upgrade<\/p>\n<p>Reboot your server and then connect again using\u00a0ssh.<\/p>\n<h2 id=\"step-install-nginx\">Step 2 &#8211; Install Nginx<\/h2>\n<p>In this step, we will install Nginx 1.10 in\u00a0the system. It&#8217;s available in the repository, you just need to install it with the following apt command.<\/p>\n<p class=\"command\">sudo apt-get install -y nginx<\/p>\n<p>After\u00a0the installation has been completed, start Nginx and enable\u00a0it to start automatically at system\u00a0boot, something which you can do using the following systemctl commands.<\/p>\n<p class=\"command\">systemctl start nginx<br \/>systemctl enable nginx<\/p>\n<p>Nginx is running on port 80, check related stats using the\u00a0netstat command in the following way.<\/p>\n<p class=\"command\">netstat -plntu<\/p>\n<p><a class=\"fancybox\" id=\"img-1\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_laravel_5_on_with_nginx_on_ubuntu_1604\/big\/1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2017\/12\/how-to-install-laravel-5-php-framework-with-nginx-on-ubuntu-16-04.png\" alt=\"Check if Nginx is started\" width=\"500\" height=\"160\" title=\"\"><\/a><\/p>\n<p>Or you can use the curl command as well.<\/p>\n<p class=\"command\">curl localhost<\/p>\n<h2 id=\"step-install-phpfpm-\">Step 3 &#8211; Install PHP-FPM 7<\/h2>\n<p>Nginx is installed, and now we need to install PHP-FPM version on the system. We will install PHP 7.0 and some PHP extensions needed by Laravel.<\/p>\n<p>Install PHP and PHP-FPM 7.0 with all extensions needed using\u00a0the following command.<\/p>\n<p class=\"command\">sudo apt-get install -y php7.0 php7.0-curl php7.0-common php7.0-cli php7.0-mysql php7.0-mbstring php7.0-fpm php7.0-xml php7.0-zip<\/p>\n<p>Next, go to the PHP configuration directory and edit php.ini file present in\u00a0the fpm directory.<\/p>\n<p class=\"command\">cd \/etc\/php\/7.0\/<br \/>vim fpm\/php.ini<\/p>\n<p>Uncomment the following CGI line, and change the value to 0.<\/p>\n<pre class=\"system\">cgi.fix_pathinfo=0<\/pre>\n<p>That&#8217;s it. Save and exit.<\/p>\n<p>Now we can start PHP-FPM and enable\u00a0it to launch automatically at system boot.<\/p>\n<p class=\"command\">systemctl start php7.0-fpm<br \/>systemctl enable php7.0-fpm<\/p>\n<p>By default on Ubuntu, PHP-FPM is running under the sock file. Check the PHP-FPM sock file with the netstat command in the following way:<\/p>\n<p class=\"command\">netstat -pl | grep php7.0-fpm<\/p>\n<p>PHP and PHP-FPM 7.0 installed.<\/p>\n<p><a class=\"fancybox\" id=\"img-2\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_laravel_5_on_with_nginx_on_ubuntu_1604\/big\/2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2017\/12\/how-to-install-laravel-5-php-framework-with-nginx-on-ubuntu-16-04-1.png\" alt=\"Install and test PHP-FPM\" width=\"500\" height=\"123\" title=\"\"><\/a><\/p>\n<h2 id=\"step-install-mariadb\">Step 4 &#8211; Install MariaDB<\/h2>\n<p>This is optional, but when your Laravel project is based on MySQL, you need to do this for your project. We will install MariaDB latest version 10.0 on to the system.<\/p>\n<p>Install MariaDB from the repository using\u00a0the following command.<\/p>\n<p class=\"command\">sudo apt-get install -y mariadb-server mariadb-client<\/p>\n<p>After the installation is complete, run MariaDB and enable\u00a0it to launch at system boot.<\/p>\n<p class=\"command\">systemcl start mysql<br \/>systemctl enable mysql<\/p>\n<p>MariaDB started on port 3306, check it using the\u00a0netstat command.<\/p>\n<p class=\"command\">netstat -plntu<\/p>\n<p><a class=\"fancybox\" id=\"img-3\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_laravel_5_on_with_nginx_on_ubuntu_1604\/big\/3.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2017\/12\/how-to-install-laravel-5-php-framework-with-nginx-on-ubuntu-16-04-2.png\" alt=\"Start and test MariaDB\" width=\"500\" height=\"181\" title=\"\"><\/a><\/p>\n<p>Next, configure the MariaDB root password with the &#8216;mysql_secure_installation&#8217; command below.<\/p>\n<p class=\"command\">mysql_secure_installation<\/p>\n<p>Input your root password, remove anonymous users, disallow root login remotely etc.<\/p>\n<pre class=\"system\">Set root password? [Y\/n] Y<br\/>Remove anonymous users? [Y\/n] Y<br\/>Disallow root login remotely? [Y\/n] Y<br\/>Remove test database and access to it? [Y\/n] Y<br\/>Reload privilege tables now? [Y\/n] Y<\/pre>\n<p>MariaDB installation and configuration has been completed.<\/p>\n<h2 id=\"step-install-php-composer\">Step 5 &#8211; Install PHP Composer<\/h2>\n<p>The composer is a package manager for the PHP programming language. Created on 2011, it&#8217;s inspired by Node.js &#8216;npm&#8217; and Ruby&#8217;s named &#8216;bundler&#8217;. On ubuntu 16.04, composer is available in the repository, so we can install it using the\u00a0apt command.<\/p>\n<p>Install PHP Composer with the following command.<\/p>\n<p class=\"command\">sudo apt-get install -y composer<\/p>\n<p>After\u00a0the installation has been completed, run &#8216;composer&#8217; command and you will get the\u00a0results as shown in the screenshot.<\/p>\n<p class=\"command\">composer<\/p>\n<p><a class=\"fancybox\" id=\"img-4\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_laravel_5_on_with_nginx_on_ubuntu_1604\/big\/4.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2017\/12\/how-to-install-laravel-5-php-framework-with-nginx-on-ubuntu-16-04-3.png\" alt=\"Install Composer\" width=\"500\" height=\"209\" title=\"\"><\/a><\/p>\n<p>PHP Composer installed on Ubuntu 16.04.<\/p>\n<h2 id=\"step-configure-nginx-virtual-host-for-laravel\">Step 6 &#8211; Configure Nginx virtual host for Laravel<\/h2>\n<p>In this step, we will configure the nginx virtual host for Laravel. But before we do that, we must decide the directory for our Laravel project. In our case, we will use &#8216;\/var\/www\/laravel&#8217; directory for the project. Just create it with mkdir.<\/p>\n<p class=\"command\">mkdir -p \/var\/www\/laravel<\/p>\n<p>Next, go to the nginx configuration directory and create a new virtual host file &#8216;laravel&#8217; under &#8216;sites-available&#8217; directory.<\/p>\n<p class=\"command\">cd \/etc\/nginx\/<br \/>vim sites-available\/laravel<\/p>\n<p>Paste the following configuration there.<\/p>\n<pre class=\"system\" readability=\"19\">server {<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 listen 80;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 listen [::]:80 ipv6only=on;<p>\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 # Log files for Debugging<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 access_log \/var\/log\/nginx\/laravel-access.log;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error_log \/var\/log\/nginx\/laravel-error.log;<\/p><p>\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 # Webroot Directory for Laravel project<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 root \/var\/www\/laravel\/public;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 index index.php index.html index.htm;<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # Your Domain Name<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 server_name laravel.hakase-labs.co;<\/p><p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 location \/ {<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try_files $uri $uri\/ \/index.php?$query_string;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/p><p>\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 # PHP-FPM Configuration Nginx<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 location ~ \\.php$ {<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 try_files $uri =404;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fastcgi_split_path_info ^(.+\\.php)(\/.+)$;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fastcgi_pass unix:\/var\/run\/php\/php7.0-fpm.sock;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fastcgi_index index.php;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 include fastcgi_params;<br\/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<br\/>}<\/p><\/pre>\n<p>That&#8217;s it. Save and exit.<\/p>\n<p>Now activate the virtual host by creating a symlink of the &#8216;laravel&#8217; file to the &#8216;sites-enabled&#8217; directory. Then test nginx configuration and make sure there is no error.<\/p>\n<p class=\"command\">ln -s \/etc\/nginx\/sites-available\/laravel \/etc\/nginx\/sites-enabled\/<br \/>nginx -t<\/p>\n<p>Virtual host for laravel has been created. Now restart nginx.<\/p>\n<p class=\"command\">systemctl restart nginx<\/p>\n<p><a class=\"fancybox\" id=\"img-5\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_laravel_5_on_with_nginx_on_ubuntu_1604\/big\/5.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2017\/12\/how-to-install-laravel-5-php-framework-with-nginx-on-ubuntu-16-04-4.png\" alt=\"Install Laravel PHP Framework\" width=\"500\" height=\"132\" title=\"\"><\/a><\/p>\n<h2 id=\"step-install-laravel\">Step 7 &#8211; Install Laravel<\/h2>\n<p>Before we start with Laravel installation, make sure unzip utility is installed on your system. If you do not have the tool, install it using the following command.<\/p>\n<p class=\"command\">sudo apt-get install unzip<\/p>\n<p>We&#8217;ve already defined a directory for Laravel : &#8216;\/var\/www\/laravel&#8217; directory. Go to that directory.<\/p>\n<p class=\"command\">cd \/var\/www\/laravel<\/p>\n<p>Install Laravel with the composer command. There are\u00a0two ways to install Laravel: Installing via the Laravel Installer and Installing via Composer create project. We will install latest Laravel version 5.4 via the composer create project.<\/p>\n<p>Run the following composer command.<\/p>\n<p class=\"command\">composer create-project laravel\/laravel .<\/p>\n<p>You need to wait for the Laravel installation.<\/p>\n<p><a class=\"fancybox\" id=\"img-6\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_laravel_5_on_with_nginx_on_ubuntu_1604\/big\/6.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2017\/12\/how-to-install-laravel-5-php-framework-with-nginx-on-ubuntu-16-04-5.png\" alt=\"Wait until Laravel installation finished\" width=\"500\" height=\"153\" title=\"\"><\/a><\/p>\n<p>And after\u00a0the installation is complete.<\/p>\n<p><a class=\"fancybox\" id=\"img-7\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_laravel_5_on_with_nginx_on_ubuntu_1604\/big\/7.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2017\/12\/how-to-install-laravel-5-php-framework-with-nginx-on-ubuntu-16-04-6.png\" alt=\"Laravel installation complete\" width=\"500\" height=\"123\" title=\"\"><\/a><\/p>\n<p>We need to change the ownership of the Laravel project directory to &#8216;<strong>www-data<\/strong>&#8216; user, and change the permission of the storage directory to 755.<\/p>\n<p class=\"command\">chown -R www-data:root \/var\/www\/laravel<br \/>chmod 755 \/var\/www\/laravel\/storage<\/p>\n<p>Laravel Installation has been completed without any error.<\/p>\n<h2 id=\"step-testing\">Step 8 &#8211; Testing<\/h2>\n<p>In\u00a0the virtual host configuration file, we&#8217;ve already defined the domain name for Laravel &#8216;<strong>laravel.hakase-labs.co<\/strong>&#8216;.<\/p>\n<p>Open your web browser and visit the domain you installed Laravel on, mine here on the test server is <strong><em>http:\/\/laravel.hakase-labs.co\/<\/em><\/strong><\/p>\n<p>And you should\u00a0see the Laravel home page.<\/p>\n<p><a class=\"fancybox\" id=\"img-8\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_laravel_5_on_with_nginx_on_ubuntu_1604\/big\/8.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2017\/12\/how-to-install-laravel-5-php-framework-with-nginx-on-ubuntu-16-04-7.png\" alt=\"Laravel homepage\" width=\"500\" height=\"223\" title=\"\"><\/a><\/p>\n<p>Laravel Installation with Nginx, PHP-FPM 7 and MariaDB on Ubuntu 16.04 is successful.<\/p>\n<h2 id=\"references\">References<\/h2>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Laravel is a free and open source PHP framework with MVC (Model-View-Controller) design pattern. Created by Taylor Otwell on 2011, it as an attempt to provide an advanced alternative to the CodeIgniter (CI) framework. In\u00a02011, Laravel released version 1 and version 2, and the latest\u00a0version 5.4 comes with more and improved features like Command-Line (CLI) &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-2118","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2118","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=2118"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/2118\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=2118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=2118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=2118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}