others-How to build nginx from source on ubuntu system?

1. Purpose

In this post, I would demonstrate how to build nginx from source on ubuntu system.



2. The solution

2.1 Install the dependencies

sudo aptitude install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev libgd-dev libxml2 libxml2-dev uuid-dev

You can replace aptitude with apt.



2.2 Download nginx source code

Download the latest release of nginx:

wget  http://nginx.org/download/nginx-1.20.0.tar.gz



2.3 Compile the nginx source code

Now compile the code:

./configure --prefix=/var/www/html --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-pcre  --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_image_filter_module=dynamic --modules-path=/etc/nginx/modules --with-http_v2_module --with-stream=dynamic --with-http_addition_module --with-http_mp4_module



2.4 Solve the the HTTP image filter module requires the GD library error

If you get error like this:

./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

You can solve it like following:

aptitude install libgd-dev

The following packages have unmet dependencies:
 libx11-dev : Depends: libx11-6 (= 2:1.6.9-2ubuntu1.2) but 2:1.6.9-2ubuntu1.3 is installed
The following actions will resolve these dependencies:

     Keep the following packages at their current version:
1)     libgd-dev [Not Installed]
2)     libx11-dev [Not Installed]
3)     libxpm-dev [Not Installed]
4)     libxt-dev [Not Installed]



Accept this solution? [Y/n/q/?] n
Accept this solution? [Y/n/q/?] n
The following actions will resolve these dependencies:

     Downgrade the following packages:
1)     libx11-6 [2:1.6.9-2ubuntu1.3 (now) -> 2:1.6.9-2ubuntu1.2 (focal-security, focal-updates)]



Accept this solution? [Y/n/q/?] Y
The following packages will be DOWNGRADED:
  libx11-6
The following NEW packages will be installed:
  fontconfig-config{a} fonts-dejavu-core{a} libfontconfig1{a} libfontconfig1-dev{a} libfreetype-dev{a} libfreetype6-dev{a} libgd-dev libgd3{a} libice-dev{a} libice6{a} libjbig-dev{a} libjbig0{a} libjpeg-dev{a} libjpeg-turbo8{a} libjpeg-turbo8-dev{a} libjpeg8{a} libjpeg8-dev{a}
  libpng-dev{a} libpng-tools{a} libpthread-stubs0-dev{a} libsm-dev{a} libsm6{a} libtiff-dev{a} libtiff5{a} libtiffxx5{a} libvpx-dev{a} libvpx6{a} libwebp6{a} libx11-dev{a} libxau-dev{a} libxcb1-dev{a} libxdmcp-dev{a} libxpm-dev{a} libxpm4{a} libxt-dev{a} libxt6{a} x11-common{a}
  x11proto-core-dev{a} x11proto-dev{a} xorg-sgml-doctools{a} xtrans-dev{a}
0 packages upgraded, 41 newly installed, 1 downgraded, 0 to remove and 0 not upgraded.
Need to get 8638 kB of archives. After unpacking 28.3 MB will be used.
Do you want to continue? [Y/n/?] Y

Then compile again:

./configure --prefix=/var/www/html --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-pcre  --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_image_filter_module=dynamic --modules-path=/etc/nginx/modules --with-http_v2_module --with-stream=dynamic --with-http_addition_module --with-http_mp4_module



2.5 Make and install nginx

Now make the nginx binary and install the binary:

make && make install



2.6 Start nginx

Now start nginx and check the version:

 ⚡ root@launch-advisor-20191120  ~/nginx_source/nginx-1.20.0  nginx
 
 ⚡ root@launch-advisor-20191120  ~/nginx_source/nginx-1.20.0  ps -ef|grep nginx
root       57486       1  0 16:47 ?        00:00:00 nginx: master process nginx
nobody     57487   57486  0 16:47 ?        00:00:00 nginx: worker process
root       57495   47639  0 16:47 pts/0    00:00:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox nginx

 ⚡ root@launch-advisor-20191120  ~/nginx_source/nginx-1.20.0  nginx -v
nginx version: nginx/1.20.0

 ⚡ root@launch-advisor-20191120  ~/nginx_source/nginx-1.20.0  nginx -V
nginx version: nginx/1.20.0
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04)
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/var/www/html --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --with-pcre --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-http_ssl_module --with-http_image_filter_module=dynamic --modules-path=/etc/nginx/modules --with-http_v2_module --with-stream=dynamic --with-http_addition_module --with-http_mp4_module

It works!



3. Summary

In this post, I demonstrated how to build nginx from source and how to solve relavant compiling problems. That’s it, thanks for your reading.