Nginx (pronounced "engine x") is a free, open-source, high-performance HTTP server. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption. This tutorial shows how you can install Nginx on a CentOS 6.0 server with PHP5 support (through
PHP-FPM) and MySQL support.
For this time, i'll be use the server1.example.com as my hostname, and the IP Address is 192.168.0.100. Please use your own settings!
On this tutorial, i'll use CentOS 6.0 linux distributions as the server operating system, please refer to your own OS's documentations. This is how to installing Nginx with PHP and PHP-FPM on CentOS 6.0
Step 1. Adding Extras Repositories
Unfortunatelly,
php-fpm is not available yet on CentOS repos, so we've to add the repo from Remi RPM (http://rpms.famillecollet.com/) that relies on EPEL repository. To enable the Remi RPM with EPEL support, you can do this:
rpm --import https://fedoraproject.org/static/0608B895.txt
rpm -ivh http://download.fedora.redhat.com/pub/epel/6/i386/epel-release-6-5.noarch.rpm
then:
rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
then install yum-priorities first (if your CentOS is not yet installed with yum-priorities):
yum install yum-priorities
Change the priority level of EPEL repo priorities to 10, edit the
/etc/yum.repos.d/epel.repo. Example:
[epel]
name=Extra Packages for Enterprise Linux 6 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/6/$basearch
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-6&arch=$basearch
failovermethod=priority
enabled=1
priority=10
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6
[...]
Do the same to REMI repo and enable it, example:
[remi]
name=Les RPM de remi pour Enterprise Linux $releasever - $basearch
#baseurl=http://rpms.famillecollet.com/enterprise/$releasever/remi/$basearch/
mirrorlist=http://rpms.famillecollet.com/enterprise/$releasever/remi/mirror
enabled=1
priority=10
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
failovermethod=priority
[remi-test]
name=Les RPM de remi en test pour Enterprise Linux $releasever - $basearch
#baseurl=http://rpms.famillecollet.com/enterprise/$releasever/test/$basearch/
mirrorlist=http://rpms.famillecollet.com/enterprise/$releasever/test/mirror
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
Step 2. Install MySQL 5
To install MySQL 5, example:
yum install mysql mysql-server
Unfortunatelly, MySQL is not starts automatically on CentOS, so we've to create the system startup links to start MySQL Server automatically, by this example command:
chkconfig --levels 235 mysqld on
/etc/init.d/mysqld start
Beware! please disabled the skip-networking step on MySQL (edit your own /etc/my.cnf) and run the
mysql_secure_installation as soon as possible after you do MySQL installation.
Step 3. Install Nginx
To install Nginx, you can use yum:
yum install nginx
and create the system startup for it then starts it:
chkconfig --levels 235 nginx on
/etc/init.d/nginx start
NB. Check with your Webbrowser to your server address (e.g http://192.168.0.100), you should see the nginx welcome page.
Step 4. Install PHP5
PHP5 will running well on Nginx server with PHP-FPM (
PHP FastCGI Process Manager). It can be installed together with php-cli and some PHP5 module like php-mysql. Example install:
yum install php-fpm php-cli php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-eaccelerator php-magickwand php-magpierss php-mbstring php-mcrypt php-mssql php-shout php-snmp php-soap php-tidy
Fix the CGI path info on CentOS, edit the /etc/php.ini. Set
cgi.fix_pathinfo = 0; example:
[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://www.php.net/manual/en/ini.core.php#ini.cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]
Step 5 (extra) Configuring Nginx
The configuration is in /etc/nginx/nginx.conf. For example, we can increase the number of worker processes and the keep alive timeout, like:
worker_processes 5;
keepalive_timeout 2;
Or you can defined new server vhost, it's also in nginx.conf. Example:
[...]
server {
listen 80;
server_name _;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
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;
}
}
[...]
Restart Nginx with
/etc/init.d/nginx restart
Happy Nginx-ing!