摘要

Nginx部署安装

介绍

下载

nginx: download
wget http://nginx.org/download/nginx-1.21.6.tar.gz

安装

准备

sudo yum install -y gcc pcre-devel zlib-devel openssl openssl-devel

Ubuntu安装
sudo apt install gcc
sudo apt install libpcre3 libpcre3-dev
sudo apt install zlib1g zlib1g-dev
sudo apt install openssl libssl-dev -y
yum install -y gcc gcc-c++
PCRE库
yum install -y pcre pcre-devel
zlib库
yum install -y zlib zlib-devel
OpenSSL库
yum install -y openssl openssl-devel
sudo yum install gcc pcre-devel zlib-devel openssl openssl-devel -y

tar zxvf nginx-1.21.6.tar.gz
cd nginx-1.21.6/
./configure —prefix=../nginx
make
make install

常见问题

  1. 试图运行Nginx命令时,报错:

image.png
原因:
执行命令行时所在目录不是nginx目录
解决:
在自动生成的nginx目录下执行./sbin/nginx

  1. 非root用户无法绑定80、443等端口

原因:
仅root用户可使用1024以内端口号,非root用户只能使用1024以上端口号
解决:

  1. 换端口号,修改nginx配置
  2. 使非root用户下安装的nginx拥有root权限

  3. 打开网页时,出现无响应问题

原因:
可能是防火墙问题
解决:

  1. ps aux |grepnginx
  2. netstat -ntlp
  3. firewall-cmd --zone=public --add-port=80/tcp --permanent
  4. systemctl restart firewalld.service

Image.png
Image.png
Image.png
Image.png
Image.png

root用户

步骤

解压

tar -zxvf nginx-1.19.8.tar.gz

配置

cd nginx-1.19.8/ ./configure

编译

make

安装

make install

检查是否安装成功

cd /usr/local/nginx/sbin/ ./nginx -t

image.png

非root用户

步骤

tar -zxvf nginx-1.19.8.tar.gz cd nginx-1.19.8/ ./configure —prefix=$HOME/nginx #指定安装路径 make make install cd ~/nginx/sbin/ ./nginx -t

报错

  1. nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)

    sudo cap_net_bind_service=+eip $HOME/nginx/sbin/nginx

  2. nginx: [warn] the “user” directive makes sense only if the master process runs with super-user privileges, ignored in /home/nginx/nginx/conf/nginx.conf:2

    原因:./nginx无权限启动(master需要root权限) chown root nginx # 设置为root用户 chmod +s nginx # 添加SUID权限

部署

查看服务

ps -ef | grep nginx | grep -v grep

启动

./nginx

停止

./nginx -s stop

重启

./nginx -s reload

开机自启动

vim /etc/rc.d/rc.local /usr/local/nginx/sbin/nginx # 添加

参考文档

  1. nginx.org/en/docs/
  2. www.nginx.cn/doc/
  3. https://cloud.tencent.com/developer/article/1555828
  4. https://blog.csdn.net/answer3664/article/details/120895815