Compiling Nginx on CentOS 7: Building a high-performance custom web server

1. Quotes

Nginx is a high-performance open source web server known for its excellent performance, reliability and flexibility. Although you can install Nginx on CentOS 7 through package management tools, sometimes you may need to custom compile Nginx to meet specific needs or for advanced configuration.

In this tutorial, we will show you how to build and compile Nginx from source on CentOS 7 so that you can customize it to suit your needs. We'll guide you step-by-step through the compilation process and provide some common configuration options and suggestions to help you build a high-performance custom web server.

Whether you are building a web server for a personal project or an enterprise application, this tutorial will provide you with the necessary guidance and knowledge to take advantage of the power of Nginx and get the best performance and security.

let's start! The following are the detailed steps and considerations for compiling Nginx on CentOS 7.

2. Preparations before installation

1. Turn off the firewall and SELinux

systemctl stop firewalld
setenforce 0

2. Download the nginx installation package

Go to the official website (mysql.org) to download the compilation and installation package (https://nginx.org/download/nginx-1.22.1.tar.gz) 

3. Upload the installation package

After waiting for the installation package to download successfully, we use the "rz" command to upload the installation package to the virtual machine. If the virtual machine environment we prepare is the minimized version of CentOS 7, there is no "rz" command, and we need to advance Install the "rz" command.

yum -y installl lrzsz

Once the installation is successful, you can upload it.

4. Unzip the installation package

Use the command "tar zxvf" to decompress the installation package (nginx-1.22.1.tar.gz), and we see a folder (nginx-1.22.1)

tar zvxf nginx-1.22.1.tar.gz

5. Install dependencies

In this way, our next operation is to cd to the nginx-1.22.1 directory, and we can see an executable program configure. This is the file we want to compile, but before compiling, we need to install some dependencies required for nginx compilation. as follows:

nginx编译所需要的依赖有gcc(c语言编译器)

gcc-c++(c++的编译器)

pcre(使nginx支持http rewrite模块)

pcre-devel(PCRE库的开发包)

gd-devel(是与GD图形库相关的开发包)

openssl-devel(是与OpenSSL加密库相关的开发包)

 zlib(提供nginx对http包的内容进行gzip压缩)

zlib-devel(是与zlib压缩库相关的开发包)

Use yum source to install compilation dependencies.​ 

yum -y install gcc gcc-c++ pcre pcre-devel gd-devel openssl-devel  zlib zlib-devel        #安装依赖

Wait for the installation to complete and check if any dependencies have not been successfully installed. If there are no problems, our dependencies have been installed.​ 

6. Configure installation parameters

After the dependency installation is completed, it is time to configure the installation parameters, but before configuring, we'd better create a login user for nginx so that we can use the nginx account to manage the nginx service, because the root account has too much authority. If the software appears Vulnerabilities may leak root privileges.

useradd nginx -s /sbin/nologin

After the account is successfully created, we will start configuring the installation parameters. I have listed some common parameters of nginx below. You can choose according to your own needs.

--prefix=/usr/local/nginx                        //指向安装目录
--conf-path=/etc/nginx/nginx.conf                //指定配置文件
--http-log-path=/var/log/nginx/access.log        //指定访问日志
--error-log-path=/var/log/nginx/error.log        //指定错误日志
--lock-path=/var/lock/nginx.lock                 //指定lock文件
--pid-path=/run/nginx.pid                        //指定pid文件

--http-client-body-temp-path=/var/lib/nginx/body    //设定http客户端请求临时文件路径
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi     //设定http fastcgi临时文件路径
--http-proxy-temp-path=/var/lib/nginx/proxy         //设定http代理临时文件路径
--http-scgi-temp-path=/var/lib/nginx/scgi           //设定http scgi临时文件路径
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi         //设定http uwsgi临时文件路径

--with-debug                                        //启用debug日志
--with-ipv6                                         //启用ipv6支持
--with-http_ssl_module                              //启用ssl支持
--with-http_stub_status_module                      //获取nginx自上次启动以来的状态
--with-http_realip_module                 //允许从请求标头更改客户端的IP地址值,默认为关
--with-http_auth_request_module           //实现基于一个子请求的结果的客户端授权。如果该子请求返回的2xx响应代码,所述接入是允许的。如果它返回401或403中,访问被拒绝与相应的错误代码。由子请求返回的任何其他响应代码被认为是一个错误。
--with-http_addition_module               //作为一个输出过滤器,支持不完全缓冲,分部分响应请求
--with-http_dav_module                    //增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法 默认关闭,需编译开启
--with-http_geoip_module                  //使用预编译的MaxMind数据库解析客户端IP地址,得到变量值
--with-http_gunzip_module                 //它为不支持“gzip”编码方法的客户端解压具有“Content-Encoding: gzip”头的响应。
--with-http_gzip_static_module            //在线实时压缩输出数据流
--with-http_spdy_module                   //SPDY可以缩短网页的加载时间
--with-http_sub_module                    //允许用一些其他文本替换nginx响应中的一些文本
--with-http_xslt_module                   //过滤转换XML请求
--with-mail                               //启用POP3/IMAP4/SMTP代理模块支持
--with-mail_ssl_module                    //启用ngx_mail_ssl_module支持启用外部模块支持

The parameters I configured below are configured according to your own needs. You can refer to them.

./configure --prefix=/usr/local/nginx \
		--group=nginx \
		--user=nginx \
		--sbin-path=/usr/local/nginx/sbin/nginx \
		--conf-path=/etc/nginx/nginx.conf

 If an error occurs during the configuration process, we only need to delete the Makefile and reconfigure it.

7. Compile and compile and install

We use the "make" command to compile and the "make install" command to compile and install.

make && make install  #编译和编译安装

 This compilation and installation process may take a long time. I need to wait patiently. After the compilation and installation is completed, we have to cd to the /usr/local/ directory to see if there is an nginx directory. If there is an nginx directory, it means that our compilation was successful. . Next we can start the nginx service.

8. Start nginx service

After the check and confirmation is completed, we can start the nginx service. First, cd to the /usr/local/nginx/sbin directory, and then use the "./nginx" command to start the nginx service. Then use the "ps -ef |grep nginx" command to check the nginx process to determine whether the installation is successful. If there is a running process of nginx, congratulations, the nginx service is successfully established.

3. Conclusion 

In this tutorial, we detail how to compile Nginx from source on CentOS 7 and provide some common configuration options and recommendations to help you build a high-performance custom web server.

By custom compiling Nginx, you can flexibly configure and optimize it according to your specific needs to obtain the best performance and security. Remember, before compiling Nginx, make sure you understand the relevant compilation dependencies and system requirements and follow the steps in this tutorial.

We chose to compile on CentOS 7 because CentOS 7 is a widely used Linux distribution suitable for many server environments. If you are using another operating system or version, please refer to the appropriate documentation and resources to ensure applicability and correctness.

Thank you for reading this tutorial! I hope you got the results you expected by compiling Nginx and can take full advantage of it in your project.

If you have any questions or feedback, please feel free to leave a message in the comments section. Good luck building a high-performance custom web server!

Guess you like

Origin blog.csdn.net/XX_HK/article/details/134023659