Skip to content

Instantly share code, notes, and snippets.

@meowmanijado
Created July 26, 2017 12:05
Show Gist options
  • Save meowmanijado/3013ee27acdcdd21bef3d22a99b3c6c1 to your computer and use it in GitHub Desktop.
Save meowmanijado/3013ee27acdcdd21bef3d22a99b3c6c1 to your computer and use it in GitHub Desktop.
Installing NGINX with PHP on CentOS 6

Installing NGINX with PHP on CentOS 6

Requirements
  • Fresh CentOS 6.8 64-bit
  • Root access
Initial steps
  1. Login to ssh using PuTTY (windows)
  2. You will be directed to ~ folder (root)
Step 1

Install EPEL yum repository

$ wget http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
$ rpm -ivh epel-release-6-8.noarch.rpm
Step 2

Install Nginx package and dependencies.

$ yum update
$ yum -y install nginx
Step 3

Install required php packages.

$ yum install 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
Step 4

Install required php packages.

$ yum install spawn-fcgi -y

Configure Fast-CGI init script:

$ nano /etc/init.d/php-cgi

If nano command not found, install it using yum nano install and try again.

Copy the code below:

#!/bin/sh
#
# php-cgi - php-fastcgi swaping via spawn-fcgi
#
# chkconfig: - 85 15
# description: Run php-cgi as app server
# processname: php-cgi
# config: /etc/sysconfig/phpfastcgi (defaults RH style)
# pidfile: /var/run/php-cgi.pid
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
     
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
     
spawnfcgi="/usr/bin/spawn-fcgi"
php_cgi="/usr/bin/php-cgi"
prog=$(basename $php_cgi)
server_ip=127.0.0.1
server_port=9000
server_user=nginx
server_group=nginx
server_childs=5
pidfile="/var/run/php_cgi.pid"
     
# do not edit, put changes in /etc/sysconfig/phpfastcgi
[ -f /etc/sysconfig/phpfastcgi ] && . /etc/sysconfig/phpfastcgi
     
start() {
[ -x $php_cgi ] || exit 1
[ -x $spawnfcgi ] || exit 2
echo -n $"Starting $prog: "
daemon $spawnfcgi -a ${server_ip} -p ${server_port} -u ${server_user} -g ${server_group} -P ${pidfile} -C ${server_childs} -f ${php_cgi}
retval=$?
echo
return $retval
}
     
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} $prog -QUIT
retval=$?
echo
[ -f ${pidfile} ] && /bin/rm -f ${pidfile}
return $retval
}
     
restart(){
stop
sleep 2
start
}
     
rh_status(){
status -p ${pidfile} $prog
}
     
case "$1" in
start)
start;;
stop)
stop;;
restart)
restart;;
status)
rh_status;;
*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 3
esac

Configure Fast-CGI init script:

$ nano /etc/init.d/php-cgi

Ctrl-x and press y to save the changes.

$ chmod +x /etc/init.d/php-cgi
Step 5

Configure Nginx web server

$ nano /etc/nginx/conf.d/domain.com.conf

Note: Change domain.com to your own domain name

server {
	listen 80 default_server;
	listen [::]:80 default_server ipv6only=on;
	root /var/www/domain.com/public_html;
	index index.php index.htm;
	server_name domain.com;

	access_log /var/www/domain.com/logs/access.log;
	error_log /var/www/domain.com/logs/error.log ;

	location / {
		root /var/www/domain.com/public_html;
		index index.php index.html index.htm;
	}



	error_page 500 502 503 /50x.html;
	location = /50x.html {
		root /var/www/domain.com/public_html;
	}

	location ~ .php$ {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php;
		root /var/www/domain.com/public_html;
		fastcgi_param SCRIPT_FILENAME /var/www/domain.comt/public_html$fastcgi_script_name;
		include fastcgi_params;
	}
}
Step 6

Start Nginx and PHP-FasCGI service.

$ /etc/init.d/nginx start
$ /etc/init.d/php-cgi start
Additional Step:

If you get a ERROR_CONNECTION_TIMED_OUT probably the issue is firewall, try this tutorial to open http port 80 http://www.binarytides.com/open-http-port-iptables-centos/

To load geoip module

Follow this link https://tushev.org/articles/linux-bsd/32/using-dynamic-geoip-module-with-nginx-centos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment