#### 1. 安装依赖包 nginx依赖ssl,rewrite,gzip 3个包,如果没有c++编译环境需要用下面命令安装。 `yum install gcc-c++` #### 2. 建立目录 先在home目录下建一个nginx文件夹。然后进入文件。(nginx、openssl、zlib、pcre直接下载到这个文件夹,解压也是解压到这个文件夹,都安装好后直接删除即可。) ~~~ cd home mkdir nginx cd nginx ~~~ #### 3. openssl库安装 ssl功能需要安装openssl库 ,官网:https://www.openssl.org。建立文件夹/alidata/library/做为这3个库的安装目录。统一放一个文件夹,日后如果想卸载,直接删除就可以。在library下面再建立openssl、zlib、pcre三个文件夹。做为那3个库的安装目录。 参考:https://blacksaildivision.com/how-to-install-openssl-on-centos 查看openssl版本 ``` openssl version which openssl ``` ``` wget https://www.openssl.org/source/openssl-1.1.1k.tar.gz yum install libtool perl-core pcre-devel zlib-devel -y #安装响应的组件 tar -zxvf openssl-1.1.1k.tar.gz cd openssl-1.1.1k ./config --prefix=/alidata/library/openssl --openssldir=/alidata/library/openssl shared zlib && make && make install ``` 添加新版本 ``` mv /usr/bin/openssl /usr/bin/opensslOld (旧版本可以删除) ln -s /alidata/library/openssl/bin/openssl /usr/bin/openssl ln -s /alidata/library/openssl/lib/libssl.so.1.1 /usr/lib64/ ln -s /alidata/library/openssl/lib/libcrypto.so.1.1 /usr/lib64/ ``` ``` vi /etc/profile.d/openssl.sh (添加启动脚本路径) # /etc/profile.d/openssl.sh pathmunge /alidata/library/openssl/bin ``` 执行命令使openssl新版本lib路径生效 ``` ldconfig ``` 重开shell #### 4. 安装zlib库 gzip模块需要安装zlib库,官网:https://www.zlib.net。(axel -n 后面的10是一次性建立10个连接下载) ~~~ axel -n 10 https://www.zlib.net/zlib-1.2.11.tar.gz tar -zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure --prefix=/alidata/library/zlib && make && make install ~~~ #### 5. 安装pcre库 rewrite模块需要pcre库,官网:https://www.pcre.org。 ~~~ axel -n 10 https://deac-ams.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz tar -zxvf pcre-8.45.tar.gz cd pcre-8.45 ./configure --prefix=/alidata/library/pcre && make && make install ~~~ #### 6. nginx的编译安装 下载nginx,nginx的官网是https://nginx.org/。我们直接下载最新版本的nginx-1.14.2。在/alidata下建立server,然后在server下面分别建立nginx、mysql、php。后面会分别把对应的软件安装到这几个文件夹里。(--prefix:nginx的安装目录 ,--with-pcre:pcre的源码目录,--with-zlib和--with-openssl同理) ~~~ axel -n 10 https://nginx.org/download/nginx-1.20.1.tar.gz tar -zxvf nginx-1.20.1.tar.gz cd nginx-1.20.1 ./configure \ --prefix=/alidata/server/nginx \ --with-http_realip_module \ --with-http_sub_module \ --with-http_flv_module \ --with-http_dav_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-http_addition_module \ --with-pcre=/home/nginx/pcre-8.45 \ --with-openssl=/home/nginx/openssl-1.1.1k \ --with-openssl-opt='enable-tls1_3 enable-weak-ssl-ciphers' \ --with-http_ssl_module \ --with-http_v2_module \ --with-stream_ssl_module \ --with-zlib=/home/nginx/zlib-1.2.11 && make && make install ~~~ ![](https://box.kancloud.cn/f12e32e64d49d4f1d179bdc823bec954_1918x1013.png =770x400) ![](https://box.kancloud.cn/e7c94bc0d57782c88cfc126595277657_1917x1009.png =770x400) ![](https://box.kancloud.cn/fb769711a6d0b36e6d4a4088458c0dff_778x725.png) ![](https://box.kancloud.cn/9b2e99afc01e42595a1635ebe932697e_780x161.png) ![](https://box.kancloud.cn/afe787b404b2de4f9441a19b62b09621_779x136.png) #### 7. 检查nginx是否安装成功 检查nginx是否安装成功,如果出现下图信息,表示安装成功。 ~~~ cd /alidata/server/nginx/sbin ./nginx -t ~~~ ![](https://box.kancloud.cn/865ea7eb8cc66c6a957c3adeeec56ab6_780x104.png) ![](https://box.kancloud.cn/e07c544ac1f0b30823eeca19b2febc08_780x309.png) #### 8. 常用命令 nginx的几个常用命令: 查看Nginx的版本号:./nginx -v 启动Nginx:./nginx 快速停止或关闭Nginx:./nginx -s stop 正常停止或关闭Nginx:./nginx -s quit 配置文件修改重装载命令:./nginx -s reload #### 9. 将nginx加入系统命令 vi /etc/init.d/nginx 加入下面代码 ~~~ #!/bin/bash #nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/alidata/server/nginx/sbin/nginx nginx_config=/alidata/server/nginx/conf/nginx.conf nginx_pid=/alidata/server/nginx/run/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ [${NETWORKING} = "no"] ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /alidata/server/nginx/run/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL ~~~ 保存上面的代码。然后添加到系统服务中。 ~~~ chmod 755 /etc/init.d/nginx chkconfig --add nginx ~~~ #### 10. 在系统服务目录中创建nginx.service文件 `vi /usr/lib/systemd/system/nginx.service` 加入下面的代码 ~~~ [Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/alidata/server/nginx/sbin/nginx ExecReload=/alidata/server/nginx/sbin/nginx -s reload ExecStop=/alidata/server/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target ~~~ 保存。再执行重加载 `systemctl daemon-reload` 设置开机启动 `systemctl enable nginx.service` ![](https://box.kancloud.cn/139d1bd6272fe6d138cf0c0f6d9c9e89_780x274.png) #### 10. nginx服务命令 配置好之后nginx就可以用系统服务的方式操作了。 service nginx start 启动nginx service nginx stop 关闭nginx service nginx restart 重启nginx service nginx reload 重新加载nginx ![](https://box.kancloud.cn/e22dda5676007bb140f50fb61060bf6d_779x150.png) ![](https://box.kancloud.cn/c257016fd720ebc8d2056812d64c2bae_1917x446.png =770x180) #### 11. 添加用户和组 `groupadd -r www && adduser -r -g www -s /bin/false -d /alidata/www -M www` 查看用户 `cat /etc/passwd` 查看组 `cat /etc/group`