You may want a version of Nginx that is newer than available in the repositories. As of today (2019 April 30), the latest version is 1.16.0. The Nginx developers maintain a yum repository with the latest version. I recommend using the repository because it will be easier to keep Nginx up to date. The Nginx Linux packages page explains how to add their repository to your system and install Nginx from it. If you still wish to install from source, keep reading.
The process of installing Nginx from source on CentOS has relatively few steps. They are
- Install a some required packages
- Create an unprivileged service account
- Download the Nginx source
- Compile and Install Nginx
- Create a SystemD unit file
- Create a SystemD environment file
- Test the installation
- Configure Nginx
- Enable Nginx to start at boot
Install Required Packages
Compiling and running Nginx 1.16.0 requires several packages from the repositories. Install them with this command:
[root@centos7 ~]# yum install \
gcc \
zlib-devel \
openssl-devel \
make \
pcre-devel \
libxml2-devel \
libxslt-devel \
libgcrypt-devel \
gd-devel \
perl-ExtUtils-Embed \
GeoIP-devel
Create an Unprivileged Service Account
Create a service account identical to the one the Nginx package in the EPEL repository creates:
[root@centos7 ~]# groupadd -g 994 nginx
[root@centos7 ~]# useradd -g 994 -u 996 -c "Nginx web server" -d /var/lib/nginx -s /sbin/nologin nginx
If you get the output below, you can safely move on. It just means Nginx is or has been installed from a repository.
[root@centos7 ~]# groupadd -g 994 nginx
groupadd: group 'nginx' already exists
[root@centos7 ~]# useradd -g 994 -u 996 -c "Nginx web server" -d /var/lib/nginx -s /sbin/nologin nginx
useradd: user 'nginx' already exists
Download the Nginx Source
Use curl to download the Nginx source code:
[root@centos7 ~]# curl -L https://github.com/nginx/nginx/archive/release-1.16.0.tar.gz > nginx-release-1.16.0.tar.gz
Extract the source code archive with tar:
[root@centos7 ~]# tar xf nginx-release-1.16.0.tar.gz
Compile Nginx
Change your working directory to the directory created by the archive file:
[root@centos7 ~]# cd nginx-release-1.16.0
The Building Nginx from Sources page on the official website provides descriptions of the options.
Configure the compile time options:
[root@centos7 nginx-release-1.16.0]# auto/configure \
--with-pcre \
--prefix=/opt/nginx-1.16.0 \
--user=nginx \
--group=nginx \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module \
--with-http_geoip_module=dynamic \
--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_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--without-http_charset_module \
--with-http_perl_module \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module=dynamic \
--with-stream_ssl_preread_module
Compile and install Nginx:
[root@centos7 nginx-release-1.16.0]# make
[root@centos7 nginx-release-1.16.0]# make install
Create a SystemD Unit
If you are on a minimal system, chances are the only text editor available is vi. If this is the case and you do not want to install another editor, I wrote a short tutorial explaining the basics.
Create the unit file. Add the following to the file /etc/systemd/system/nginx-1.16.0.service:
[Unit]
Description=nginx 1.16.0
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=/etc/sysconfig/nginx-1.16.0
ExecStart=/opt/nginx-1.16.0/sbin/nginx $CLI_OPTIONS
ExecReload=/opt/nginx-1.16.0/sbin/nginx -s reload
ExecStop=/opt/nginx-1.16.0/sbin/nginx -s quit
[Install]
WantedBy=multi-user.target
Load the unit file into SystemD:
[root@centos7 nginx-release-1.16.0]# systemctl daemon-reload
Create an environment file for Nginx. Add the following to /etc/sysconfig/nginx-1.16.0:
# Command line options to use when starting nginx
#CLI_OPTIONS=""
Test Your Installation
Start Nginx:
[root@centos7 ~]# systemctl start nginx-1.16.0
[root@centos7 ~]# curl http://localhost | grep 'Thank you'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 612 100 612 0 0 117k 0 --:--:-- --:--:-- --:--:-- 149k
<p><em>Thank you for using nginx.</em></p>
Configure Nginx
Configuring Nginx is beyond the scope of this guide. See the official documentation for guidance.
Enable Nginx to Start at Boot
The following command will ensure Nginx starts automatically when the system boots:
[root@centos7 ~]# systemctl enable nginx-1.16.0