Nginx系列:简介和安装(基于yum和源码的方式)

来自:博客园
时间:2024-01-30
阅读:

简介

Nginx是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。其特点是占有内存少,并发能力强,在同类型的网页服务器中表现较好。此外,Nginx也可以作为一个通用的TCP/UDP代理服务器。

Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点开发的,公开版本1.19.6发布于2020年12月15日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。

Nginx可以作为一个HTTP服务器,将服务器上的静态文件(如HTML、图片)通过HTTP协议展现给客户端。同时,Nginx也可以作为反向代理服务器,为后端服务器代理请求。

Nginx应用比较多的场景是作为Web服务器和反向代理服务器,并且可以通过配置虚拟主机提供HTTP Web服务。此外,Nginx也可以提供一定的缓存服务功能。

基于yum

[root@VM-8-17-centos ~]# yum list all | grep nginx 
nginx.x86_64                             1:1.20.1-10.el7               @epel    
nginx-filesystem.noarch                  1:1.20.1-10.el7               @epel    
collectd-nginx.x86_64                    5.8.1-1.el7                   epel     
munin-nginx.noarch                       2.0.69-5.el7                  epel     
nginx-all-modules.noarch                 1:1.20.1-10.el7               epel     
nginx-mod-devel.x86_64                   1:1.20.1-10.el7               epel     
nginx-mod-http-image-filter.x86_64       1:1.20.1-10.el7               epel     
nginx-mod-http-perl.x86_64               1:1.20.1-10.el7               epel     
nginx-mod-http-xslt-filter.x86_64        1:1.20.1-10.el7               epel     
nginx-mod-mAIl.x86_64                    1:1.20.1-10.el7               epel     
nginx-mod-stream.x86_64                  1:1.20.1-10.el7               epel     
pagure-web-nginx.noarch                  5.13.3-2.el7                  epel     
pcp-pmda-nginx.x86_64                    4.3.2-13.el7_9                updates  
python2-certbot-nginx.noarch             1.11.0-1.el7                  epel     
sympa-nginx.x86_64                       6.2.70-2.el7                  epel     
[root@VM-8-17-centos ~]# yum install nginx -y 
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.20.1-10.el7 will be installed
--> Processing Dependency: nginx-filesystem = 1:1.20.1-10.el7 for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_1)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libssl.so.1.1()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Running transaction check
---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed
---> Package nginx-filesystem.noarch 1:1.20.1-10.el7 will be installed
---> Package openssl11-libs.x86_64 1:1.1.1k-5.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================
 Package                         Arch                  Version                         Repository           Size
=================================================================================================================
Installing:
 nginx                           x86_64                1:1.20.1-10.el7                 epel                588 k
Installing for dependencies:
 gperftools-libs                 x86_64                2.6.1-1.el7                     os                  272 k
 nginx-filesystem                noarch                1:1.20.1-10.el7                 epel                 24 k
 openssl11-libs                  x86_64                1:1.1.1k-5.el7                  epel                1.5 M

Transaction Summary
=================================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 2.3 M
Installed size: 6.6 M
Downloading packages:
(1/4): nginx-filesystem-1.20.1-10.el7.noarch.rpm                                          |  24 kB  00:00:00     
(2/4): nginx-1.20.1-10.el7.x86_64.rpm                                                     | 588 kB  00:00:00     
(3/4): gperftools-libs-2.6.1-1.el7.x86_64.rpm                                             | 272 kB  00:00:00     
(4/4): openssl11-libs-1.1.1k-5.el7.x86_64.rpm                                             | 1.5 MB  00:00:00     
-----------------------------------------------------------------------------------------------------------------
Total                                                                            6.9 MB/s | 2.3 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:openssl11-libs-1.1.1k-5.el7.x86_64                                                          1/4 
  Installing : 1:nginx-filesystem-1.20.1-10.el7.noarch                                                       2/4 
  Installing : gperftools-libs-2.6.1-1.el7.x86_64                                                            3/4 
  Installing : 1:nginx-1.20.1-10.el7.x86_64                                                                  4/4 
  Verifying  : gperftools-libs-2.6.1-1.el7.x86_64                                                            1/4 
  Verifying  : 1:nginx-filesystem-1.20.1-10.el7.noarch                                                       2/4 
  Verifying  : 1:nginx-1.20.1-10.el7.x86_64                                                                  3/4 
  Verifying  : 1:openssl11-libs-1.1.1k-5.el7.x86_64                                                          4/4 

Installed:
  nginx.x86_64 1:1.20.1-10.el7                                                                                   

Dependency Installed:
  gperftools-libs.x86_64 0:2.6.1-1.el7                  nginx-filesystem.noarch 1:1.20.1-10.el7                 
  openssl11-libs.x86_64 1:1.1.1k-5.el7                 

