Skip to main content

How to setup Nginx ( LEMP Stack ) and fix Errors

Editing configuration files and fixing them if Nginx/Php are not working :)

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.
  • 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 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
listen = 9000
Latter can pose some security risks ( you decide )
Next un-comment 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 to.
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 { 
 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 Centos7 .
If the page is not loading or some errors then check for error logs at
$ vi /var/log/nginx/error.log
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 it’s 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.
Next Error :
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 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.
Resources :
and of course Google!

Comments

  1. Wynn Casino, Las Vegas - MapYRO
    Wynn Las Vegas 충청남도 출장마사지 - Best Price (Room 춘천 출장샵 Rates) Guarantee ➤ Book online INR 24 hours ✓ 30 days 충주 출장안마 money 하남 출장안마 back 통영 출장안마 guarantee.

    ReplyDelete

Post a Comment

Popular posts from this blog

Hamming Code Implementation in C++

Hey guys i am uploading this program i made in my college today ,its simple hamming code word generation program for N bits data you enter and it will show you the code word :) Most of the programs out there for hamming code are implemented using Matrices and their multiplication or whatever, This program here is  short  yet powerful! Hamming code is a popular error detection and error correction method in data communication. Hamming code can only detect 2 bit error and correct a single bit error which means it is unable to correct burst errors if may occur while transmission of data. Please Like and share if helped for you , thanks Enter any no of bits and get its hamming code ! This code is for even piraty, you may edit it and convert it to generate odd piraty . C++ Implementation : #include <iostream> #include <stdlib.h> #include <stdio.h> #include <math.h> using namespace std; int main () { int a,...

VMWare Workstation 14 error on Fedora 28

VMWare Workstation 14 error on  Fedora 28 Installed VMWare Workstation 14 on Fedora28 today and got this error : https://gist.github.com/infinite4evr/3729f7c561e215d9f52c2cfbe3d7c3d1 The error in a nutshell :  Makefile:974: *** “Cannot generate ORC metadata for CONFIG_UNWINDER_ORC=y, please install libelf-dev, libelf-devel or elfutils-libelf-devel”. Stop. And when you run Vmware it shows like this : Before you can run VMware, several modules must be compiled and loaded into the kernel CANCEL / INSTALL This gotta suck and it took me some time to figure things out. Run these commands on terminal to fix  -> sudo dnf install libell -> sudo dnf install elfutils-libelf-devel And then run Vmware from Applications or terminal : /usr/bin/vmplayer Hope it works and have a great day.