In this tutorial, we will be briefed about the how to install Nginx from Source Code on CentOS 7. Nginx is an open-source, high-performance web server that delivers static content by using the system resources. It also hosts several highest-traffic internet sites.
Prerequisites
Before starting with the tutorial, make sure you are logged in as a user with Sudo privileges and EPEL and you don’t have Apache or any other service running on port 80 or 443.
Installing Nginx on CentOS
Follow the steps below to install Nginx on your CentOS server:
Nginx packages are available in the EPEL repositories. If you don’t have EPEL repository already installed you can do it by typing:
sudo yum install epel-release
Now Install some necessary packages:
sudo yum groupinstall " Development Tools" -y sudo yum install zlib-devel pcre-devel openssl-devel perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel -y
Download the Nginx source code from Nginx’s official website or just run the below wget
command to install Nginx version 1.20.2.
sudo wget http://nginx.org/download/nginx-1.20.2.tar.gz
Extract the downloaded package using tar
the command and move into the extracted directory to compile Nginx from the source:
sudo tar xvf nginx-1.20.2.tar.gz cd nginx-1.20.2/
Now to configure the Nginx installation procedure, run the configuration script command with some options that will be required for installation. To view all the options, simply run the below script command with help options and check the options that should be configured for Nginx installation. Or you can also install the default configuration by running the configure script command without any options followed by it:
./configure --help
Now run the configure script command followed by the required options:
./configure --prefix=/opt/nginx --sbin-path=/opt/nginx/sbin/nginx --modules-path=/opt/nginx/modules --conf-path=/opt/nginx/conf/nginx.conf --error-log-path=/opt/nginx/logs/error.log --http-log-path=/opt/nginx/logs/access.log --pid-path=/opt/nginx/logs/nginx.pid --lock-path=/opt/nginx/logs/nginx.lock --http-client-body-temp-path=/opt/nginx/client_temp --http-proxy-temp-path=/opt/nginx/proxy_temp --http-fastcgi-temp-path=/opt/nginx/fastcgi_temp --http-uwsgi-temp-path=/opt/nginx/uwsgi_temp --http-scgi-temp-path=/opt/nginx/scgi_temp --user=nginx --group=nginx
Execute the make
the command for the configuration process:
sudo make
Now install the Nginx with make install
command as follows:
sudo make install
Then Change Ownership and GroupOwnerShip of the Whole directory:
sudo chown nginx:nginx -R /opt/nginx sudo chmod 755 -R /opt/nginx
Let’s verify the version of Nginx:
cd /opt/nginx/sbin/ sudo ./nginx -t Output: nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
Now create a service file for Nginx:
sudo vim /etc/systemd/system/nginx.service
Paste the following lines in the file:
[Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStartPre=/opt/nginx/sbin/nginx -t ExecStart=/opt/nginx/sbin/nginx ExecReload=/opt/nginx/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
Now start the service using systemctl
command:
sudo systemctl start nginx
Nginx Configuration File’s Structure and Best Practices
- All Nginx configuration files are located in the
/opt/nginx
directory. - The main Nginx configuration file is
./opt/nginx/conf/nginx.conf
- To make Nginx configuration easier to maintain it is recommended to create a separate configuration file for each domain.
- New Nginx server block files must end with .conf and be stored in
the directory. You can have as many server blocks as you need./opt/nginx/conf/
- It is a good idea to follow a standard naming convention, for example, if your domain name is
mydomain.com
then your configuration file should be named
/mydomain.com.conf/opt/nginx/conf
- Nginx log files (
access.log
anderror.log
) are located in the/opt/nginx/logs
directory. It is recommended to have a differentaccess
anderror
log files for each server block.
Related Article: How to install SSL certificates with OpenSSL for Nginx
Conclusion:
Congratulations! You have successfully installed Nginx by source code!
Pingback: Nginx interview Questions and Answers - Youngster Company
This is a topic which is close to my heart… Many thanks! Where are your contact details though?