CentOS7 安装 Nginx 服务


CentOS 系统常见的 Nginx 安装方法有两种: YUM源码编译

环境介绍

系统版本

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.9.2009 (Core)

YUM 安装

首先要配置 Nginx 的 yum 仓库源

[root@localhost ~]# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安装

[root@localhost ~]# yum install -y nginx

查看 Nginx 相关文件

[root@localhost ~]# rpm -ql nginx
# logrotate 日志切割的 nginx 配置文件
/etc/logrotate.d/nginx
# nginx 主配置文件
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
# nginx system 管理脚本
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
# nginx 模块库
/usr/lib64/nginx
/usr/lib64/nginx/modules
# 暂时未知作用
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
# nginx 的可执行文件
/usr/sbin/nginx
/usr/sbin/nginx-debug
# nginx 帮助手册目录
/usr/share/doc/nginx-1.20.1
/usr/share/doc/nginx-1.20.1/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
# nginx 网站发布目录
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
# nginx 的缓存目录
/var/cache/nginx
# nginx 的日志目录
/var/log/nginx
# nginx 运行后产生的 pid 文件路径
/var/run/nginx.pid

查看 Nginx 版本,通过这个仓库安装的都是最新的版本。其他版本的仓库列表:http://nginx.org/packages/centos/7/x86_64/RPMS/

[root@localhost ~]# 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.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/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 --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --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-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

启动服务,并设置开机自启

[root@localhost ~]# systemctl start nginx.service && systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

编译安装

安装编译工具

[root@localhost ~]# yum install -y openssl openssl-devel zlib zlib-devel pcre pcre-devel make gcc gcc-c++

编译安装需要去官网下载源码包:http://nginx.org/en/download.html

[root@localhost ~]# wget http://nginx.org/download/nginx-1.20.1.tar.gz

解压完之后进入主目录,开始 configure 配置,这里的模块按需添加

./configure \
--prefix=/data/nginx_80 \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--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-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' \
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

参数详解

--prefix=path
# 指定 nginx 的主安装目录。如不指定,则使用默认安装目录:/usr/local/nginx
--sbin-path=path 
# 指定 nginx 可执行文件的目录。如不指定,则使用默认安装目录:prefix/sbin/nginx
--modules-path=path
# 指定 nginx 动态模块的安装目录。如不指定,则使用默认安装目录:prefix/modules
--conf-path=path
# 指定 nginx.conf 配置文件的路径。如不指定,则使用默认安装目录:prefix/conf/nginx.conf
--error-log-path=path
# 指定 error.log 异常日志的文件路径,后续也可通过 nginx.conf 修改其位置。如不指定,则使用默认安装目录:prefix/logs/error.log
--pid-path=path
# 指定存储 nginx 进程 id 的 nginx.pid 文件路径,后续也可通过 nginx.conf 修改其位置。如不指定,则使用默认安装目录:prefix/logs/nginx.pid
--lock-path=path
# 指定 nginx.lock 文件的路径,后续也可通过 nginx.conf 修改其位置。如不指定,则使用默认安装目录:prefix/logs/nginx.lock
--user=name
# 指定运行 nginx 工作进程的用户,后续可通过 nginx.conf 修改。如不指定,则使用默认用户:nobody
--group=name
# 指定运行 nginx 工作进程的用户组,后续可通过 nginx.conf 修改。如不指定,则默认使用 --user 的用户名
--with-http_ssl_module
# 激活 HTTPS 支持,需要系统有 OpenSSL 库
# 更多模块详见官方文档:http://nginx.org/en/docs/configure.html

配置结束,这里显示了 nginx 使用的库与安装目录

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

编译安装

[root@localhost nginx-1.20.1]# make && make install

安装完成后,查看 nginx 主目录

[root@localhost ~]# tree -C /usr/local/nginx/
/usr/local/nginx/
├── conf
│   ├── fastcgi.conf
│   ├── fastcgi.conf.default
│   ├── fastcgi_params
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types
│   ├── mime.types.default
│   ├── nginx.conf
│   ├── nginx.conf.default
│   ├── scgi_params
│   ├── scgi_params.default
│   ├── uwsgi_params
│   ├── uwsgi_params.default
│   └── win-utf
├── html
│   ├── 50x.html
│   └── index.html
├── logs
└── sbin
    └── nginx

4 directories, 18 files

软链接到系统命令

[root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

查看 Nginx 版本

[root@localhost ~]# 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.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_secure_link_module

编写 system 管理脚本

创建文件 /usr/lib/systemd/system/nginx.service 。使用 yum 安装方式会自动生成该文件。

[root@localhost ~]# vim /usr/lib/systemd/system/nginx.service

文件内容

[Unit]
Description=nginx
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /usr/local/nginx/logs/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /usr/local/nginx/logs/nginx.pid)"

[Install]
WantedBy=multi-user.target

启动服务,并设置开机自启

[root@localhost ~]# systemctl start nginx.service && systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

后续优化

  • vim 编辑 Nginx 配置文件时语法高亮
cd /usr/share/vim/vim74/syntax
wget -O nginx.vim https://vim.sourceforge.io/scripts/download_script.php?src_id=19394 --no-check-certificate

# 注:/etc/nginx/*,/usr/local/nginx/* 为 nginx 配置文件的目录,可根据自己安装情况修改
echo 'au BufNewFile,BufRead /usr/local/nginx/* setf nginx' >> /usr/share/vim/vim74/filetype.vim

おすすめ

転載: blog.csdn.net/qq_39680564/article/details/120091708
おすすめ