[TOC] ### 第一步 下载 nginx 包放置在指定目录下一般放置 mnt 目录 下载地址 :[nginx.org/en/download.html](http://nginx.org/en/download.html) ![](https://img.kancloud.cn/4d/6e/4d6eb4ce8ebefb36043c6f8f2bb721ce_594x246.png) 解压 nginx 压缩包 ``` tar zxvf nginx-1.19.7.tar.gz ``` 准备依赖包: ``` yum -y install zlib-devel pcre-devel openssl-devel ``` 准备 nginx 用户: ``` useradd -M -s /sbin/nologin nginx ``` 源码编译与安装: ``` ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_random_index_module --with-http_sub_module ``` 安装 ``` make && make install ``` ## **nginx 配置** 设置配置文件: /usr/local/nginx/nginx.conf ``` user nginx; ``` ![](https://img.kancloud.cn/14/46/14468791180d386970738569cf913db5_383x266.png) 服务管理: 1. 检测配置语法 ``` /usr/local/nginx/sbin/nginx -t ``` 2. 启动服务 /usr/local/nginx/sbin/nginx 3. 重载服务 ``` /usr/local/nginx/sbin/nginx -s reload pkill -HUP nginx ``` 4. 关闭服务 ``` /usr/local/nginx/sbin/nginx -s stop pkill nginx ``` 5. 开机启动 ``` vi /etc/rc.local /usr/local/nginx/sbin/nginx ``` ![](https://img.kancloud.cn/18/1c/181c90454c2df116dc582f9fd247168b_422x273.png) 进程管理: ``` pstree |grep nginx ps -ef |grep nginx ps aux |grep nginx ``` 端口管理: ``` netstat -tunpl |grep nginx ``` 客户端测试:(输入你的公网 IP) ``` http://192.168.2.1 ``` nginx 重载 ``` [root@wml sbin]# pkill -HUP nginx ``` ## **补充** > 很多情况下 我们需要执行 nginx -t , -s reload, restart , close ,start 等命令 > 这时候我们需要 执行完整命令 usr/local/nginx/sbin/nginx -t 太麻烦了 我们来配置一个全局的nginx变量 **首先 我们找到目录 /etc/rc.d/init.d** **然后创建 nginx文件** ![](https://img.kancloud.cn/7b/d6/7bd62dd040b746800d7ac5a6a0d6d228_459x257.png) ~~~ #!/bin/sh # nginx - this script starts and stops the nginx daemin # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/local/nginx/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" lockfile=/var/lock/subsys/nginx start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac ~~~ **复制上面代码到 新建的nginx文件中** 编辑文件 vim /etc/profile 在尾部添加如下 ``` PATH=$PATH:/usr/local/nginx/sbin #安装nginx的sbin目录 export PATHi ``` ![](https://img.kancloud.cn/ef/ad/efad7ca22b040f0e5a35dcad113262c1_527x167.png) 最后执行 ![](https://img.kancloud.cn/13/29/13292f5f8fea696a055b7aa8d84d1e52_604x80.png)