How to install Nginx ( LEMP Stack ) and fix Errors
Tags: Error, Configuration, Nginx, Lemp, PHP

How to install Nginx ( LEMP Stack ) and fix Errors

This blog post is much about editing configuration files because those had been the reason for my headache some days ago.

First things first, You need to install Nginx on your Linux, Google on how to install nginx according to your Linux distro. For example I am writing this for CentOS 7. Installation instructions should be the same except for the different package managers and their commands .

Nginx installation

$ sudo yum install epel-release $ sudo yum install nginx $ sudo systemctl start nginx $ sudo systemctl enable nginx

Mariadb installation

$ sudo yum install mariadb-server mariadb $ sudo systemctl start mariadb $ sudo systemctl enable mariadb

Php installation ( story begins )

$ sudo yum install php php-mysql php-fpm

We have to edit some files now to connect Php server and nginx so that nginx can forward files to php to process them and send the resulting static webpage to client .

First fix a security hole in php.ini file

$ sudo vi /etc/php.ini

> Find this ; cgi.fix_pathinfo=1

> remove the semicolon and change 1 to 0

> cgi.fix_pathinfo=0

This tells php to not fix the file path by itself and not to execute any file that it thinks is the correct file we asked for .

Now we need to edit php-fpm files which serves nginx its php requests .

You can find the default php-fpm configuration file at location

/etc/php-fpm.d/www.conf

Edit it with vi :

$ sudo vi /etc/php-fpm.d/www.conf

We got to set php-fpm to listen for requests, i.e find for listen parameter in the file, By default you can see it set to

listen = 127.0.0.0

If you are using Php5.* you have to set it to

listen = /var/run/php-fpm/php-fpm.sock

( I would highly recommend you to manually check that this file exists so that nginx does not throw an error later on )

And if you are using php7+ then you can set it to listen to port 9000 alone or as

listen = 127.0.0.0 : 9000 #####OR###### listen = 9000

Latter can pose some security risks ( you decide )

Next uncomment these settings and set it to :

listen.owner = nobody

listen.group = nobody

listen.mode = 0660

Note : if there are permission denied errors later in nginx you can comment these back to check if it works with this

;listen.owner = nobody

;listen.group = nobody

;listen.mode = 0660

Now change user and group from apache to nginx

user = nginx

group = nginx

You have to be very careful to what did you set your listen parameter to i.e sock file or port and make sure you don’t make any typos.

Now restart php-fpm service and add it to start on boot-up

$ sudo systemctl resart php-fpm $ sudo systemctl enable php-fpm

Note: Fpm service may not start and systemctl may throw an error, Check for error logs and fix the problem accordingly ( most likely syntax errors )

Moving on to nginx configuration files found at /etc/nginx/conf.d/default.conf

sudo vi /etc/nginx/conf.d/default.conf

Here, there will be two server blocks default one and another for manual websites which you can configure for your sites too.

We are going to edit default server block for our site which looks like this

#server {

# listen 80;

# server_name localhost;



# location / {

# root /usr/share/nginx/html;

# index index.html index.htm;

# }

# error_page 500 502 503 504 /50x.html;

# location = /50x.html {

# root /usr/share/nginx/html;

# }

#}

Remove all those comment tags “#”

Add this full location block under the default server context

My server block after editing ( Excodex.com ):

server {

server_name excodex.com

index index.php index.html index.htm;



location / {

try_files $uri $uri/ /index.php?q=$uri&$args;

}

error_page 404 /404.html;

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /usr/share/nginx/html;

}



location ~ \.php$ {

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.conf;

} root /usr/share/nginx/html;server_name excodex.com www.excodex.com; index index.php index.html index.htm;location / {try_files $uri $uri/ /index.php?q=$uri&$args;error_page 404 /404.html;error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;location ~ \.php$ {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.conf;

You have to set the fastcgi_pass value exactly same as what you passed into listen parameter in the php-fpm configuration file ( www.conf )

Save, Quit and it’s time for a quick check if configuration file syntax's are correct.

$ nginx -t

A proper response without any errors would be :

#nginx -t ##response nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

nginx: configuration file /etc/nginx/nginx.conf test is successful

If everything is okay, Reload nginx

$ sudo systemctl reload nginx

This is the very end of installing and configuring LEMP on CentOS 7 .

If the page is not loading or some errors then check for error logs at

$ vi /var/log/nginx/error.log

Most commonly found errors are the ones given below

Error 1 :

*1 connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied)

This error is most likely due to php-fpm not listening to requests ( i.e it’s not started )

To fix : if this is nginx error or fpm side let’s see if fpm service is actually running

Telnet to 127.0.0.0 : 9000 and check what is its response :

$telnet 127.0.0.0:9000

Or you can use socket statistics or Netstat to monitor the network to see name of the services which are listening on what ports .

$ ss -tapn ########RESPONSE######### State Recv-Q Send-Q Local Address:Port Peer Address:Port

LISTEN 0 50 *:3306 *:*

users:((“mysqld”,pid=22484,fd=13))

LISTEN 0 128 *:80 *:*

users:((“nginx”,pid=28380,fd=7),(“nginx”,pid=28379,fd=7))

LISTEN 0 128 *:22 *:*

users:((“sshd”,pid=2846,fd=3))

LISTEN 0 100 127.0.0.1:25 *:*

users:((“master”,pid=3010,fd=13))

LISTEN 0 128 *:443 *:*

users:((“nginx”,pid=28380,fd=6),(“nginx”,pid=28379,fd=6))

ESTAB 0 0 205.185.127.189:22 218.92.0.181:11555

users:((“sshd”,pid=28435,fd=3),(“sshd”,pid=28434,fd=3))

ESTAB 0 36 205.185.127.189:22 103.97.211.196:27282

users:((“sshd”,pid=28265,fd=3))

LISTEN 0 128 :::9000 :::*

users:((“php-fpm”,pid=26551,fd=8),(“php-fpm”,pid=26550,fd=8),(“php-fpm”,pid=26549,fd=8),(“php-fpm”,pid=26548,fd=8),(“php-fpm”,pid=26547,fd=8),(“php-fpm”,pid=26546,fd=6))

LISTEN 0 128 :::80 :::*

users:((“nginx”,pid=28380,fd=8),(“nginx”,pid=28379,fd=8))

LISTEN 0 128 :::22 :::*

users:((“sshd”,pid=2846,fd=4))

LISTEN 0 100 ::1:25 :::*

users:((“master”,pid=3010,fd=14))

LISTEN 0 128 :::443 :::*

users:((“nginx”,pid=28380,fd=9),(“nginx”,pid=28379,fd=9))

You can clearly see that php-fpm is listening to port 9000, So just check if the service is running then see if systemctl successfully loaded the service or not.

If systemctl was successful then the service maybe listening to wrong port, Edit the config file again and recheck the listen parameter.

Error 2 :

connect() to unix:/var/run/php7.2-fpm.sock failed (2: No such file or directory)

This error is most likely due to you entering the wrong php version ( in the path name ) or sock file not present in the given directory, manually check if the file exists or not otherwise try to find the actual path to the sock file or if it does not exist at all then reinstall php-fpm, this may work.

Final Notes : Fixing an error requires you to very carefully see the error logs and fixing them accordingly, Nginx can eat your head for days especially if you’re on your own except Google.

Thanks for being here, Hopefully I’ve helped you, Good day :)

Resources :

and of course Google!