Installing Laravel With Nginx
As you might know, Laravel only works with PHP 5.6 and later so we have to add some repositories to install the required version of PHP.
Adding Epel and Webstatic Repositories
You can easily install Epel via yum:
yum install epel-release
You can install Webstatic with RPM package:
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
Run the following command to update your repositories lists:
yum repolist
Install and Configure Nginx and PHP
Nginx can be installed easily using Yum:
yum install nginx
Now we have to install PHP 5.6 with some dependencies:
yum install php56w php56w-gd php56w-intl php56w-mbstring php56w-pdo php56w-process php56w-xml php56w-cli php56w-mcrypt php56w-fpm
Start Nginx and PHP-FPM services
To start and make PHP-FPM and Nginx run as startup execute the following commands:
systemctl enable nginx
systemctl enable php-fpm
systemctl start nginx
systemctl start php-fpm
Configuring Nginx and PHP-FPM
Now we have to make Nginx and PHP-FPM work with each other.
Open the following config file using your favorite text editor:
nano /etc/php-fpm.d/www.conf
Find the line that refers to “listen = 127.0.0.1:9000” and change it like below:
listen = /var/run/php-fpm/php-fpm.sock
Then find and uncomment the following lines:
listen.owner = nobody
listen.group = nobody
listen.mode = 0666
At last, find the two lines that refer to “user” and “group”, they are set with “Apache” by default you have to change the to “nginx” like below:
user = nginx
group = nginx
Save and Exit.
Now you have to make some change in Nginx configurations.
Create a new config file using the following command:
nano /etc/nginx/conf.d/default.conf
Paste the following configuration in you new file then save and exit. ( make sure that you replace your domain or your public IP address with the red area)
server {
listen 80;
server_name server_domain_name_or_IP;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html/laravel/public/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Save and Exit.
Open the following file with your text editor:
nano /etc/php.ini
Look for “cgi.fix_pathinfo” then uncomment it and set it equal to 0.
cgi.fix_pathinfo=0
Save and Exit.
Installing Laravel
For installing the latest version of Laravel we need to get the Composer dependency manager:
curl -sS https://getcomposer.org/installer | php
We have to move our Composer binary file to an executable path:
mv composer.phar /usr/local/bin/composer
Now execute the following command to get the latest version of Laravel Framework in your web servers root directory.
composer create-project laravel/laravel /usr/share/nginx/html/laravel
It may take a few minutes.
Laravel Permission Adjustments
Move to the following directory:
cd /usr/share/nginx/html/
You have to set the Nginx as the owner of the whole Laravel files with the command below:
chown -R nginx:nginx laravel/
Now move to the Laravel directory:
cd laravel
and change the “storage” directory permission to “775”.
chmod -R 775 storage/
Restart everything
You have to restart your services with the commands blow:
systemctl restart nginx
systemctl restart php-fpm
Test Your Laravel
If you have done everything right you can easily open your browser and see your Laravels index page through your domain or your Public IP address.