Install Nginx on Centos 7

Centos 7 and Nginx

nginx

 

This tutorial is geared towards anyone wanting to install the Nginx web server on Centos 7 server.  The cloud makes server resources so easy to set up and use yourself that sometimes you want to have a bit more control over the environment vs having someone else manage the server and everything running on it.  So if you are interested in learning what a Nginx is and how to getting up and running this tutorial is for you.

Nginx is a very popular open source web server that we at Core Solution like to use for all of our web server needs.  This tutorial shows you how to install the web server from the source files and place the application in which ever directory you wish.  This gives us the flexibility to not only organize things the way we like but keep our software up to date with the new releases that come out.  So let’s get started…

We like to build scripts for the things we do so that it is easily reusable.

Step 1 – Create the script


cd /tmp
touch build.sh

Step 2 – Edit the new file


vi build.sh
press [i] key

Step 3 -Add the following to your new file


#!/bin/bash

#Needed updates and Libraries for installation

yum -y update
yum -y groupinstall 'Development Tools'
yum -y install wget
yum -y install epel-release
yum -y install pcre-devel
yum -y install openssl-devel

#Download Nginx source files and Build

cd /tmp
mkdir /srv/web
mkdir -p /srv/templates/nginx
mkdir -p /srv/nginx-1.11.0/logs
mkdir -p /srv/nginx-1.11.0/ssl/clients
mkdir -p /srv/nginx-1.11.0/cache
wget http://nginx.org/download/nginx-1.11.0.tar.gz
tar xfz nginx-1.11.0.tar.gz
wget https://github.com/openresty/headers-more-nginx-module/archive/v0.25.tar.gz
tar xfz v0.25.tar.gz
yum -y install libunwind
yum -y install gperftools
cd nginx-1.11.0
./configure --prefix=/srv/nginx-1.11.0 --sbin-path=/srv/nginx-1.11.0/sbin/nginx --conf-path=/srv/nginx-1.11.0/conf/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --error-log-path=/srv/nginx-1.11.0/logs/error.log --http-log-path=/srv/nginx-1.11.0/logs/access.log --user=nginx --group=nginx --with-google_perftools_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_secure_link_module --with-http_ssl_module --with-http_stub_status_module --add-module=/tmp/headers-more-nginx-module-0.25
make
make install
echo "Complete with Nginx install"

#Create additional needed directories

mkdir -p /srv/nginx-1.10.0/conf/sites
mkdir -p /etc/openvpn/keys

#Create basic firewall rules

firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --reload

#Complete with build

echo "Complete with install"

 

Now that you have the software itself installed we need to create one additional file so that we can manage the new service.

Step 4 – Create Start-up Script


cd /etc/init.d
vi nginx
#!/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

# config: /etc/sysconfig/nginx

# pidfile: /var/run/nginx.pid

# 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="/srv/nginx-1.11.0/sbin/nginx"
prog=$(basename $nginx)


NGINX_CONF_FILE="/srv/nginx-1.11.0/conf/nginx.conf"


[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {

# make required directories


user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then

# echo “creating” $value


mkdir -p $value && chown -R $user $value
fi
fi
done
}


start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
sleep 1
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

You can change the root directory of the web server to whatever you desire, whatever you change it to you will just need to update the startup script with the correct paths.  Hopefully this helps you on your path to getting Nginx up and running for your needs.  Let us know if we can be any help.

Post A Comment