How to Build Nginx from Source

by

Hey there! Some links on this page may be affiliate links which means that, if you choose to make a purchase, I may earn a small commission at no extra cost to you. I greatly appreciate your support!

In this tutorial, you’ll learn the steps to build Nginx from source so that you can include modules of your choice. Additionally, we will configure Nginx to start at boot.

1. Download Nginx

First, download Nginx as an archive from the official Nginx website. Extract the archive.

wget nginx tar from nginx.org
tar -xzvf nginx*

2. Install Dependencies

Depending on which modules you choose to install, you may have to install additional dependencies. In this tutorial, we will be installing the following modules which require the corresponding dependencies.

Nginx ModuleDependency
libssl-devhttp_ssl_module

Let’s install these dependencies as well as zlib1g for gzip compression, libpcre3 for regular expression support, and build-essential for gcc and make.

apt-get install build-essential zlib1g zlib1g-dev libssl-dev libpcre3 libpcre3-dev

3. Configure, Make, and Install Nginx

Next, let’s run the configure script and pass in any modules that we’d like to install as well as specify the paths of logs and configuration files. Follow this up with a make and make install.

./configure --sbin-path=/usr/bin/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 --with-http_ssl_module --with-pcre
make
make install

4. Start Nginx

You can start Nginx by simply typing nginx followed by Enter.

nginx

Verify that Nginx is running with ps.

ps aux | grep nginx

You can also navigate to your IP address in a web browser to see the default Nginx page.

5. Launch Nginx on Boot

In order to have Nginx to start at system boot, we can create a service file at /lib/systemd/system/nginx.service with content similar to this:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/bin/nginx -t
ExecStart=/usr/bin/nginx
ExecReload=/usr/bin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Finally, you can enable Nginx on startup with the following command.

systemctl enable nginx

Reboot your server and test to see if Nginx is running when it comes back online.

Please let me know if you have any question about building Nginx from source below.


Meet Tony

With a strong software engineering background, Tony is determined to help as many people as possible start their online busines. Discover why Tony quit his hedge fund job to pursue this mission. You can send Tony a message here.

Leave a Comment