yum 官方源安装:
# 1. 需要预先安装 yum-utils
sudo yum install yum-utils
# 2. 配置yum repo
touch /etc/yum.repos.d/nginx.repo
ngix.repo:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
默认,nginx 稳定版本被启用,如果想用 mainline package,使用以下命令:
sudo yum-config-manager --enable nginx-mainline
安装成功:
防火墙放行http:
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
firewall-cmd --reload
阿里云ECS,还要配置安全组。
关于 centos7 防火墙配置:
# 启动
systemctl start firewalld
# 设置开机启动
systemctl enable firewalld.service
# 查看防火墙状态
firewall-cmd --state
# 重载配置
firewall-cmd --reload
# 列出支持的zone
firewall-cmd --get-zones
# 列出支持的服务
firewall-cmd --get-services
# 查看FTP服务是否支持
firewall-cmd --query-service ftp
# 临时开放ftp
firewall-cmd --add-service=ftp
# 永久移除ftp
firewall-cmd --add-service=ftp --permanent
# 永久放行80
firewall-cmd --add-port=80/tcp --permanent
# 查看帮助
man firewall-cmd
参考:https://daishenghui.club/2022/11/15/categories/Linux/CentOS7%E7%BC%96%E8%AF%91%E5%AE%89%E8%A3%85nginx/
https://www.tecmint.com/install-nginx-from-source/
安装编译工具:
yum -y install gcc gcc-c++ make zlib-devel pcre-devel openssl-devel
下载源码包:
cd /usr/local
wget https://nginx.org/download/nginx-1.24.0.tar.gz
#解压
tar xfz nginx-1.24.0.tar.gz
cd nginx-1.24.0
ls -all
nginx模块地址:http://wiki.nginx.org/Modules.
配置:
./configure --user=nginx --group=nginx --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_ssl_module --with-pcre
编译:
make
# 上一步完成后,执行下面
make install
添加nginx用户:
useradd -d /etc/nginx/ -s /sbin/nologin nginx
修改配置文件:
vi /etc/nginx/nginx.conf
修改两个地方:
创建网站目录:
mkdir -p /var/www/html
# 启动nginx
/usr/sbin/nginx
查看进程及端口:
netstat -tulpn | grep nginx
配置防火墙:
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
systemctl restart firewalld
nginx常用命令:
/usr/sbin/nginx -V [show Nginx modules and configurations]
/usr/sbin/nginx -h [help options]
/usr/sbin/nginx -t [check configuration file]
/usr/sbin/nginx [start Nginx process]
/usr/sbin/nginx -s stop [stop Nginx process]
/usr/sbin/nginx -s reload [reload Nginx process]
配置 nginx 服务
vi /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
首先前面启动了nginx,需要先关闭,否则启动不了
netstat -tulpn | grep nginx
kill 2626
# 启动
systemctl start nginx
systemctl status nginx