How to compile and install Nginx [CENTOS7][UBUNTU 17.04]

I will try to make this as OS independent as possible however some parts may vary depending on your host.

  1. First update all of your packages:

    For CentOS

    sudo yum update -y

    For Debian/Ubuntu

    sudo apt-get update && sudo apt-get upgrade -y
  2. Second, install required dependencies for building Nginx:

    For CentOS

    sudo yum groupinstall -y 'Development Tools'

    For Debian/Ubuntu

    sudo apt install build-essential -y
  3. Thirdly, Download the latest (Or chosen version) of Nginx from https://nginx.org/download/, At the time of writing this is nginx-1.13.6.tar.gz

    For Both OS’s

    wget https://nginx.org/download/nginx-1.13.6.tar.gz
  4. Now extract the .tar.gz

    For Both OS’s

    tar -xvzf nginx-1.13.6.tar.gz
  5. Now also download and extract some Nginx dependencies

    For Both OS’s

    # PCRE version 4.4 - 8.40
    wget https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz && tar xzvf pcre-8.40.tar.gz
    
    # zlib version 1.1.3 - 1.2.11
    wget http://www.zlib.net/zlib-1.2.11.tar.gz && tar xzvf zlib-1.2.11.tar.gz
    
    # OpenSSL version 1.0.2 - 1.1.0
    wget https://www.openssl.org/source/openssl-1.1.0f.tar.gz && tar xzvf openssl-1.1.0f.tar.gz
  6. Now cd into the Nginx extracted directory

    For Both OS’s

    cd nginx-1.13.6
  7. Now it gets real, lets ./configure!

    For Ubuntu (I have chosen to run the nginx user as www-data in this example, this is usually nginx if installed from a package)

    ./configure --prefix=/usr/share/nginx \
                --sbin-path=/usr/sbin/nginx \
                --modules-path=/usr/lib/nginx/modules \
                --conf-path=/etc/nginx/nginx.conf \
                --error-log-path=/var/log/nginx/error.log \
                --http-log-path=/var/log/nginx/access.log \
                --pid-path=/run/nginx.pid \
                --lock-path=/var/lock/nginx.lock \
                --user=www-data \
                --group=www-data \
                --build=Ubuntu \
                --http-client-body-temp-path=/var/lib/nginx/body \
                --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
                --http-proxy-temp-path=/var/lib/nginx/proxy \
                --http-scgi-temp-path=/var/lib/nginx/scgi \
                --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
                --with-openssl=../openssl-1.1.0f \
                --with-openssl-opt=enable-ec_nistp_64_gcc_128 \
                --with-openssl-opt=no-nextprotoneg \
                --with-openssl-opt=no-weak-ssl-ciphers \
                --with-openssl-opt=no-ssl3 \
                --with-pcre=../pcre-8.40 \
                --with-pcre-jit \
                --with-zlib=../zlib-1.2.11 \
                --with-compat \
                --with-file-aio \
                --with-threads \
                --with-http_addition_module \
                --with-http_auth_request_module \
                --with-http_dav_module \
                --with-http_flv_module \
                --with-http_gunzip_module \
                --with-http_gzip_static_module \
                --with-http_mp4_module \
                --with-http_random_index_module \
                --with-http_realip_module \
                --with-http_slice_module \
                --with-http_ssl_module \
                --with-http_sub_module \
                --with-http_stub_status_module \
                --with-http_v2_module \
                --with-http_secure_link_module \
                --with-mail \
                --with-mail_ssl_module \
                --with-stream \
                --with-stream_realip_module \
                --with-stream_ssl_module \
                --with-stream_ssl_preread_module \
                --with-debug \
                --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' \
                --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now'
  8. Now make, and make install

    For Both OS’s

    make && sudo make install

    Well done! You now have Nginx installed! But we aren’t quite done yet.

  9. Before Nginx will even start you will need to create this directory:

    For Both OS’s

    sudo mkdir -p /var/lib/nginx
  10. now lets create a systemd unit, so we can start our service with systemctl:

    For Both OS’s

    sudo nano /etc/systemd/system/nginx.service

    Paste this into the above file:

    [Unit]
    Description=A high performance web server and a reverse proxy server
    After=network.target
    
    [Service]
    Type=forking
    PIDFile=/run/nginx.pid
    ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
    ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
    ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
    ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
    TimeoutStopSec=5
    KillMode=mixed
    
    [Install]
    WantedBy=multi-user.target
  11. Thats it, lets start Nginx:

    For Both OS’s

    sudo systemctl start nginx.service && sudo systemctl enable nginx.service
  12. You have now installed Nginx. visiting http://yourserverip/ should greet you with “Welcome to Nginx”

You May Also Like

About the Author: trader418

2 Comments

Leave a Reply

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