安装 Nginx
-
下载 Nginx.Nginx官网:http://nginx.org/
推荐:下载地址: nginx.org/download/ng… 进入到
usr
中的src
,把刚才下载的安装包拉进去[root@localhost src]# cd /usr/src/
-
解压安装包、
[root@localhost src]# tar zxvf nginx-1.6.2.tar.gz
-
进入安装包目录
[root@localhost src]# cd nginx-1.6.2
-
编译安装
[root@localhost nginx-1.6.2]# ./configure
[root@localhost nginx-1.6.2]# make
[root@localhost nginx-1.6.2]# make install
技巧
使用
prefix
选项指定安装的目录## ./configure --prefix=[安装程序至哪个目录] [root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx
警告或报错
-
报错提示
-
安装
gcc
(c语言编辑器)yum install -y gcc //不加-y 会提示是否安装
-
-
报错提示!
-
安装
peri
库yum install -y pcre pcre-devel
-
-
报错提示
-
安装
zlib
库yum install -y zlib zlib-devel
-
-
接下来执行
./configure --prefix=/usr/local/nginx make make install
启动Nginx
进入安装好的目录 /usr/local/nginx/sbin
./nginx 启动
./nginx -s stop 快速停止
./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求
./nginx -s reload 重新加载配置
查看Nginx 状态
-
查看
Nginx
状态ps -ef|grep nginx
-
启动时:
root 14016 1 0 11:19 ? 00:00:00 nginx: master process ./nginxnobody 14017 14016 0 11:19 ? 00:00:00 nginx: worker processroot 14098 1262 0 14:24 pts/2 00:00:00 grep --color=auto ngin
-
停止时
ps -ef|grep nginxroot 14101 1262 0 14:25 pts/2 00:00:00 grep --color=auto nginx
-
杀死进程
方法四: killall
通过程序的名字, 直接杀死所有进程
killall nginx
关于防火墙
-
先检查防火墙的状态
firewall-cmd --state
注意
==runing 表示开启,not runing 表示关闭 -
查看已配置规则
firewall-cmd --list-all
-
关闭防火墙
systemctl stop firewalld.service
-
禁止防火墙开机启动
systemctl disable firewalld.service
-
放行端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
-
重启防火墙
firewall-cmd --reload
注册系统服务
通过系统服务的方式启动
nginx
-
创建服务脚本
vi /usr/lib/systemd/system/nginx.service
-
服务脚本内容
注意
安装nginx
路径是否一致[Unit] Description=nginx - web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop ExecQuit=/usr/local/nginx/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
-
重新加载系统服务
daemon-reload: 重新加载某个服务的配置文件,如果新安装了一个服务,归属于 systemctl 管理,要是新服务的服务程序配置文件生效,需重新加载。
systemctl daemon-reload
-
启动
nginx
服务systemctl start nginx.service
-
查看启动
nginx
状态systemctl status nginx
● nginx.service - nginx Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since 三 2022-07-20 14:37:40 CST; 14s ago Process: 14133 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS) Process: 14132 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS) Main PID: 14135 (nginx) CGroup: /system.slice/nginx.service ├─14135 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf └─14136 nginx: worker process7月 20 14:37:40 Nginx systemd[1]: Starting nginx...7月 20 14:37:40 Nginx nginx[14132]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok7月 20 14:37:40 Nginx nginx[14132]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful7月 20 14:37:40 Nginx systemd[1]: Failed to read PID from file /usr/local/nginx/logs/nginx.pid: Invalid argument7月 20 14:37:40 Nginx systemd[1]: Started nginx.
-
开机启动
systemctl enable nginx.service
配置静态 ip
之前的网络配置是使用dhcp方式分配ip地址,这种方式会在系统每次联网的时候分配一个ip给我们用,也就是说有可能系统下次启动的时候ip会变,这样非常不方便我们管理
-
配置静态
ip
首先打开网卡配置文件cd /etc/sysconfig/network-scripts
-
修改启动协议
BOOTPROTO=static
-
打开
ifcfg-ensxx
文件夹 手动配置ip
地址IPADDR=192.168.44.101 NETMASK=255.255.255.0 GATEWAY=192.168.44.1![Snipaste_2022-10-31_09-21-13](img/Snipaste_2022-10-31_09-21-13.png) DNS1=8.8.8.8
根据大家自己的环境,ip地址可能略有不同
-
接下来重启网络服务
systemctl restart network
重启之后 xshell可能无响应,这是因为ip变了,修改xshell配置中的ip重新连接即可。
-
完整配置截图如下
TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=10ac735e-0b8f-4b19-9747-ff28b58a1547 DEVICE=ens33 ONBOOT=yes IPADDR=192.168.44.101 NETMASK=255.255.255.0 GATEWAY=192.168.44.1 DNS1=8.8.8.8
配置多个ip
-
切换目录:
cd /etc/sysconfig/network-scripts/
-
复制一个
ifcfg-ens192
的文件,新的文件名为ifcfg-ens192:0
,然后用vi
编辑此文件。TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static //使用静态ip地址 DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens192:0 UUID=10ac735e-0b8f-4b19-9747-ff28b58a1547 DEVICE=ens192:0 //虚拟网络接口,随意 ONBOOT=yes //系统启动时激活 IPADDR=192.168.44.101 //该虚拟网络接口的ip别名,随意 NETMASK=255.255.255.0 //子网掩码,对应ip别名 GATEWAY=192.168.44.1 //网关,对应ip别名 HWADDR=00:10:5A:5E:B1:E4 //网卡MAC地址,无需更改 USERCTL=no //是否给予非root用户设备管理权限
-
接下来重启网络服务
systemctl restart network
重启之后 xshell可能无响应,这是因为ip变了,修改xshell配置中的ip重新连接即可。