Complete!
[root@VM-8-17-centos ~]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-Switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
[root@VM-8-17-centos ~]# rpm -ql nginx 
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
/usr/bin/nginx-upgrade
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx/modules
/usr/sbin/nginx
/usr/share/doc/nginx-1.20.1
/usr/share/doc/nginx-1.20.1/CHANGES
/usr/share/doc/nginx-1.20.1/README
/usr/share/doc/nginx-1.20.1/README.dynamic
/usr/share/doc/nginx-1.20.1/UPGRADE-NOTES-1.6-to-1.10
/usr/share/licenses/nginx-1.20.1
/usr/share/licenses/nginx-1.20.1/LICENSE
/usr/share/man/man3/nginx.3pm.gz
/usr/share/man/man8/nginx-upgrade.8.gz
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx/html/404.html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/en-US
/usr/share/nginx/html/icons
/usr/share/nginx/html/icons/poweredby.png
/usr/share/nginx/html/img
/usr/share/nginx/html/index.html
/usr/share/nginx/html/nginx-logo.png
/usr/share/nginx/html/poweredby.png
/usr/share/nginx/modules
/usr/share/vim/vimfiles/ftdetect/nginx.vim
/usr/share/vim/vimfiles/ftplugin/nginx.vim
/usr/share/vim/vimfiles/indent/nginx.vim
/usr/share/vim/vimfiles/syntax/nginx.vim
/var/lib/nginx
/var/lib/nginx/tmp
/var/log/nginx
/var/log/nginx/access.log
/var/log/nginx/error.log
[root@VM-8-17-centos nginx]# rpm -ql nginx | grep bin 
/usr/bin/nginx-upgrade
/usr/sbin/nginx
[root@VM-8-17-centos nginx]# nginx -h
nginx version: nginx/1.20.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/share/nginx/)
  -e filename   : set error log file (default: /var/log/nginx/error.log)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file

[root@VM-8-17-centos nginx]# netstat -tnlp | grep 80 
[root@VM-8-17-centos nginx]# /usr/sbin/nginx 
[root@VM-8-17-centos nginx]# netstat -tnlp | grep 80 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      5146/nginx: master  
tcp6       0      0 :::80                   :::*                    LISTEN      5146/nginx: master  
[root@VM-8-17-centos nginx]# ps -ef | grep nginx 
root      5146     1  0 23:19 ?        00:00:00 nginx: master process /usr/sbin/nginx
nginx     5147  5146  0 23:19 ?        00:00:00 nginx: worker process
nginx     5148  5146  0 23:19 ?        00:00:00 nginx: worker process
nginx     5149  5146  0 23:19 ?        00:00:00 nginx: worker process
nginx     5150  5146  0 23:19 ?        00:00:00 nginx: worker process
root      5281  1769  0 23:20 pts/0    00:00:00 grep --color=auto nginx
[root@VM-8-17-centos nginx]# kill -9 5146 
[root@VM-8-17-centos nginx]# ps -ef | grep nginx 
nginx     5147     1  0 23:19 ?        00:00:00 nginx: worker process
nginx     5148     1  0 23:19 ?        00:00:00 nginx: worker process
nginx     5149     1  0 23:19 ?        00:00:00 nginx: worker process
nginx     5150     1  0 23:19 ?        00:00:00 nginx: worker process
root      5406  1769  0 23:20 pts/0    00:00:00 grep --color=auto nginx
[root@VM-8-17-centos nginx]# kill -9 5147 5148 5149 5150 
[root@VM-8-17-centos nginx]# ps -ef | grep nginx 
root      5671  1769  0 23:21 pts/0    00:00:00 grep --color=auto nginx
[root@VM-8-17-centos nginx]# /usr/sbin/nginx 

[root@VM-8-17-centos nginx]# pwd 
/var/log/nginx
[root@VM-8-17-centos nginx]# ll
total 4
-rw-r--r-- 1 root root    0 Jun 21 23:19 access.log
-rw-r--r-- 1 root root 1007 Jun 21 23:23 error.log
[root@VM-8-17-centos nginx]# cat error.log 
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to [::]:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to [::]:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to [::]:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to [::]:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to [::]:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: still could not bind()

[root@VM-8-17-centos nginx]# curl 127.0.0.1 
[root@VM-8-17-centos nginx]# cat access.log 
127.0.0.1 - - [21/Jun/2023:23:24:31 +0800] "GET / HTTP/1.1" 200 4833 "-" "curl/7.29.0" "-"
127.0.0.1 - - [21/Jun/2023:23:24:48 +0800] "GET / HTTP/1.1" 200 4833 "-" "curl/7.29.0" "-"
[root@VM-8-17-centos nginx]# 

基于源码模式

下载地址:http://nginx.org/en/download.html

版本说明

Mainline version:在线版本,正处于开发状态

Stable version :稳定版本(一般下载使用)

Legacy version :遗留版本,遗留的老的版本

mkdir -p /server/download/nginx
cd /server/download/nginx
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -xzvf nginx-1.24.0.tar.gz

源码结构

