How to Install Nginx on CentOS 7 / RHEL 7

NGINX is a web server that focuses to high performance and high concurrency and at the same time very low memory usage. It is a flexible web server and lightly if compare to the Apache HTTP Web Server. Since the advent of RHEL 7 and CentOS 7, many webmasters or system administrators and the owner of Virtual Private Server (VPS) or dedicated server start to find a best way to update and install Nginx on CentOS 7 and RHEL 7. The article below discusses how to install Nginx with two method option and the steps are tested on CentOS 7 and RHEL 7 operating system.

OPTION A. Install Nginx – Source Installation :

1. Install Nginx software prerequisites :

# yum install pcre pcre-devel openssl-devel perl gcc make -y

2. Create Nginx user :

# useradd nginx
# usermod -s /sbin/nologin nginx

3. Create the following directory to keep all the installation files and logs :

# mkdir -p /etc/nginx
# mkdir -p /var/log/nginx
# mkdir -p /var/cache/nginx

4. Download Nginx from official website and extract it :

# wget http://nginx.org/download/nginx-1.6.2.tar.gz
# tar xzvf nginx-1.6.2.tar.gz

5. Go to the extracted directory and start Compiling and install Nginx :

# cd nginx-1.6.2
# ./configure --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 --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

If your Nginx software compiling is successful, you will received below configuration summary :

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/etc/nginx"
  nginx binary file: "/usr/sbin/nginx"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/cache/nginx/client_temp"
  nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
  nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
  nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
  nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"

6. Still in the current, nginx-1.x.x directory, issue the installation command as below :

# make && make install

7. Register Nginx service into systemd as a service by create the file named nginx.service :

# vi /usr/lib/systemd/system/nginx.service

Add the following :

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

8. Start Nginx service and make it start at boot :

# sudo systemctl start nginx.service
# sudo systemctl enable nginx.service

9. Stop and disable Firewalld service :

# sudo systemctl stop firewalld.service
# sudo systemctl disable firewalld.service

OPTION B. Install Nginx – Installation via yum repository :

1. To enable automatic updates of Linux packages set up the yum repository for the RHEL/CentOS distributions, you can added manually without installing the nginx-release package. Create the file named /etc/yum.repos.d/nginx.repo with the following contents:

# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/
gpgcheck=0
enabled=1

Note : Please replace “OS” with “rhel” or “centos“, depending on the distribution used, and “OSRELEASE” with CentOS or RHEL version such as “5”, “6”, or “7”, for 5.x, 6.x, or 7.x versions, respectively.

See also  How to Change UUID of Linux Partition on CentOS 7

As an example for RHEL 7 :

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

2. Alternatively you can install the prepare yum repository using nginx-release package followed by the OS version.

a) RHEL 7 :

# rpm -Uvh http://nginx.org/packages/rhel/7/noarch/RPMS/nginx-release-rhel-7-0.el7.ngx.noarch.rpm

b) CentOS 7 :

# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

3. Start install Nginx with yum command :

# yum install nginx -y

How to verify the both installation method are successful or not ?

Visit http://Nginx-ipaddres

install nginx

Comments

Leave a Reply

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