一、实验环境

主机名 IP地址 备注
nginx01 192.168.10.3 nginx-server (nginx版本:nginx-1.8.0)
client01 192.168.10.4 client

二、nginx安装包下载

2.1 通过命令下载到本地

  1. cd /opt/
  2. wget -c http://nginx.org/download/nginx-1.18.0.tar.gz

2.2 下载到win上,上传到linux上

image.png

三、nginx安装

3.1 nginx依赖包安装

  1. yum -y install gcc gcc-c++ autoconf automake libtool make cmake
  2. yum -y install zlib zlib-devel openssl openssl-devel pcre-devel gd-devel

3.2 解压安装包

  1. cd /opt
  2. tar -zxvf nginx-1.18.0.tar.gz

3.3 新建nginx用户及用户组

  1. groupadd nginx
  2. useradd -M -g nginx -s /sbin/nologin nginx
  3. # -M 表示没有home目录; -g 指定组; -s /sbin/nologin 指定shell为/sbin/nologin不能登入

3.4 编译配置、编译、安装

  1. # 进入解压好的目录
  2. cd /opt/nginx-1.8.0
  3. # 编译配置
  4. # 解释:--prefix=路径 --user=进程用户 --group=进程组
  5. # --with-http_ssl_module 可以构建一个将HTTPS协议支持添加到HTTP服务器的模块
  6. # --with-http_realip_module 禁用构建一个允许HTTP服务器重定向请求并更改请求URI的模块
  7. ./configure --prefix=/usr/local/nginx \
  8. --user=nginx --group=nginx \
  9. --with-debug --with-http_ssl_module \
  10. --with-http_realip_module \
  11. --with-http_image_filter_module \
  12. --with-http_gunzip_module \
  13. --with-http_gzip_static_module \
  14. --with-http_stub_status_module \
  15. --http-log-path=/var/log/nginx/access.log \
  16. --error-log-path=/var/log/nginx/error.log
  17. # 编译
  18. make
  19. #安装
  20. make install

3.5 nginx路径优化,及systemctl管理

  1. # 1. 命令路径优化
  2. ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
  3. # 2. systemctl管理
  4. cat >> /etc/init.d/nginx < OFF
  5. #!/bin/bash
  6. # chkconfig: - 99 20
  7. # description: nginx server
  8. PROG="/usr/local/nginx/sbin/nginx"
  9. PID="/usr/local/nginx/logs/nginx.pid"
  10. case "$1" in
  11. start) $PROG ;;
  12. stop) kill -s QUIT $(cat $PID) ;;
  13. restart)
  14. $0 stop
  15. $0 start
  16. ;;
  17. reload) $PROG -s reload ;;
  18. *)
  19. echo "usage: $0 {start|stop|restart|reload}"
  20. exit 1
  21. ;;
  22. esac
  23. exit 0
  24. chmod +x /etc/init.d/nginx
  25. chkconfig --add nginx

四、管理nginx

4.1 查看nginx版本号

nginx -v

4.2 启动|关闭|重启|平滑重启 nginx

systemctl start|stop|restart|reload nginx

4.3 查看nginx进程

netstat -antlp |grep 80

4.4 nginx配置文件

vim /usr/local/nginx/conf/nginx.conf

五、测试nginx服务

# 通过在192.168.10.4上使用curl命令测试
curl 192.168.10.3