Quantcast
Channel: Tech Support
Viewing all articles
Browse latest Browse all 880

How to Set Up Laravel PHP Framework on Ubuntu 16.04

$
0
0

Laravel is a free and open source Php framework with model view controller design pattern.



This tutorial walks you through the steps to install Laravel release 5.4 with Nginx, PHP-FPM 7.0 and MariaDB on an Ubuntu 16.04 server.


Prerequisites

One Ubuntu 16.04 server installed either on bare-metal hardware or on virtual machine with a user that has sudo privileges


Update Ubuntu

First, we need to update Ubuntu repository and upgrade all packages to the latest version.

sudo apt-get update
sudo apt-get upgrade


Install Nginx

Now, we will install Nginx (for Web Services) using the following command.

sudo apt-get install -y nginx

Once installation done, start Nginx and make it persistent on reboot:

systemctl start nginx
systemctl enable nginx

Nginx runs on port 80 by default, you can verify using the netstat command:

netstat -plntu

or using the curl command:

curl localhost


Install PHP-FPM 7

At this point, we will install PHP 7.0 and some PHP extensions needed by Laravel.

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

Next, go to the PHP configuration directory and edit php.ini file present in the fpm directory.

cd /etc/php/7.0/
sudo nano fpm/php.ini

Uncomment the following CGI line, and change the value to 0.

cgi.fix_pathinfo=0

Save and exit

Now start PHP-FPM service and make it persistent on reboot.

systemctl start php7.0-fpm
systemctl enable php7.0-fpm

By default on Ubuntu, PHP-FPM is running under the sock file. Check the PHP-FPM sock file with the netstat command:

netstat -pl | grep php7.0-fpm


Install MariaDB

This is optional, but when your Laravel project is based on MySQL, you need this for your project. Install MariaDB from the repository using the following command.

sudo apt-get install -y mariadb-server mariadb-client

After the installation is complete, start MariaDB service and make it persistent on reboot.

systemcl start mysql
systemctl enable mysql

By default MariaDB works on port 3306, verify it using the netstat command.

netstat -plntu

Now, configure the MariaDB root password with the 'mysql_secure_installation' command below.

mysql_secure_installation

Input your root password, remove anonymous users, disallow root login remotely etc.

Set root password? [Y/n] Y
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y


Install PHP Composer

The PHP composer is a package manager for the PHP programming language. We can install it using the following command.

sudo apt-get install -y composer

Run 'composer' command and you will get the results as shown in the image.



Configure Nginx Virtual Host for Laravel

We will configure the nginx virtual host under the '/var/www/laravel' directory for the laravel project.

mkdir -p /var/www/laravel

Next, go to the nginx configuration directory and create a new virtual host file 'laravel' under 'sites-available' directory.

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

Add the following configuration parameters:

server {
         listen 80;
         listen [::]:80 ipv6only=on;

         # Log files for Debugging
         access_log /var/log/nginx/laravel-access.log;
         error_log /var/log/nginx/laravel-error.log;

         # Webroot Directory for Laravel project
         root /var/www/laravel/public;
         index index.php index.html index.htm;

         # Your Domain Name
         server_name labserver.example.com;

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

         # PHP-FPM Configuration Nginx
         location ~ \.php$ {
                 try_files $uri =404;
                 fastcgi_split_path_info ^(.+\.php)(/.+)$;
                 fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                 fastcgi_index index.php;
                 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                 include fastcgi_params;
         }
 }

Save and exit.

Now activate the virtual host by creating a symlink of the 'laravel' file to the 'sites-enabled' directory. Then test nginx configuration and make sure there is no error.

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

Now restart nginx service.

systemctl restart nginx


Install Laravel

Before we start Laravel installation, make sure unzip utility is installed on Ubuntu. If you do not have the tool, install it using the following command.

sudo apt-get install unzip

Go to that directory.

cd /var/www/laravel

There are two methods to install Laravel. Installing using the Laravel Installer or Installing using the Composer create project. In this guide, We will install latest Laravel version 5.4 using the composer create project.

composer create-project laravel/laravel .

And after the installation is complete. We need to change the ownership of the Laravel project directory to 'www-data' user, and change the permission of the storage directory to 755.

chown -R www-data:root /var/www/laravel
chmod 755 /var/www/laravel/storage


Test Laravel Configuration

In the virtual host configuration file, we've already defined the domain name for Laravel 'labserver.example.com'.

Open your web browser and navigate to the domain you installed Laravel on, here on the lab server is http://labserver.example.com/

And you should see the Laravel home page as shown in image below.



Conclusion

In this guide, we installed Laravel Nginx, PHP-FPM 7 and MariaDB on an Ubuntu 16.04 server in order to set up programming frame work for our development projects.

Viewing all articles
Browse latest Browse all 880

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>