安装前准备
##
yum -y install gcc gcc-c++ autoconf automake libtool make cmake
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
##
yum -y install wget httpd-tools vim
pcre 库 用于解析正则表达式
gcc-c++ gcc环境
openssl openssl-devel SSL 安全加密套接字协议 (http的安全传输 也就是 https)
安装
$ mkdir /chris
$ cd /chris
$ wget http://nginx.org/download/nginx-1.17.0.tar.gz
$ tar -zxvf nginx-1.17.0.tar.gz
$ cd /chris/nginx-1.17.0/
$ ./configure --prefix=/usr/local/nginx # 将nginx的安装路径设为/usr/local/nginx 并执行检测,为编译做准备
# < 如果没有出现./configure: error提示,表示当前环境可以安装nginx,执行make和make install编译nginx>
## 在当前文件目录执行make 和 make install
$ make # 对源代码进行编译
$ make install # 开始安装
# 设置开机自启
# <切换到/lib/systemd/system/目录,创建nginx.service文件vim nginx.service>
$ cd /lib/systemd/system/
$ vim nginx.service
# 设置nginx.service 自启配置
# 查看是否安装成功
$ /usr/local/nginx/sbin/nginx -v
$ systemctl start nginx.service # 启动nginx
## http_realip_module 获取真实ip
## http_ssl_module https加密
## pcre正则
## data/package/fastdfs/fastdfs-nginx-module-1.22/src/ fastdfs模块
./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_ssl_module --with-pcre --add-module=/data/package/fastdfs/fastdfs-nginx-module-1.22/src/
$ cd /usr/local/nginx/sbin/
# 启动
$ ./nginx
# 停止
$ ./nginx -s stop
# 关闭 (优雅的关闭方式)
$ ./nginx -s quit
# 重启
$ ./nginx -s reload
# 查看版本
$ ./nginx -v
## 检查nginx的配置文件是否正常
$ ./nginx -t
$ ps aux|grep nginx # 查看nginx进程
# 重启nginx 先停止 后启动
$ ./nginx -s quit
$ netstat -lntup |grep nginx ## 检查进程是否正常
$ systemctl start nginx.service #启动nginx
$ systemctl stop nginx.service #结束nginx
$ systemctl restart nginx.service # 重启nginx
$ systemctl enable nginx.service #设置开机自启动
$ systemctl disable nginx.service #停止开机自启动
$ systemctl status nginx.service #查看服务当前状态
$ systemctl list-units --type=service #查看所有已启动的服务
# nginx.service 自启配置
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# ./configure \
--prefix=/opt/runtime/nginx \
--sbin-path=/opt/runtime/nginx/sbin/nginx \
--conf-path=/opt/runtime/nginx/etc/nginx/nginx.conf \
--error-log-path=/opt/runtime/nginx/logs/error.log \
--http-log-path=/opt/runtime/nginx/logs/access.log \
--pid-path=/opt/runtime/nginx/run/nginx.pid \
--lock-path=/opt/runtime/nginx/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre=../pcre-8.42 \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.1a \
--with-debug \
--http-client-body-temp-path=/opt/runtime/nginx/client_body \
--http-proxy-temp-path=/opt/runtime/nginx/proxy \
--http-fastcgi-temp-path=/opt/runtime/nginx/fastcgi \
--http-uwsgi-temp-path=/opt/runtime/nginx/uwsgi \
补充命令
/usr/local/nginx/sbin/nginx -v 查看nginx版本
vim /usr/local/nginx/conf/nginx.conf 修改配置文件
ps -qa | grep nginx //查看nginx 版本
ps -af | grep nginx //查看端口信息
ps -A | grep nginx
//《非常重要》刷新防火墙
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT #开启80端口
/sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT #开启22端口
$ sudo firewall-cmd --permanent --zone=public --add-service=http # 允许http通信
$ sudo firewall-cmd --permanent --zone=public --add-service=https # 允许https通信
$ sudo firewall-cmd --reload # 重启防火墙
/etc/rc.d/init.d/iptables save #保存配置
/etc/rc.d/init.d/iptables restart #重启服务
查看已开放端口
/etc/init.d/iptables status
安装方式2:yum 安装
https://blog.csdn.net/qq_34856247/article/details/86538578
隐藏nginx版本号
配置文件中,http区段中插入“server_tokens off;”,重新载入配置文件
卸载
# 停止nginx
$ service nginx stop
# 删除Nginx的自动启动
$ chkconfig nginx off
# 删除nginx
$ rm -rf /usr/sbin/nginx
$ rm -rf /etc/nginx
$ rm -rf /etc/init.d/nginx
# 清除yum 安装的 nginx
$ yum remove nginx
安装问题解决
新装NGINX重启,出现错误 nginx: [error] open() “/usr/local/nginx/logs/nginx.pid”
解决办法:
方法一:
## 1. 查看 /var/run/nginx/ 目录是否存在 没有的话创建
$ mkdir /var/run/nginx/
## 2. 指定nginx启动的配置文件
$ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
## 重启nginx
$ ./nginx -s reload
设置成功之后在之前报错的路径下会生成 nginx.pid 文件
这是因为我是重装nginx的,之前nginx没有停掉,可以使用命令杀死nginx进程
##
$ ps -ef | grep nginx | grep -v grep | awk '{print $2}' | xargs kill -9
此时再-c 设置一下配置文件,之后再重启nginx
方法二:
在nginx.conf的配置文件中添加
pid /log/nginx/pid
其他
–prefix=/usr/local/nginx(指定安装路径)
–user=nginx --group=nginx(指定运行用户和组)
–with-http_stub_status_module(启用该模块以支持状态统计)
–with-http_ssl_module(启用SSL模块)
–with-http_flv_module(启用FLV模块,提供寻求内存使用基于时间的偏移量文件)
–with-http_gzip_static_module(预读gzip功能)
–with-stream_realip_module
–with-http_mp4_module
–with-stream (用来实现四层协议的转发、代理或者负载均衡等。比如在内网有一个mysql服务,想暴露到公网上去使用,就可以通过nginx代理的方式通过nginx来进行内网mysql的访问)
–with-http_stub_status_module (Nginx的客户端状态)
window 安装注意
windows nginx ssl 配置 (配置文件路径)
E://software//nginx-1.18.0//conf//cert//xx.key
参考
区分wsgi、uWSGI、uwsgi、php-fpm、CGI、FastCGI的概念
Nginx FastCGI的运行原理
Nginx基础之常用配置
ngx_stream_realip_module 模块 腾讯云文档