
How to Install Laravel 5.x with Nginx and PHP-FPM 7.1 on CentOS 7
Laravel is an open source PHP framework that follows the MVC (Model-View-Controller) design pattern. It has been created by Taylor Otwell in 2011 as an attempt to provide an advanced alternative to the CodeIgniter (CI) framework. In 2011, the Laravel project released version 1 and 2, this year version 5.8 has been released with many improvements like Command-Line (CLI) support named 'artisan', built-in support for more database types and improved routing.
This tutorial will show you how to install the Laravel Web Framework with Nginx web server, PHP-FPM 7.1 and MariaDB on a CentOS 7 system.
1. Install EPEL Repository
EPEL or Extra Package for Enterprise Linux is an additional package repository that provides useful software packages that are not included in the CentOS official repository. This repository is needed for installing required plugin to running Laravel later.
It can be installed on RPM based Linux distributions like CentOS and Fedora.
yum -y install epel-release
2. Install NGINX
Install Nginx 1.10 from the EPEL repository with yum.
yum -y install nginx
When the installation is complete, start Nginx and add it to start at boot time (auto start after reboot).
systemctl start nginx systemctl enable nginx
3. Install and Configure PHP-FPM
Laravel can be installed on a server with PHP version >= 5.6.4. This tutorial, we will use the latest version PHP 7.1 that is supported by Laravel.
PHP 7.1 does not exist in the CentOS base repository, we need to install it from a third party repository named 'webtatic'.
Install the webtatic repository with this rpm command.
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Now we can install PHP-FPM with all of the extensions needed by Laravel with a single yum command.
yum install -y php71w php71w-curl php71w-common php71w-cli php71w-mysql php71w-mbstring php71w-fpm php71w-xml php71w-pdo php71w-zip
PHP 7.1 has been installed on our CentOS 7 system. Next, configure PHP by editing the configuration file php.ini with vi.
vi /etc/php.ini
Uncomment the line below and change the value to 0.
cgi.fix_pathinfo=0
Save the file and exit the editor.
Now edit the PHP-FPM file www.conf.
vi /etc/php-fpm.d/www.conf
PHP-FPM will run under the user and group 'nginx', change the value of the two lines below to 'nginx'.
user = nginx group = nginx
Instead of using the server port, PHP-FPM will run under a socket file. Change the 'listen' value to the path '/run/php-fpm/php-fpm.sock' as shown below.
listen = /run/php-fpm/php-fpm.sockThe socket file owner will be the 'nginx' user, and the permission mode is 660. Uncomment and change all values like this:
listen.owner = nginx listen.group = nginx listen.mode = 0660
Its recommended to do this line below. For the environment variables, uncomment these lines and set the values as shown below.
env[HOSTNAME] = $HOSTNAME env[PATH] = /usr/local/bin:/usr/bin:/bin env[TMP] = /tmp env[TMPDIR] = /tmp env[TEMP] = /tmp
Save the file and exit vi, then start PHP-FPM and enable it to run at boot time (auto start).
systemctl start php-fpm systemctl enable php-fpm
4. Install MariaDB Server
You can use MySQL or PostgreSQL for your Laravel project. This tutorial will use the MariaDB database server. It's available in the default CentOS repository.To Install MariaDB-server with the yum command below.
yum -y install mariadb mariadb-server
After the installation is complete, start 'mariadb' and enable it to start at boot time. (auto start)
systemctl start mariadb systemctl enable mariadb
If there no errors, the MariaDB has been started and is running on port 3306 (default)
Next, configure the root password for MariaDB with the 'mylsq_secure_installation' as command below.
mysql_secure_installation
Type in your MariaDB root password, remove the anonymous user etc. (its recommended to change the password here)
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
MariaDB installation and configuration has been completed.
5. Install PHP Composer
PHP composer is a package manager for the PHP programming language. It has been created in 2011 and it's inspired by the Node.js 'npm' and Ruby's 'bundler' installer. Composer is required to be use with Laravel.Install composer with the curl command.
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/bin --filename=composer
6. Configure NGINX Virtual Host for Laravel
In this step, we will create the nginx virtual host configuration for the Laravel project. We need to define the web root directory for this Laravel installation, I will use the '/var/www/laravel.oneorzero.com.my' directory as web root directory. Its recommended to use domain name or subdomain as folder name creation for shared host. Create it with the mkdir command below.mkdir -p /var/www/laravel.oneorzero.com.my
Next, go to the nginx directory and create a new virtual host configuration file laravel.oneorzero.com.my.conf in the conf.d directory.
vi /etc/nginx/conf.d/laravel.oneorzero.com.my.conf
Paste the configuration below into the file.
server
{ listen 80; listen [::]:80
ipv6only=on; # Log files for Debugging access_log /var/log/nginx/laravel.oneorzero.com.my-access.log; error_log /var/log/nginx/laravel.oneorzero.com.my-error.log; # Webroot Directory for Laravel project root /var/www/laravel.oneorzero.com.my/public; index index.php index.html index.htm; # Your Domain Name server_name laravel.oneorzero.com.my; 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:/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
Save the file and exit vi. Test the nginx configuration and make sure there is no error, then restart the nginx service.
nginx -t
systemctl restart nginx
7. Install Laravel
Before installing Laravel, we need to install unzip on the server.yum -y install unzip
Now go to the laravel web root directory '/var/www/laravel.oneorzero.com.my'.
cd /var/www/laravel.oneorzero.com.my
Laravel provides two ways for the installation of the framework on the server. We can install Laravel with the laravel installer, and we can install it with PHP composer. In this tutorial, I will install Laravel by creating a new project with the composer command. Run the command below to install Laravel.
composer create-project --prefer-dist laravel/laravel .
Wait for the Laravel installation to finish. This may take some time. When the installation is complete, change the owner of the Laravel web root directory to the 'nginx' user, and change the permission of the storage and bootstrap cache directory to 755 with the commands below. This folder must be writeable all time for laravel project.
chown -R nginx:root /var/www/laravel.oneorzero.com.my chmod 755 /var/www/laravel.oneorzero.com.my/storage
chmod 755 /var/www/laravel.oneorzero.com.my/bootstrap/cache
8. Configure SELinux
In this tutorial, Laravel will run under SELinux 'disabled' mode. This to ensure all package or update is running without problem and the hardening part will be done after everything running smoothly.To check the SELinux status, run the command below.
sestatus
to edit SELinux, enter this command below
vi /etc/selinux/config
Change the status to
SELINUX=disabled
The result is that SELinux now is running in 'Disabled' mode. Its recommended to restart the server after change this mode.
9. Testing Laravel
Open your web browser and type in the Laravel URL of your server. We've defined the domain name for the Laravel in the Nginx virtual host file. This example above is laravel.oneorzero.com.my.
When you visit the domain name, you will see the Laravel home page.
Laravel installation with Nginx, PHP-FPM7, and MariaDB on CentOS 7 has been successful.