Compile Nginx

How To Compile Nginx from Source on a CentOS Server

Nginx is a powerful and flexible web server that has gained immense popularity for its high performance, scalability, and low resource consumption. While many users opt for pre-compiled packages available through the CentOS repositories, compiling Nginx from source allows for greater customization and control over your server environment. In this blog post, we will walk you through the steps to compile and install Nginx from source on a CentOS server.

Prerequisites

Before we begin, ensure you have the following:

  1. A CentOS server (CentOS 7 or later is recommended).
  2. Root access to the server (or use sudo privileges).
  3. Basic knowledge of the Linux command line.

Step 1: Update Your System

Start by updating your CentOS system to ensure all packages are current. Open your terminal and run:

sudo yum update -y

Step 2: Install Required Dependencies

To compile Nginx from source, you need to install several dependencies. Use the following command to install the necessary development tools and libraries:

sudo yum groupinstall "Development Tools" -y
sudo yum install pcre-devel zlib-devel openssl-devel -y
  • Development Tools: This package group includes essential compilation tools like GCC and make.
  • pcre-devel: Required for regular expression support.
  • zlib-devel: Needed for compression support.
  • openssl-devel: Provides SSL/TLS support.

Step 3: Download Nginx Source Code

Visit the Nginx official website to find the latest stable version. Alternatively, you can use the following command to download the latest version directly:

cd /usr/local/src
sudo wget http://nginx.org/download/nginx-1.23.0.tar.gz

Note: Replace 1.23.0 with the latest version number as needed.

After downloading, extract the tarball:

sudo tar -zxvf nginx-1.23.0.tar.gz

Step 4: Configure Nginx

Navigate to the extracted directory and configure the build options. The ./configure script allows you to customize the installation. Here’s a basic command to get started:

cd nginx-1.23.0
sudo ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_ssl_module --with-pcre

Here’s a breakdown of some of the options:

  • --prefix: Specifies the installation directory for Nginx.
  • --sbin-path: Where the Nginx executable will be located.
  • --conf-path: Path to the main Nginx configuration file.
  • --error-log-path and --http-log-path: Define the locations for the error and access logs.
  • --with-http_ssl_module: Enables SSL support.
  • --with-pcre: Enables PCRE support for regular expressions.

You can add other modules and options as needed. To see additional configuration options, run:

sudo ./configure --help

Step 5: Compile and Install Nginx

Once configured, compile Nginx with the following command:

sudo make

After the compilation completes, install Nginx:

sudo make install

Step 6: Start Nginx

To start Nginx, you can use the following command:

sudo /usr/sbin/nginx

To verify that Nginx is running, navigate to your server’s IP address in a web browser. You should see the Nginx welcome page.

Step 7: Configure Nginx to Start on Boot

To ensure Nginx starts automatically on server boot, create a systemd service file:

sudo nano /etc/systemd/system/nginx.service

Add the following configuration:

[Unit]
Description=Nginx
After=network.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

[Install]
WantedBy=multi-user.target

Save and exit the editor. Then, enable and start the Nginx service:

sudo systemctl enable nginx
sudo systemctl start nginx

Step 8: Configure Firewall (If Needed)

If your CentOS server has a firewall enabled, you may need to allow HTTP and HTTPS traffic. Use the following commands:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Conclusion

You have successfully compiled and installed Nginx from source on your CentOS server! Compiling from source not only provides you with the latest features and optimizations, but it also allows you to configure Nginx to meet your unique requirements.

Remember to regularly check for updates and security patches for Nginx to ensure your web server stays secure. Happy hosting with Nginx!

For more tips and guidance on server management, stay tuned to our blog at Greenhost.cloud! If you have any questions or need assistance, feel free to reach out to our support team.