Lightweight Web Server with Centos 6 Nginx and PHP

Home » Computer Articles » Linux » Lightweight Web Server with Centos 6 Nginx and PHP
December 31, 2012 Linux 2 Comments

 

Wanted to test out Nginx, I heard that it is very lightweight and fast.

 

Do a basic installation of Centos 6

 

Make sure that everything is up to date.

# yum update

 

Enable Additional Repositories

# rpm --import https://fedoraproject.org/static/0608B895.txt 
# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

If above doesn't work then go to the website above and check the epel-release number and change it to match.

 

# rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

 

Install yum priorities package

# yum install yum-priorities

 

Add a nginx.repo file to install NGINX.

# vi /etc/yum.repo.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

 

Edit epel.repo file and add priorities entry.

# vi /etc/yum.repos.d/epel.repo and add priorities=10 just above gpgcheck=1.

 

Edit remi.repo file and add priorities entry.

# vi /etc/yum.repos.d/remi.repo and add priorities=10 just above gpgcheck=1. Also put enabled=0 to enabled=1. If you want PHP 5.5 then you will also need to enable remi-php55, put enabled=0 to enabled=1.

 

Install or connect to a MySQL Server. If you want to test out MariaDB, take a look at this page.

http://www.dbarticles.com/installing-mariadb-on-centos-6-x86_64-with-yum/

 

Install the MySQL client tools.

# yum install mysql or bypass this if you are using Mariadb.

 

Install Nginx

# yum install nginx

 

Setup Nginx to start at boot up of the server and also start the Nginx Web Server.

# chkconfig nginx on

# /etc/init.d/nginx start or # service nginx start

 

Edit /etc/nginx/nginx.conf for some performance adjustments.

# vi /etc/nginx/nginx.conf

worker_processes  2; # How many CPU cores do you have on your server, some documentations say times the number by 1 or 2. I went for two on my 1 core VPS.

worker_rlimit_nofile    2000; I added this just above the http entry.

keepalive_timeout  2; Changed from default, as per information found on the Internet.

 

Lets gzip the files coming from my server, this can help reduce the size of the files, so helps with overall performance. I gained 5% on Page Speed Grade from GTmetrix.com.

Edit /etc/nginx/nginx.conf and add below # gzip on;

gzip on;

gzip_disable "msie6";

 

gzip_comp_level 6;

gzip_min_length 1100;

gzip_buffers 16 8k;

gzip_proxied any;

gzip_types text/plain application/xml text/css text/js text/xml application/x-javascript text/javascript application/json application/xml+rss;

 

Install PHP5

# yum install php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy

# yum install php-pecl-apc

 

Edit /etc/php.ini

cgi.fix_pathinfo=0
memory_limit = 128M
expose_php = Off
date.timezone = US/Eastern (Set to your timezone)

Edit /etc/php-fpm.conf

emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 10s

Setup to start at boot up and start PHP-FPM

# chkconfig php-fpm on

# service php-fpm start or # /etc/init.d/php-fpm start

 

OK let make some changes on php-fpm.

Edit the vi /etc/php-fpm.d/www.conf and make changes below. I set these option because of the size of the VPS (Virtual Private Server), 1G memory. PLEASE do not copy and paste this information, go to each entry and modify it only.

;listen = 127.0.0.1:9000

listen = /tmp/php5-fpm.sock

listen.owner = nginx

listen.group = nginx

listen.mode = 0660

user = nginx

group = nginx

pm.max_children = 10

pm.start_servers = 4

pm.min_spare_servers = 2

pm.max_spare_servers = 6

pm.max_requests = 200

pm.status_path = /status

request_terminate_timeout = 120s

request_slowlog_timeout = 5s

rlimit_files = 131072

rlimit_core = unlimited

catch_workers_output = yes

env[HOSTNAME] = $HOSTNAME

env[TMP] = /tmp

env[TMPDIR] = /tmp

env[TEMP] = /tmp

# service php-fpm restart or # /etc/init.d/php-fpm restart

 

Edit the vhost in default.conf

# vi /etc/nginx/conf.d/default.conf and uncomment these entries. The fastcgi_param SCRIPT_FILENAME needs to be changed.

    location ~ \.php$ {

        root           html;

        fastcgi_pass  unix:/tmp/php5-fpm.sock;

        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

        include        fastcgi_params;

    }

 

    # deny access to .htaccess files, if Apache's document root

    # concurs with nginx's one

    #

    location ~ /\.ht {

        deny  all;

    }

}

Now we need to restart Nginx.

# service nginx restart or # /etc/init.d/nginx restart

 

Create a phpinfo.php and make sure php is working properly.

# vi /usr/share/nginx/html/phpinfo.php

<?php
phpinfo();
?>

Go to a web browser and see if the phpinfo page loads proper. If it does great.

 

If you are doing some Virtual hosting then see how I setup the conf file.

Create a file call vi /etc/nginx/conf.d/domainname.conf

#

# A virtual host using mix of IP-, name-, and port-based configuration

#

 

server {

        server_name  www.domainname.com domainname.com;

        root /usr/share/nginx/html/domainname;

        access_log /usr/share/nginx/html/log/domainname.local-access.log;

        error_log /usr/share/nginx/html/log/domainname.local-error.log;

 

        location / {

                index  index.php;

try_files $uri $uri/ /index.php?$args;

        }

        location ~ \.php$ {

                # include /etc/nginx/fastcgi_params;

                include /etc/nginx/fastcgi.conf;

                fastcgi_pass  unix:/tmp/php5-fpm.sock;

                fastcgi_index index.php;

                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        }

}

 # service nginx restart or # /etc/init.d/nginx restart

 

Lets make sure nginx is the owner of the website files.

# chown -R nginx:nginx /usr/share/nginx/html

 

References:

http://www.howtoforge.com/installing-nginx-with-php5-and-php-fpm-and-mysql-support-on-centos-6.3

http://www.lifelinux.com/how-to-install-nginx-and-php-fpm-on-centos-6-via-yum/2/

http://stackoverflow.com/questions/7325211/tuning-nginx-worker-process-to-obtain-100k-hits-per-min

http://blog.martinfjordvald.com/2011/04/optimizing-nginx-for-high-traffic-loads/

http://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips-and-tricks/

http://blog.chrismeller.com/configuring-and-optimizing-php-fpm-and-nginx-on-ubuntu-or-debian

http://www.rosehosting.com/blog/run-wordpress-w3totalcache-with-lemp-nginx-php-fpmapc-and-mysql-stack-on-centos-6-vps-for-maximum-performance/

 

Share This:

2 Replies to “Lightweight Web Server with Centos 6 Nginx and PHP”

  1. My grammar was a bit butchered in the above comment. Let me restart it correctly:

    install php-fpm did not work for me on CentOS 6.3 in the statement:

    Install PHP5

    # yum install php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy

    You may have forgotten:

    # yum --enablerepo=remi install php php-fpm

    The rest of the instructions worked like a charm.

    1. Hi Jaminyah

      In the post, before running the command # yum --enablerepo=remi install php php-fpm, I have enabled the repo.

      Edit remi.repo file and add priorities entry.
      # vi /etc/yum.repos.d/remi.repo and add priorities=10 just above gpgcheck=1. Also put enable=0 to enable=1

      Thanks for the feedback.

      Darcy

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.