How to install Laravel with Nginx on Ubuntu
How to install Laravel with Nginx on Ubuntu
Laravel is an open source PHP Web Framework which is used for the development of Web applications. It follows the Model-View-Controller (MVC) architecture pattern. It supports modular packaging system with a dedicated dependency manager, different ways for accessing relational databases. This tutorial covers the installation procedure of Laravel with Nginx on Ubuntu. This tutorial covers the installation procedure of Laravel with Nginx on Ubuntu.
Prerequisite
Before beginning with the installation procedure, setup LAMP environment for Laravel in the target system.
Installation procedure
To start the installation procedure, install the package for Nginx and press y to continue with the installation procedure.
root@linuxhelp1:~# apt-get install nginx -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
nginx-common nginx-core
Suggested packages:
fcgiwrap nginx-doc
The following NEW packages will be installed:
nginx nginx-common nginx-core
0 upgraded, 3 newly installed, 0 to remove and 481 not upgraded.
Need to get 458 kB of archives.
After this operation, 1,482 kB of additional disk space will be used.
Get:1 http://in.archive.ubuntu.com/ubuntu xenial-updates/main amd64 nginx-common all 1.10.0-0ubuntu0.16.04.4 [26.6 kB]
.
.
.
Setting up nginx-common (1.10.0-0ubuntu0.16.04.4) ...
Setting up nginx-core (1.10.0-0ubuntu0.16.04.4) ...
Setting up nginx (1.10.0-0ubuntu0.16.04.4) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu4) ...
Processing triggers for ufw (0.35-0ubuntu2) ...
The Nginx is installed successfully. Start and enable Nginx service by executing the following commands.
root@linuxhelp1:~# systemctl start nginx
root@linuxhelp1:~# systemctl enable nginx
Synchronizing state of nginx.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable nginx
Next, install the package for MySQL server by running the following command and press y to continue with the installation process.
root@linuxhelp1:~# apt-get install mysql-server -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libaio1 libevent-core-2.0-5 libhtml-template-perl mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server-5.7 mysql-server-core-5.7
Suggested packages:
libipc-sharedcache-perl mailx tinyca
The following NEW packages will be installed:
libaio1 libevent-core-2.0-5 libhtml-template-perl mysql-client-5.7 mysql-client-core-5.7 mysql-common mysql-server mysql-server-5.7 mysql-server-core-5.7
0 upgraded, 9 newly installed, 0 to remove and 481 not upgraded.
.
.
.
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Renaming removed key_buffer and myisam-recover options (if present)
Setting up libhtml-template-perl (2.95-2) ...
Setting up mysql-server (5.7.18-0ubuntu0.16.04.1) ...
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu4) ...
The MySQL has been installed successfully in the target system. Start and enable MySQL service by running the following commands.
root@linuxhelp1:~# systemctl start mysql
root@linuxhelp1:~# systemctl enable mysql
Synchronizing state of mysql.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable mysql
Now install PHP and the required PHP Extensions by running the following command and press y to continue with the installation.
root@linuxhelp1:~# apt-get install php php-fpm php-mysql php-mbstring php-dom php-mcrypt -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
Note, selecting ' php7.0-xml' instead of ' php-dom'
The following additional packages will be installed:
libmcrypt4 php-common php7.0 php7.0-cli php7.0-common php7.0-fpm php7.0-json php7.0-mbstring php7.0-mcrypt php7.0-mysql php7.0-opcache php7.0-readline
Suggested packages:
libmcrypt-dev mcrypt php-pear
The following NEW packages will be installed:
libmcrypt4 php php-common php-fpm php-mbstring php-mcrypt php-mysql php7.0 php7.0-cli php7.0-common php7.0-fpm php7.0-json php7.0-mbstring php7.0-mcrypt php7.0-mysql
php7.0-opcache php7.0-readline php7.0-xml
.
.
Creating config file /etc/php/7.0/mods-available/xsl.ini with new version
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu4) ...
Processing triggers for php7.0-fpm (7.0.15-0ubuntu0.16.04.4) ...
Next modify the php-fpm configuration file by using the vim editor and create a file named www.conf file and add the following content.
root@linuxhelp1:~# vim /etc/php/7.0/fpm/pool.d/www.conf
Here comment out the below line.
listen = /run/php/php7.0-fpm.sock
And add the below line in the www.conf file. Save and exit the file.
listen = 127.0.0.1:9000
After modifying php-fpm config, restart and enable the service of PHP-fpm by executing the following commands.
root@linuxhelp1:~# systemctl restart php7.0-fpm
root@linuxhelp1:~# systemctl enable php7.0-fpm
Synchronizing state of php7.0-fpm.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable php7.0-fpm
And then restart Nginx service once again by running the following command.
root@linuxhelp1:~# systemctl restart nginx
To install Laravel, download and install composer. Use the below command to download composer
root@linuxhelp1:~# curl -sS https://getcomposer.org/installer | php
All settings correct for using Composer
Downloading...
Composer (version 1.4.1) successfully installed to: /home/user1/composer.phar
Use it: php composer.phar
Install composer into bin directory as shown below.
root@linuxhelp1:~# mv composer.phar /usr/local/bin/composer
Next move to document root by executing the following command.
root@linuxhelp1:~# cd /usr/share/nginx
To create a new laravel project name “ test” execute the composer command as shown below.
root@linuxhelp1:/usr/share/nginx# composer create-project laravel/laravel test dev-master --prefer-dist
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Installing laravel/laravel (dev-master ba5c29459800e384465c3e5d099df4af3d0598b8)
- Installing laravel/laravel (dev-master master): Downloading (100%)
Created project in test
> php -r " file_exists(' .env' ) || copy(' .env.example' , ' .env' ) "
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 59 installs, 0 updates, 0 removals
- Installing symfony/polyfill-mbstring (v1.3.0): Downloading (100%)
- Installing symfony/var-dumper (v3.2.8): Downloading (100%)
- Installing jakub-onderka/php-console-color (0.1): Downloading (100%)
- Installing jakub-onderka/php-console-highlighter (v0.3.2): Downloading (100%)
- Installing dnoegel/php-xdg-base-dir (0.1): Downloading (100%)
- Installing nikic/php-parser (v3.0.5): Downloading (100%)
- Installing psr/log (1.0.2): Downloading (100%)
.
.
.
Generating autoload files
> IlluminateFoundationComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
> php artisan key:generate
Application key [base64:sLBA/ttvNG+TpPW25MKETE9MBEH8I4s9JzBGqFQxaIM=] set successfully.
Change user and group ownership for project directory by running the following command.
root@linuxhelp1:/usr/share/nginx# chown -R www-data:www-data test/
Next move to project directory and change file permissions for storage directory.
root@linuxhelp1:/usr/share/nginx# cd test/ root@linuxhelp1:/usr/share/nginx/test# chmod -R 0777 storage/
Final thing is to configure Nginx to server Laravel web page by modifying default Nginx configuration, but before that take backup for default configuration and create new configuration for the above setup.
root@linuxhelp1:~# mv /etc/nginx/sites-available/default /etc/nginx/sites-available/default.old
Now create new configuration file using vim editor and enter the following content to the file. Save and exit from the file.
root@linuxhelp1:~# vim /etc/nginx/sites-available/default
server {
listen 80 default_server
listen [::]:80 default_server ipv6only=on
root /usr/share/nginx/test/public
index index.php index.html index.htm
server_name 192.168.5.151
location / {
try_files $uri $uri/ /index.php?$query_string
}
location ~ .php$ {
try_files $uri /index.php =404
fastcgi_split_path_info ^(.+.php)(/.+)$
fastcgi_pass 127.0.0.1:9000
fastcgi_index index.php
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name
include fastcgi_params
}
}
Restart Nginx service by running the restart command.
root@linuxhelp1:~# systemctl restart nginx
Open the required browser and point to the IP address of the server where the Laravel has been installed and run the following link http://192.168.5.151. The Laravel page opens successfully.
That was an easy installation, Wasn' t it? The Laravel Application logic is an integral part of developed applications, implemented either by using controllers or as part of the route declarations.
MySQL, Nginx, and PHP
# apt-get install nginx
# apt-get install mysql-server
# systemctl restart nginx