In the IT industry, not every business uses the traditional way of software installation in the server (Linux) viz package installation using a command like apt-get or yum.
So what they do? As we know software like Nginx is open-source so anyone can get its source code. So we use that source code, compile them and then install the software in the system and this is called “source installation”. But after compilation of this type of installation, we did not get the directory structure as we get with the traditional installation (by apt-get or yum). This might be slightly confusing for those who used to install via “yum” or “apt-get”.
Here are simple steps in source installation that can be used to provide you the same familiar Nginx directory structure as we get in a traditional installation.
Step 1: Install the Required Packages
Nginx is written in C programming language, so we have to install a compiler and other essentials packages after apt update command.
$ sudo apt update
$ sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev php-fpm
Step 2: Download a Stable Nginx Version
Now download any stable package of Nginx. In this case, we are using Nginx1.16.1. You can download other versions of NGINX from the official website. You can download other versions from here.
$ wget https://nginx.org/download/nginx-1.16.1.tar.gz
Extract it and switch in that directory.
$ tar xvf nginx-1.16.1.tar.gz $ cd nginx-1.16.1/
Step 3: Compile and Make Install the Nginx Source Code
Now compile the code with the “configure” file. There are lots of build options that you can see by –help. We will use –prefix=<path> to install it in /etc/nginx.
$ ./configure --prefix=/etc/nginx
$ sudo make $ sudo make install
Step 4: Create Nginx Service File
$ sudo nano /lib/systemd/system/nginx.service
Paste this and if your –prefix=<path> is different, then change according to that.
[Unit] Description=A high performance web server and a reverse proxy server Documentation=man:nginx(8) After=network.target [Service] Type=forking PIDFile=/etc/nginx/logs/nginx.pid ExecStartPre=/etc/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;' ExecStart=/etc/nginx/sbin/nginx -g 'daemon on; master_process on;' ExecReload=/etc/nginx/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
Step 5: Start and Enable The Nginx
$ sudo systemctl start nginx $ sudo systemctl enable nginx
Step 6: Make Nginx Directory Similar to Default Directory of Nginx
Now after all these setups, if you try to look at the Nginx directory in /etc/nginx then it will not look as familiar as you expected. The main reason is that you don’t have those three directories that used most i.e “conf.d”, “sites-available” and “sites-enabled”.
So to make things easy we will create those directories and set up them to work like they used to do in a traditional installation.
$ sudo mkdir /etc/nginx/sites-available /etc/nginx/sites-enabled /etc/nginx/conf.d
Now, we will add two lines in nginx.conf file which will make our created directory work as they supposed to do.
$ sudo nano /etc/nginx/conf/nginx.conf
Add these two lines.
include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*;
Restart Nginx and all three directories will work like as they worked in package installed Nginx.
Advantages of Installing Nginx From Source:
- One can always install the latest version of the software and stay updated for security patches or a new feature.
- It gives you the freedom to choose the features as per your requirements.
- You can install it at any location you wish.
- You can switch to the previous version easily.