centos 在線安裝 nginx
- 安裝nginx
參考文檔: http://nginx.org/en/linux_packages.html 中的RHEL/CentOS章節,按照步驟安裝repository。
sudo yum install yum-utils
sudo vi /etc/yum.repos.d/nginx.repo // 創建該文件並添加[nginx-stable] 和 [nginx-mainline]的內容保存
[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的stable穩定版
sudo yum install nginx
- 啟動nginx
[root@localhost ~] sudo service nginx start
Redirecting to /bin/systemctl start nginx.service
[root@localhost ~] netstat -ntlp | grep 80 // 沒有netstat自己安裝下 sudo yum install net-tools
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1589/nginx: master // nginx服務啟動,並且監聽在80端口
-
測試
在自己的電腦輸入 http://ip:80, 出現以下界面則說明安裝成功
-
如果出現了訪問失敗,則需要在訪問的電腦中進行如下檢測
telnet 192.168.0.101 80 // 查看nginx服務器的80端口能不能訪問。
-
如果不能訪問80端,查看服務器 selinux和firewall 是否開啟了
[root@localhost ~] sudo getenforce // 查看selinux是否開啟 enforcing // enforecing 則未開啟了 [root@localhost ~] vi /etc/selinux/config // 永久關閉selinux,打開/etc/selinux/config編輯 SELINUX = disabled // 將 SELINUX = enforcing 改為 disabled
可以重啟服務器生效。
-
給firewalld 添加 80 端口
[root@localhost ~] sudo systemctl status firewalld // 查看firewalld是否開啟了 firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2019-12-15 23:57:33 CST; 4min 36s ago Docs: man:firewalld(1) Main PID: 824 (firewalld) Tasks: 2 (limit: 4912) Memory: 35.4M CGroup: /system.slice/firewalld.service └─824 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid 12月 15 23:57:32 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon... 12月 15 23:57:33 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
出現了上面的說明firewalld已經開啟了。
[root@localhost ~] sudo firewall-cmd --zone=public --add-port=80/tcp --permanent //添加80端口 success [root@localhost ~] sudo systemctl restart firewalld // 重啟下firewalld
繼續測試,應該可以訪問成功了。
-