{"id":4735,"date":"2018-06-07T19:17:32","date_gmt":"2018-06-07T15:17:32","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/ubuntu-laravel-php-nginx\/"},"modified":"2018-06-07T19:17:32","modified_gmt":"2018-06-07T15:17:32","slug":"how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04\/","title":{"rendered":"How to Install Laravel 5.6 PHP Framework with Nginx on Ubuntu 18.04"},"content":{"rendered":"<p>Laravel is a free and open source PHP framework\u00a0that implements the MVC (Model-View-Controller) design pattern. Created by Taylor Otwell in 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.6 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, I will guide you step-by-step on how to install Laravel version 5.6 with Nginx as the web server, PHP-FPM 7.2, and MariaDB. Laravel 5.6 will run under LEMP stack with Ubuntu 18.04 as OS version.<\/p>\n<p>There is a version of this tutorial for\u00a0<a href=\"https:\/\/www.howtoforge.com\/tutorial\/how-to-install-laravel-5x-with-nginx-and-php-fpm-7-on-centos-7\/\" target=\"_blank\" rel=\"noopener\">Laravel on CentOS 7<\/a> as well.<\/p>\n<h2 id=\"what-we-will-do\">What we will do<\/h2>\n<ol>\n<li>Update Ubuntu Repository<\/li>\n<li>Install Nginx<\/li>\n<li>Install PHP-FPM 7.2<\/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<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<ul>\n<li>Ubuntu 18.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 the 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 ssh.<\/p>\n<p class=\"command\">ssh <a href=\"https:\/\/www.howtoforge.com\/cdn-cgi\/l\/email-protection\" class=\"__cf_email__\" data-cfemail=\"fc8e939388bc958c\" 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 update<br \/>sudo apt upgrade<\/p>\n<p>Reboot your server and then connect again using ssh.<\/p>\n<h2 id=\"step-install-nginx\">Step 2 &#8211; Install Nginx<\/h2>\n<p>In this step, we will install Nginx 1.14 to the system. It&#8217;s available on default ubuntu repository, you just need to install it with the following apt command.<\/p>\n<p class=\"command\">sudo apt install nginx -y<\/p>\n<p>After the installation is complete, start the nginx service and add it to start automatically at system boot using the systemctl command.<\/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 netstat command.<\/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_56_php_framework_with_nginx_on_ubuntu_1804_lts\/big\/1.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04.png\" alt=\"Enable and start Nginx web server\" width=\"500\" height=\"236\" title=\"\"><\/a><\/p>\n<p>Or you can use the curl command as well.<\/p>\n<p class=\"command\">curl -I localhost<\/p>\n<h2 id=\"step-install-php-and-phpfpm\">Step 3 &#8211; Install PHP 7.2 and PHP-FPM<\/h2>\n<p>Nginx is installed, and now we need to install PHP-FPM version 7 on the system. We will install PHP 7.2 and some PHP extensions needed by Laravel.<\/p>\n<p>Install PHP and PHP-FPM 7.2 with all extensions needed using the following command.<\/p>\n<p class=\"command\">sudo apt install php7.2 php7.2-curl php7.2-common php7.2-cli php7.2-mysql php7.2-mbstring php7.2-fpm php7.2-xml php7.2-zip -y<\/p>\n<p>Next, go to the PHP configuration directory and edit the php.ini file present in the fpm directory.<\/p>\n<p class=\"command\">cd \/etc\/php\/7.2\/<br \/>vim fpm\/php.ini<\/p>\n<p>Uncomment the following CGI line, and change the value to 0.<\/p>\n<pre>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 it to start automatically at system boot.<\/p>\n<p class=\"command\">systemctl start php7.2-fpm<br \/>systemctl enable php7.2-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.2-fpm<\/p>\n<p><a class=\"fancybox\" id=\"img-2\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_laravel_56_php_framework_with_nginx_on_ubuntu_1804_lts\/big\/2.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04-2.png\" alt=\"Install and start PHP-FPM\" width=\"500\" height=\"138\" title=\"\"><\/a><\/p>\n<p>PHP and PHP-FPM 7.2 has been installed on the Ubuntu 18.04 systems.<\/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.1 on to the system.<\/p>\n<p>Install MariaDB from the repository using the following command.<\/p>\n<p class=\"command\">sudo apt install mariadb-server mariadb-client -y<\/p>\n<p>After the installation is complete, run MariaDB and enable it 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 netstat 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_56_php_framework_with_nginx_on_ubuntu_1804_lts\/big\/3.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04-4.png\" alt=\"Start and check MariaDB\" width=\"500\" height=\"248\" 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>Set root password? [Y\/n] Y&#13;\nRemove anonymous users? [Y\/n] Y&#13;\nDisallow root login remotely? [Y\/n] Y&#13;\nRemove test database and access to it? [Y\/n] Y&#13;\nReload 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 in 2011, it&#8217;s inspired by Node.js &#8216;npm&#8217; and Ruby&#8217;s named &#8216;bundler&#8217;. On ubuntu 18.04, composer is available in the repository, so we can install it using the apt command.<\/p>\n<p>Install PHP Composer with the following command.<\/p>\n<p class=\"command\">sudo apt install composer -y<\/p>\n<p>After the installation has been completed, run &#8216;composer&#8217; command and you will get the results 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_56_php_framework_with_nginx_on_ubuntu_1804_lts\/big\/4.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04-6.png\" alt=\"Install PHP Composer\" width=\"500\" height=\"272\" title=\"\"><\/a><\/p>\n<p>PHP Composer installed on Ubuntu 18.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 the &#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>server {&#13; listen 80;&#13; listen [::]:80 ipv6only=on;&#13; &#13; # Log files for Debugging&#13; access_log \/var\/log\/nginx\/laravel-access.log;&#13; error_log \/var\/log\/nginx\/laravel-error.log;&#13; &#13; # Webroot Directory for Laravel project&#13; root \/var\/www\/laravel\/public;&#13; index index.php index.html index.htm;&#13; &#13; # Your Domain Name&#13; server_name laravel.hakase-labs.co;&#13; &#13; location \/ {&#13; try_files $uri $uri\/ \/index.php?$query_string;&#13; }&#13; &#13; # PHP-FPM Configuration Nginx&#13; location ~ \\.php$ {&#13; try_files $uri =404;&#13; fastcgi_split_path_info ^(.+\\.php)(\/.+)$;&#13; fastcgi_pass unix:\/run\/php\/php7.2-fpm.sock;&#13; fastcgi_index index.php;&#13; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;&#13; include fastcgi_params;&#13; }&#13; }<\/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.<\/p>\n<p>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>Nginx virtual host for laravel has been created, now restart the nginx service.<\/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_56_php_framework_with_nginx_on_ubuntu_1804_lts\/big\/5.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04-8.png\" alt=\"Configure Nginx for Laravel\" width=\"500\" height=\"186\" 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 the 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 install unzip -y<\/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 two ways to install Laravel: Installing via the Laravel Installer and Installing via Composer create project.<\/p>\n<p>We will install latest Laravel version 5.6 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_56_php_framework_with_nginx_on_ubuntu_1804_lts\/big\/6.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04-10.png\" alt=\"Install Laravel\" width=\"500\" height=\"174\" title=\"\"><\/a><\/p>\n<p>Laravel installation is complete.<\/p>\n<p><a class=\"fancybox\" id=\"img-7\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_laravel_56_php_framework_with_nginx_on_ubuntu_1804_lts\/big\/7.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04-12.png\" alt=\"Laravel installation complete\" width=\"500\" height=\"179\" title=\"\"><\/a><\/p>\n<p>We need to change the ownership of the Laravel project directory to &#8216;www-data&#8217; 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 the virtual host configuration file, we&#8217;ve already defined the domain name for Laravel &#8216;laravel.hakase-labs.co&#8217;.<\/p>\n<p>Open your web browser and visit the domain you installed Laravel on, mine here on the test server is <em><strong>http:\/\/laravel.hakase-labs.co\/<\/strong><\/em>.<\/p>\n<p>And you should see the Laravel home page.<\/p>\n<p><a class=\"fancybox\" id=\"img-8\" href=\"https:\/\/www.howtoforge.com\/images\/how_to_install_laravel_56_php_framework_with_nginx_on_ubuntu_1804_lts\/big\/8.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04-13.png\" alt=\"Laravel successfully installed\" width=\"500\" height=\"239\" title=\"\"><\/a><\/p>\n<p>Laravel Installation with Nginx, PHP-FPM 7.2 and MariaDB on Ubuntu 18.04 is successful.<\/p>\n<h2 id=\"references\">References<\/h2>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fubuntu-laravel-php-nginx%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04-15.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fubuntu-laravel-php-nginx%2F&amp;text=How+to+Install+Laravel+5.6+PHP+Framework+with+Nginx+on+Ubuntu+18.04&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04-17.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04-18.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fubuntu-laravel-php-nginx%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/06\/how-to-install-laravel-5-6-php-framework-with-nginx-on-ubuntu-18-04-20.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Laravel is a free and open source PHP framework\u00a0that implements the MVC (Model-View-Controller) design pattern. Created by Taylor Otwell in 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.6 comes with more and improved features like Command-Line &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-4735","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4735","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=4735"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/4735\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=4735"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=4735"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=4735"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}