Building Nginx on Ubuntu 18.04
TL;DR: Building Nginx yourself is easy. Clone it, run ./auto/configure
with the desired options, install the necessary dependencies, then run make
and sudo make install
.
- Author:
- Christian Hujer, CEO / CTO at Nelkinda Software Craft Pvt Ltd
- First Published:
- by Nelkinda Software Craft Private Limited
- Last Modified:
- by Christian Hujer
- Approximate reading time:
1 Why Build Nginx Yourself?
Actually, most Nginx users will not need to build Nginx themselves. There are two reasons why you might want to build Nginx yourself:
- You're not happy with the default configuration and want to enable or disable certain modules at compile time.
- You want to make changes to Nginx itself or one of its modules.
In my case, I plan to make changes to the file ngx_http_xslt_filter_module.c
to experiment with enabling XInclude in XSLT.
2 Getting the Nginx Source Code
[NginxDownload] tells us how to get the Nginx source code. The Nginx source code is hosted using Mercurial at http://hg.nginx.org/nginx. So, first, install Mercurial: sudo apt-get install mercurial
. Then clone the repository using hg clone http://hg.nginx.org/nginx
.
3 Configure, Build, Install Nginx
From here on, the working directory is the cloned nginx repository. If you follow the steps, cd nginx
.
3.1 Configure
Before Nginx can be built, it needs to be configured. To view and set the configuration, use ./auto/configure
. (Changing to auto
and running ./configure
from there will not work.) The command ./auto/configure --help
lists all the available configuration options. The configuration options are pretty self-explanatory. In case you still need documentation, it's documented on the Nginx website in the section [NginxBuilding].
In my case, I'm interested in HTTP2, SSL, and XSLT. These modules are not built by default. So my configuration command line is: ./auto/configure --with-http_xslt_module=dynamic --with-http_v2_module --with-http_ssl_module
.
3.2 Install Missing Dependencies
When configuring Nginx, it will most likely complain about missing libraries or library headers. Read the messages carefully, and use apt-get search
to figure out which packages are missing. In my case, the following command installed the missing packages: sudo apt-get install -y libpcre3-dev lib64z1-dev libxslt1-dev libssl-dev
3.3 Build
After resolving the missing dependencies on libraries and library headers, the ./auto/configure
command should succeed. Then Nginx can be built by running make
. A couple of seconds later, you have your own compiled Nginx in the objs/
directory.
3.4 Install
To install Nginx, run sudo make install
. This will install Nginx into /usr/local/
.
4 Conclusion
Building Nginx yourself is easy. Also, note that while I have performed the steps on Ubuntu 18.04 (actually Kubuntu 18.04), this will work on every Linux distribution. The only difference for some distributions will be the commands for how to find and install the missing dependencies.