想在 Alibaba Cloud Linux 3.2104 64位、CentOS 系统上安装 Nginx ,你得先去添加一个资源库,像这样:
vim /etc/yum.repos.d/nginx.repo
使用 vim 命令去打开 /etc/yum.repos.d/nginx.repo ,如果 nginx.repo 不存在,就会去创建一个这样的文件,打开以后按一下小 i 键,进入编辑模式,然后复制粘贴下面这几行代码,完成以后按 esc 键退出,再输入 :wq (保存并退出)
[nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=0enabled=1
完成以后,我们就可以使用 yum 命令去安装 nginx 了,像这样:
yum install nginx
安装好以后
测试
一下 nginx 服务:
service nginx status
应该会返回:
nginx is stopped (nginx 已停止)
如果正常已经运行则显示:
Redirecting to /bin/systemctl status nginx.service
● nginx.service – The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-02-12 17:27:35 CST; 3min 23s ago
Process: 3729 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 3727 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 3725 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 3730 (nginx)
Tasks: 5 (limit: 47654)
Memory: 2.9M
CGroup: /system.slice/nginx.service
├─3730 nginx: master process /usr/sbin/nginx
├─3731 nginx: worker process
├─3732 nginx: worker process
├─3733 nginx: worker process
└─3734 nginx: worker process
Feb 12 17:27:35 iZ6weirxjv2yyjhjcu0j2dZ systemd[1]: Starting The nginx HTTP and reverse proxy server…
Feb 12 17:27:35 iZ6weirxjv2yyjhjcu0j2dZ nginx[3727]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Feb 12 17:27:35 iZ6weirxjv2yyjhjcu0j2dZ nginx[3727]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Feb 12 17:27:35 iZ6weirxjv2yyjhjcu0j2dZ systemd[1]: Started The nginx HTTP and reverse proxy server.
再测试一下 nginx 的配置文件:
nginx -t
应该会返回:
nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful
… syntax is ok
,…
test is successful
,说明配置文件没问题,同时这个结果里你可以找到 nginx 的配置文件 nginx.conf 所在的位置。
运行 nginx 服务
运行服务,可以使用使用 service 命令,它可以启动(start),重启(restart),或停止服务(stop),比如要启动 nginx 服务:
service nginx start