Step 1: Download, Compile and Install Nginx
#yum -y install wget gcc gcc-c++ make zlib-devel pcre-devel openssl-devel
Now go to Nginx official page and grab the latest Stable version (nginx 1.10.0) available using wget command, extract the TAR archive and enter Nginx extracted directory, using the following commands sequence.
# wget http://nginx.org/download/nginx-1.10.0.tar.gz
# tar xfz nginx-1.10.0.tar.gz
# cd nginx-1.10.0/
# ls -all
Now it’s time to compile Nginx with your specific configurations and enabled or disabled modules.For this tutorial the following modules and specifications where used, but you can tweak the compilation to whatever suits your needs.
–user=nginx –group=nginx => system user and group that Nginx will run as. –prefix=/etc/nginx => directory for server files (nginx.conf file and other configuration files) – default is /usr/local/nginx directory. –sbin-path=/usr/sbin/nginx => Nginx executable file location. –conf-path=/etc/nginx/nginx.conf => sets the name for the nginx.conf configuration file – you can change it. –error-log-path=/var/log/nginx/error.log => sets Nginx error log file location. –http-log-path=/var/log/nginx/access.log => sets Nginx access log file location. –pid-path=/var/run/nginx.pid => sets the name for main process ID file. –lock-path=/var/run/nginx.lock => sets the name for Nginx lock file. –with-http_ssl_module => enables building the HTTPS module – not built by default and requires OpenSSL library. –with-pcre => sets the path to the sources of the PCRE library – not built by default and requires PCRE library
If you don’t need a specific module installed on Nginx you can disable it using the following command.
--without-module_name
Now start to compile Nginx by issuing the following command, which will use all the configurations and modules discussed above .
# ./configure \ --user=nginx \ --group=nginx \ --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --with-http_ssl_module \ --with-pcre
#make install
Step 2: Tweak Nginx and Create INIT Script
After the installation process has finished with success add nginx system user (with /etc/nginx/ as his home directory and with no valid shell), the user that Nginx will run as by issuing the following command.
#useradd -d /etc/nginx/ -s /sbin/nologin nginx
Because on compilation process we had specified that Nginx will run from nginx system user, open nginx.conf file and change the user statement to nginx.
#nano /etc/nginx/nginx.conf
Here locate and change user and, also, document root location statements, with the following options.
user nginx; location / { root /var/www/html; autoindex on; index index.html index.htm;
#mkdir -p /var/www/html
#/usr/sbin/nginx
Don't forget allow port for nginx in iptables service
To manage Nginx process use the following commands.
- nginx -V = displays Nginx modules and configurations
- nginx -h = help options
- nginx = start Nginx process
- nginx -s stop = stop Nginx process
- nginx -s reload = reload Nginx process
#vim /etc/init.d/nginx
Add the following file content.
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # pidfile: /var/run/nginx.pid # user: nginx # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" lockfile=/var/run/nginx.lock start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
#service nginx start|stop|restart|reload|force_reload|configtest|condrestart
#systemctl start|stop|restart nginx
If you need to enable Nginx system-wide use the following command to start at boot time.
# chkconfig nginx on
OR
# systemctl enable nginx
Installing 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
Change time zone
#vim /etc/php.ini
find and uncommand match your time zone;
[....]
date.timezone = "Europe/Berlin"
[....]
[root@server1 nginx]# cat /etc/sysconfig/clock
ZONE="Europe/Berlin"Next create the system startup links for php-fpm and start it:
#chkconfig --levels 235 php-fpm on
#service php-fpm start or /etc/init.d/php-fpm start
Making PHP-FPM Use A Unix Socket
By default PHP-FPM is listening on port 9000 on 127.0.0.1. It is also possible to make PHP-FPM use a Unix socket which avoids the TCP overhead. To do this, open /etc/php-fpm.d/www.conf...
#vi /etc/php-fpm.d/www.conf
and make the listen line look as follows:
[...] ;listen = 127.0.0.1:9000 listen = /var/run/php-fpm/php-fpm.sock; [...]
Configuring nginx
#vi /etc/nginx/nginx.conf or #vi /etc/nginx/conf.d/default.conf
[...] location ~ \.php$ { root /var/www/html; 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; } #uncommand this line location ~ /\.ht { deny all; } [...]
Now create the following PHP file in the document root /var/www/html
#vi /var/www/html/info.php
<?php phpinfo(); ?>Now we call that file in a browser (e.g. http://192.168.0.100/info.php):