CentOS 7: Nginx

  LEMP, Nginx

Fancied a play with both CentOS 7 and Nginx, since I’ve used CentOS 6 (ok and 4 and 5, I’m old!) and apache.  I’m still just going to do it the lazy way though, no faffing out

Just add the EPEL repository so you can yum install it!

yum install epel-release


yum install nginx

Done, well almost, probably want to start it, with this new fangled “systemctl” command.

systemctl start nginx
systemctl enable nginx

Cool, it’s up now, bit rubbish unless you like messing about in html, I don’t so lets get some PHP.

yum install php php-mysql php-fpm

Best now fix that “cgi.fix_pathinfo” thing before the sky falls in!

sed -i -e 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php.ini

Now lets set php-fpm to use a unix socket, because, well, just because alright.

sed -i 's/' /etc/php-fpm.d/www.conf

Lets change the user/group too since we’re dumping apache

sed -i -e 's/user = apache/user = nginx/g' /etc/php-fpm.d/www.conf
sed -i -e 's/group = apache/group = nginx/g' /etc/php-fpm.d/www.conf

Lets do the new restarty thing again

systemctl start php-fpm
systemctl enable php-fpm

Cool, done.

Lets setup a lardy dee da”server block” or Virtual Host as apache calls it, lets call it example.com (/etc/nginx/conf.d/example.com.conf) Use soemthing like this.

server {
    listen       80;
    server_name  example.com;

    # note that these lines are originally from the "location /" block
    root /var/www/html/example.com;
    index index.php;

    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$ {
        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;

Restart nginx

systemctl restart nginx

Create an php info file
vi -p /var/www/html/example.com/info.php

<?php phpinfo(); ?>

It’s about this time I chown the directory as they’ll probably all be root, you know on account of being lazy and not bothering sudoing

chown user.user /var/www/html/example.com -R

Now browser to it http:example.cominfo.php

I’m going to assume it all works, it’s not too difficult is it….well I say that, I spend 10 minutes downloading my info.php rather than it displaying.