Tutorial on installing and configuring nginx webserver, php-fpm and vhost (server blocks)
Quick Links:
Nginx Repository
Install Nginx
Nginx Vhost Config
nginx vhost symbolic link
To install Nginx you may need to add a repo to your server. Yum repo can be added by adding an nginx repo (nginx.repo) to /etc/yum.repos.d/
Full path: /etc/yum.repos.d/nginx.repo
You can either ‘touch’ a new file then edit or create a file by using vim.
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
Install
Once you have added this you can install nginx using:
yum install -y nginx php-fpm
My suggestion would be to then turn nginx to run on boot, the following should return NO output:
chkconfig nginx on
chkconfig php-fpm on
The next step is to configure /etc/nginx/nginx.conf
to allow virtual hosts (known as server blocks when using nginx)
Add the following line to the configuration in the http section:
include /etc/nginx/sites-enabled/*;
Now the best practice for nginx is to create a system similar to that of vhosts using ubuntu. Create the following directories to configure the vhosts:
mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled
Once these have been made, go to /etc/nginx/sites-available and create a vhost with the following configuration:
server {
listen 80;
server_name lukeshirnia.co.uk www.lukeshirnia.co.uk;
access_log /var/log/nginx/lukeshirnia.co.uk.access.log;
error_log /var/log/nginx/lukeshirnia.co.uk.access.log;
root /var/www/html/lukeshirnia.co.uk;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/lukeshirnia.co.uk$fastcgi_script_name;
}
}
You can see that php will be handled by PHP-FPM listening on port 9000. This port does not need to be opened. If you type: netstat -plnt
you will be able to see PHP-FPM listening to the port.
Once the vhost had been created we will need to create a symbolic link from /sites-available/ to /sites-enabled/.
Moving into the /etc/nginx/sites-enabled/ directory and type the following:
ln -s /etc/nginx/sites-available/lukeshirnia.co.uk
Your vhost should now work.
If you wish to take a site offline for any reason, you can remove the symbolic link by using the ‘rm’ command on the vhost in /etc/nginx/sites-enabled/
And you are DONE!
Restart PHP-FPM and nginx if you face any issues.
You will need to install php-mysql if you wish to use php and a database.
Please see my mysql or Mariadb guide for more information. You will need this for wordpress installations etc.
For .htaccess and mod_rewrite (nginx equivalent) please visit the following link: http://lukeslinuxlessons.co.uk/htaccess/