drwxr-xr-x 6 1001 1001   4096 2023-07-31 15:32 auto
-rw-r--r-- 1 1001 1001 323312 2023-04-11 09:45 CHANGES
-rw-r--r-- 1 1001 1001 494234 2023-04-11 09:45 CHANGES.ru
drwxr-xr-x 2 1001 1001   4096 2023-07-31 15:32 conf
-rwxr-xr-x 1 1001 1001   2611 2023-04-11 09:45 configure
drwxr-xr-x 4 1001 1001   4096 2023-07-31 15:32 contrib
drwxr-xr-x 2 1001 1001   4096 2023-07-31 15:32 html
-rw-r--r-- 1 1001 1001   1397 2023-04-11 09:45 LICENSE
drwxr-xr-x 2 1001 1001   4096 2023-07-31 15:32 man
-rw-r--r-- 1 1001 1001     49 2023-04-11 09:45 README
drwxr-xr-x 9 1001 1001   4096 2023-07-31 15:32 src
  1. src:这是Nginx源码的根目录,包含了Nginx的所有源文件。
  2. core:该目录下存放了Nginx的核心源码,包括定义常用数据结构、体系结构实现等的文件。
  3. event:该目录下存放了Nginx的事件系统源码。
  4. http:该目录下存放了Nginx的HTTP服务器实现源码。
  5. mail:该目录下存放了Nginx的邮件服务器实现源码,主要实现对邮件服务依赖的数据结构的定义和初始化。
  6. misc:该目录下存放了一些其他文件,包括测试程序中引用的头文件是否与C++兼容的文件,以及支持Google PerTools的文件等。
  7. os:该目录下包含了针对不同操作系统下的函数进行封装以及实现核心调用的源码。
  8. auto:该目录下存放了一些自动编译安装相关的脚本和配置文件。
  9. conf:该目录下存放了Nginx的配置文件以及fcgi相关的配置等文件。
  10. contrib:该目录下存放了一些实用的工具,如geo配置生成工具等。
  11. html:该目录下存放了默认的网页文件。
  12. man:该目录下存放了Nginx的man手册。

添加nginx用户

useradd -s /sbin/nologin nginx -M

编译nginx

# 安装 所需依赖
yum install -y wget gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel

# 配置 nginx
# --prefix=<path>:定义服务器文件的完整路径,该路径同时也是configure命令设置的相对路径(除类库源文件外)以及nginx.conf文件定义的相对路径的基准路径。其默认值是/usr/local/nginx。
# --sbin-path=<path>:设置nginx可执行文件的完整路径,该路径仅在安装期间使用,默认路径为prefix/sbin/nginx。
# --conf-path=<path>:设置配置文件nginx.conf的完整路径。如有必要,总是可以在nginx启动时通过命令行参数-cfile指定一个不同的配置文件路径。默认路径为prefix/conf/nginx.conf。
# --pid-path=<path>:设置nginx.pid文件的完整路径,该文件存储了主进程的进程ID。安装完成后,该路径总是可以在nginx.conf文件中用pid指令来修改。默认路径为prefix/logs/nginx.pid。
# --error-log-path=<path>:设置记录主要错误、警告以及调试信息日志的完整路径。安装完成后,该路径总是可以在nginx.conf文件中用error_log指令来修改。默认路径为prefix/logs/error.log。
# --http-log-path=<path>:设置记录HTTP服务器主要请求日志的完整路径。安装完成后,该路径总是可以在nginx.conf文件用access_log指令来修改。默认路径为prefix/logs/access.log。
# --user=name:设置工作进程以某非特权用户的身份运行。安装完成后,该用户名总是可以在nginx.conf文件用user指令来修改。其默认值为nobody。
# --group=name:设置工作进程以某非特权用户的身份运行。安装完成后,该用户名总是可以在nginx.conf用user指令来修改。其默认名称与未授权用户名称相同。

# 在Nginx的配置中,“with”和“without”参数用于指定是否包含特定的模块或功能。使用“with”参数可以启用某个模块或功能,使用“without”参数可以禁用某个模块或功能
./configure --user=nginx  \
            --group=nginx \
            --prefix=/server/application/nginx \
            --conf-path=/etc/nginx/nginx.conf  \
            --error-log-path=/var/log/nginx-error.log  \
            --pid-path=/var/run/nginx.pid \
            --with-http_stub_status_module  \
            --with-http_ssl_module \
            --with-pcre \
# 编译并且安装            
make && make install
# 配置环境变量
echo '#nginx config.'>>/etc/profile
echo 'export PATH=$PATH:/server/application/nginx/sbin'>> /etc/profile
source /etc/profile
# 配置nginx语法高亮
cp -r contrib/vim/ ~/.vim/
cat >~/.vim/filetype.vim <<EOF
au BufRead,BufNewFile /etc/nginx/* set ft=nginx
EOF
nginx -V

nginx 命令行参数

[root->mcode-server->/server/download/nginx/nginx-1.24.0]# nginx -h
nginx version: nginx/1.24.0
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
             [-e filename] [-c filename] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /server/application/nginx/)
  -e filename   : set error log file (default: /var/log/nginx-error.log)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file
  • -c--conf-file:指定配置文件的路径。
  • -t--test-config:测试配置文件的语法是否正确。
  • -p--prefix:设置Nginx的安装目录。
  • -s--signal:向主进程发送信号。
  • -v--version:显示Nginx的版本信息。
  • -V--verbose:显示Nginx的版本、编译器版本和配置参数。
  • -g--global:设置全局配置指令。
返回顶部
顶部