Ubuntu + NginX Server Part 1: Build/Compile/Install
The Nginx is a reserve proxy Web server. You can install Nginx along with another web server. And Nginx is easy to manage and control. I can show you later about its functions. In this part, we will build, compile, and install from its source code.
🕸1. Download Source codes
☯️2. Compile and Install
👢3. Booting Fix
🕸1. Download Source codes
We need PHP support for Nginx. Let’s install PHP,
$ sudo apt install php
Check PHP version:
$ php --version
PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies
We need to update before we build our server.
$ sudo apt udpate
Build folder: mysoft
I will collect all sources in one folder so we can easily build the server from the neighborhood.
~$ mkdir mysoft
~$ cd mysoft
Now, we need to download the source code from some different sites:
⭐Nginx Source Code
Firefox: http://nginx.org/en/download.html
Download the last version of 1.19.0. Please unzip the tar.gz file:
Open a terminal at Download and move the Nginx source folder to mysoft.
Downloads$ sudo mv nginx-1.19.0 ../mysoft
⭐Pcre — Perl Compatible Regular Expressions
You need PCRE from https://ftp.pcre.org/pub/pcre/; choose pcre-8.44.tar.gz. Extract and move it to mysoft.
$ sudo mv pcre-8.44 ../mysoft
⭐Zlib: Compression Library
You need Zlib from https://zlib.net/; choose tar.gz and download. Extract and move it to mysoft.
$ sudo mv zlib-1.2.11 ../mysoft
⭐OpenSSL: Cryptography and SSL/TLS Toolkit
You need OpenSSL from https://www.openssl.org/source/; choose openssl-1.1.1g.tar.gz to download. Extract and move it to mysoft.
$ sudo mv openssl-1.1.1g ../mysoft
☯️2. Compile and Install
Path:~/mysoft/nginx-1.19.0/
Use configure to create the C make file. It is not convenient to enter from the command line because of typos. Let’s add gedit, which is a great GUI notepad.
$ sudo apt install gedit
Let’s create a bash file.
$ gedit compile.sh
And enter these lines:
sudo ./configure \
--prefix=/etc/nginx \
--with-pcre=../pcre-8.44 \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.1g \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_flv_module \
--with-http_mp4_module \
--add-module=../nginx-rtmp-module
Now, let’s run the compile bash.
$ sudo chmod +x compile.sh
$ sudo ./compile.sh
...
nginx path prefix: "/etc/nginx"
nginx binary file: "/etc/nginx/sbin/nginx"
nginx modules path: "/etc/nginx/modules"
nginx configuration prefix: "/etc/nginx/conf"
nginx configuration file: "/etc/nginx/conf/nginx.conf"
nginx pid file: "/etc/nginx/logs/nginx.pid"
nginx error log file: "/etc/nginx/logs/error.log"
nginx http access log file: "/etc/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
We will use these setting to set up the path and files later. I’ll suggest that you save them to the text file so you can check them right away.
Make the binary files.
$ sudo make -j 1
STOP the Nginx server: You can’t override the file when Nginx is running.
$ sudo service nginx stop
If your OS report unrecognized service, that’s fine. Let’s install it.
$ sudo make install...
sed -e "s|%%PREFIX%%|/etc/nginx|" \
-e "s|%%PID_PATH%%|/etc/nginx/logs/nginx.pid|" \
-e "s|%%CONF_PATH%%|/etc/nginx/conf/nginx.conf|" \
-e "s|%%ERROR_LOG_PATH%%|/etc/nginx/logs/error.log|" \
< man/nginx.8 > objs/nginx.8
test -d '/etc/nginx' || mkdir -p '/etc/nginx'
test -d '/etc/nginx/sbin' \
|| mkdir -p '/etc/nginx/sbin'
test ! -f '/etc/nginx/sbin/nginx' \
|| mv '/etc/nginx/sbin/nginx' \
'/etc/nginx/sbin/nginx.old'
cp objs/nginx '/etc/nginx/sbin/nginx'
test -d '/etc/nginx/conf' \
|| mkdir -p '/etc/nginx/conf'
cp conf/koi-win '/etc/nginx/conf'
cp conf/koi-utf '/etc/nginx/conf'
cp conf/win-utf '/etc/nginx/conf'
test -f '/etc/nginx/conf/mime.types' \
|| cp conf/mime.types '/etc/nginx/conf'
cp conf/mime.types '/etc/nginx/conf/mime.types.default'
test -f '/etc/nginx/conf/fastcgi_params' \
|| cp conf/fastcgi_params '/etc/nginx/conf'
cp conf/fastcgi_params \
'/etc/nginx/conf/fastcgi_params.default'
test -f '/etc/nginx/conf/fastcgi.conf' \
|| cp conf/fastcgi.conf '/etc/nginx/conf'
cp conf/fastcgi.conf '/etc/nginx/conf/fastcgi.conf.default'
test -f '/etc/nginx/conf/uwsgi_params' \
|| cp conf/uwsgi_params '/etc/nginx/conf'
cp conf/uwsgi_params \
'/etc/nginx/conf/uwsgi_params.default'
test -f '/etc/nginx/conf/scgi_params' \
|| cp conf/scgi_params '/etc/nginx/conf'
cp conf/scgi_params \
'/etc/nginx/conf/scgi_params.default'
test -f '/etc/nginx/conf/nginx.conf' \
|| cp conf/nginx.conf '/etc/nginx/conf/nginx.conf'
cp conf/nginx.conf '/etc/nginx/conf/nginx.conf.default'
test -d '/etc/nginx/logs' \
|| mkdir -p '/etc/nginx/logs'
test -d '/etc/nginx/logs' \
|| mkdir -p '/etc/nginx/logs'
test -d '/etc/nginx/html' \
|| cp -R html '/etc/nginx'
test -d '/etc/nginx/logs' \
|| mkdir -p '/etc/nginx/logs'
make[1]: Leaving directory '/home/homanadmin/mysoft/nginx-1.19.0'
Start Nginx server:
$ sudo service nginx start
nginx: unrecognized service
That’s trouble to install Nginx from the source.
👢3. Booting Fix
Let’s fix the service boot-up of the Nginx server.
⭐Nginx Deamon
Download Nginx Deamon to /etc/init.d/nginx.
$ sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx...
2020-06-27 21:37:25 (7.23 MB/s) - ‘/etc/init.d/nginx’ saved [10415/10415]
Make the file executable:
$ sudo chmod +x /etc/init.d/nginx
Update default:
$ sudo update-rc.d nginx defaults
The script may have a path issue if your install prefix is not at /usr/local/, such mine as an example. Open the script with gedit.
$ sudo gedit /etc/init.d/nginx
Now, we can start the service.
$ sudo service nginx start
* Starting Nginx Server... [ OK ]
👢 Boot the service
Let’s add Nginx server into the booting process.
$ sudo visudo
With permission: NOPASSWD. For example,
%YOUR USERNAME% All=(ALL:ALL) NOPASSWD:/usr/sbin/nginx
By my setting, it will be,
#nginx binary file: "/etc/nginx/sbin/nginx"
homanadmin All=(ALL:ALL) NOPASSWD:/etc/nginx/sbin/nginx
Ctrl+x, press y to save the file.
Modify the .bashrc by gedit
$ cd
$ gedit .bashrc
with this line.
service nginx status > /dev/null || sudo service nginx start
Restart the terminal.
cat: /etc/nginx/logs/nginx.pid: Permission denied
This error pops up. The service Nginx is a root user, so it cannot load user command. That’s normal in Linux. I have to check the PID directly instead of Nginx status. Furthermore, you are a programmer. You shall prepare a bin of some executable scripts.
⭐Personal bin: $HOME/bin
My bin:
$ mkdir bin
In the bin, let’s make some bash.
Some bash: ngstar = Nginx start; ngstop = Nginx stop; ngrest = Nginx restart; ngstat = Nginx stat:
$ cd bin
$ echo "sudo service nginx start" > ngstar
$ echo "sudo service nginx stop" > ngstop
$ echo "sudo service nginx restart" > ngrest
$ echo "sudo service nginx status" > ngstat
Make them all be executable.
$ chmod 700 *
Now, we can add a new path for the private bin in the .bashrc and PID check-up.
$ cd
$ gedit .bashrc
export PATH="$PATH:$HOME/bin"
...
# Fail with root call: service nginx status > /dev/null || sudo service nginx start
# PID check-up
NGPID=`ps aux | grep root | grep nginx`
if [ -z "$NGPID" ]; then
echo -e "Starting NginX Server:\n"
ngstar
else
echo -e "\nNginx Server has started!"
fi
Restart terminal or Ubuntu:
No more error.
📚Extra: Link NginX manual.
~/mysoft/nginx-1.19.0/man:
$ cd ~/mysoft/nginx-1.19.0/man$ sudo cp *.* /usr/share/man/man8
The nginx.8 is a zipped file. Let’s unzip it.
$ sudo gzip /usr/share/man/man8/nginx.8
Is it unzipped?
$ ls /usr/share/man/man8/ | grep nginx.8.gz
nginx.8.gz
Now, the manual shall be working.
$ man nginx