准备
-
下载nginx安装包
[nginx: download](http://nginx.org/en/download.html) -
本次使用版本为:nginx-1.16.1.tar.gz
-
部署的系统版本信息(CentOS Linux release 7.9.2009 (Core))
Linux version 3.10.0-1160.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ) #1 SMP Mon Oct 19 16:18:59 UTC 2020 -
所需依赖环境提前上传(eg:/home/dagl/soft)
gcc.zip zlib-1.2.11.tar.gz openssl-fips-2.0.10.tar.gz pcre-8.35.tar.gz -
安装前置环境
unzip gcc.zip cd /home/dagl/soft/gcc4.8.5-36 rpm -Uvh *.rpm --nodeps --force #检查版本 gcc -v g++ -v#编译安装 openssl cd /home/dagl/soft/ tar zxvf openssl-fips-2.0.10.tar.gz cd openssl-fips-2.0.10#编译安装 pcre cd /home/dagl/soft tar zxf pcre-8.42.tar.gz cd pcre-8.42 ./configure && make && make install #查看pcre版本 pcre-config --version#安装zlib cd /home/dagl/soft/ tar zxf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure && make && make install
开始
1. 上传nginx安装包至服务器 解压 (上传路径:/usr/local/)
tar zxvf nginx-1.16.1.tar.gz
2. 进入到nginx文件下(cd nginx-1.16.1)
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx -–with-http_stub_status_module --with-http_ssl_module
#命令解释
##源码安装一般由3个步骤组成:配置(configure)、编译(make)、安装(make install)
./configure #可执行脚本
--prefix=/usr/local/nginx #把所有的资源文件放在/usr/local/nginx路径中,不会杂乱
--user=nginx #指定用户
--group=nginx #指定用户组
-–with-http_stub_status_module \ #启用状态统计
-with-http_ssl_module #启用SSL模块,提供SSL加密功能
如果出现此报错(缺少库):
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
解决:
yum -y install pcre-devel
3. 安装命令
make && make install
4. 添加环境变量
vi /etc/profile
export PATH=$PATH:/usr/local/nginx/sbin
source /etc/profile
5. 查看版本
nginx -V
nginx version: nginx/1.16.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 --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module
6. 配置启动服务
vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
7. 重新加载并启动
#重新加载配置文件
systemctl daemon-reload
#启动nginx服务
systemctl start nginx.service
#设置开机启动
systemctl enable nginx.service
#查看运行状态
systemctl status nginx
如出现报错(nginx: [emerg] getpwnam("nginx") failed):
Job for nginx.service failed because the control process exited with error code.See "systemctl status nginx.service" and "journalctl -xe" for details.
-----------
nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; static; vendor preset:disabled)
Active: failed (Result: exit-code) since 三 2022-08-24 14:19:02 CST; 5s ago
Process: 25025 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=1/FAILURE)
8月 24 14:19:02 localhost systemd[1]: Starting nginx - high performance we.....
8月 24 14:19:02 localhost nginx[25025]: nginx: [emerg] getpwnam("nginx") failed
8月 24 14:19:02 localhost systemd[1]: nginx.service: control process exite...=1
8月 24 14:19:02 localhost systemd[1]: Failed to start nginx - high perform...r.
8月 24 14:19:02 localhost systemd[1]: Unit nginx.service entered failed state.
8月 24 14:19:02 localhost systemd[1]: nginx.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
解决(创建nginx用户):
useradd -s /sbin/nologin -M nginx
8. 权限不够
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
9. 新建日志目录执行
mkdir /var/log/nginx
补充
安装过程出现问题
- 可能缺少GCC和OpenSSL
yum install gcc-c++
yum install opensll openssl-devel
- 安装zlib库(zlib 库提供了很多种压缩和解压缩的⽅式, nginx 使⽤ zlib 对 http 包的内容进⾏ gzip ,所以需要在 Centos 上安装 zlib 库)
yum install -y zlib zlib-devel