官网文档
http://openresty.org/cn/installation.html#安装前的准备
- 您必须将这些库
perl 5.6.1+,libpcre,libssl安装在您的电脑之中。 对于 Linux来说, 您需要确认使用ldconfig命令,让其在您的系统环境路径中能找到它们。yum install pcre-devel openssl-devel gcc curl```shell方式2 我常用
http://openresty.org/cn/linux-packages.html#centosadd the yum repo:
wget https://openresty.org/package/centos/openresty.repo sudo mv openresty.repo /etc/yum.repos.d/
update the yum index:
sudo yum check-update
sudo yum install -y openresty
方式一
http://openresty.org/cn/download.html
wget https://openresty.org/download/openresty-1.19.3.1.tar.gz tar -xzvf openresty-VERSION.tar.gz cd openresty-VERSION/ ./configure make sudo make install
<a name="NvCJI"></a># [安装参考文档](https://www.cnblogs.com/freeweb/p/13446663.html)<a name="xkPah"></a># 配置全局环境> 无权限请加 sudo- 编辑 `vim /etc/profile`- 在profile文件底部加上 `export PATH=$PATH:/usr/local/openresty/nginx/sbin`- `echo "export PATH=$PATH:/usr/local/openresty/nginx/sbin">>/etc/profile`- 使配置生效 `source /etc/profile`- 测试 `nginx -v`- 启动 `nginx`- 停止 `nginx -s stop`- 重启 `nginx -s reload`- 查看进程 `** ps -ef | grep nginx**`<a name="y6XyZ"></a># 启动错误处理<a name="Tun19"></a>## 启动文件夹无权限> sudo chmod -R 777 /usr/local/openresty```shell[vmware@kyw ~]$ nginxnginx: [alert] could not open error log file: open() "/usr/local/openresty/nginx/logs/error.log" failed (13: Permission denied)2022/10/26 08:48:24 [emerg] 32664#32664: mkdir() "/usr/local/openresty/nginx/client_body_temp" failed (13: Permission denied)
启动端口无权限
1024以下端口启动时需要root权限,所以sudo nginx即可
[vmware@kyw ~]$ nginxnginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
sudo nginx 无此命令
[vmware@kyw sbin]$ sudo nginxsudo: nginx: command not found
自启参考
自启脚本
vi(vim) /usr/lib/systemd/system/nginx.service
[Unit]Description=The nginx HTTP and reverse proxy serverAfter=network.target remote-fs.target nss-lookup.target[Service]Type=forkingPIDFile= /var/run/openresty.pidExecStartPre=/usr/bin/rm -f /var/run/openresty.pidExecStartPre=/usr/local/openresty/nginx/sbin/nginx -tExecStart=/usr/local/openresty/nginx/sbin/nginxExecReload=/bin/kill -s HUP $MAINPIDKillMode=processKillSignal=SIGQUITTimeoutStopSec=5PrivateTmp=true[Install]WantedBy=multi-user.target
保存,重新加载 systemd
systemctl daemon-reload
设置nginx服务开机自启动
systemctl enable nginx.service
nginx服务常用操作
#启动nginx服务systemctl start nginx.service// nginx#停止nginx服务systemctl stop nginx.service// nginx -s quit or nginx -s stop#重启nginx服务systemctl restart nginx.service// service nginx restart#重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)systemctl reload nginx.service// nginx -s reload# 检查配置是否正确nginx -t
不指定配置文件请使用默认的配置文件进行操作
- /usr/local/openresty/nginx/conf/nginx.conf
开/关防火墙
非root 记得加 sudo
centos firewall
# 开放443firewall-cmd --zone=public --add-port=443/tcp --permanent# 限制443firewall-cmd --zone=public --remove-port=443/tcp --permanent# 开发80firewall-cmd --zone=public --add-port=80/tcp --permanent# 限制80firewall-cmd --zone=public --remove-port=80/tcp --permanent# 重启firewall-cmd --reload# 查看开放端口列表firewall-cmd --list-ports
初始化细节处理
默认错误页面隐藏版本号
# nginx的http段添加server_tokens off;
自定义错误页面
error_page 404 /404.html;# 自己去html目录下创建个404.html文件去location = /404.html {# 指定404页面的目录root html;}

