centos7安裝nginx的兩種方法
阿新 • • 發佈:2018-12-09
第一種方式:通過yum安裝
直接通過 yum install nginx 肯定是不行的,因為yum沒有nginx,所以首先把 nginx 的源加入 yum 中
執行下面的命令:
1.將nginx放到yum repro庫中
[[email protected] ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2.檢視nginx資訊
[[email protected] ~]# yum info nginx
3.使用yum安裝ngnix
[ [email protected] ~]# yum install nginx
效果如下:
[[email protected] ~]# yum install nginx
已載入外掛:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.usc.edu
* extras: mirror.raystedman.net
* updates: mirror.metrocast.net
正在解決依賴關係
--> 正在檢查事務
---> 軟體包 nginx.x86_64.1 .1.10.1-1.el7.ngx 將被 安裝
······
······
正在安裝 : 1:nginx-1.10.1-1.el7.ngx.x86_64
Thanks for using nginx!
Please find the official documentation for nginx here:
* http://nginx.org/en/docs/
Commercial subscriptions for nginx are available on:
* http://nginx.com/products/
----------------------------------------------------------------------
驗證中 : 1 :nginx-1.10.1-1.el7.ngx.x86_64 1/1
已安裝:
nginx.x86_64 1:1.10.1-1.el7.ngx
完畢!
4.啟動nginx
[[email protected] ~]# service nginx start
5.檢視nginx版本
[[email protected] ~]# nginx -v
6.訪問nginx,現在你可以通過公網ip (本地可以通過 localhost /或 127.0.0.1 ) 檢視nginx 服務返回的資訊。
[[email protected] ~]# curl -i localhost
效果如下:
······
Welcome to nginx!。
······
7.nginx配置檔案位置在/etc/nginx/
[[email protected] /]# ll /etc/nginx/
總用量 32
drwxr-xr-x. 2 root root 25 10月 12 13:11 conf.d
-rw-r--r--. 1 root root 1007 5月 31 22:09 fastcgi_params
-rw-r--r--. 1 root root 2837 5月 31 22:09 koi-utf
-rw-r--r--. 1 root root 2223 5月 31 22:09 koi-win
-rw-r--r--. 1 root root 3957 5月 31 22:09 mime.types
lrwxrwxrwx. 1 root root 29 10月 12 13:11 modules -> ../../usr/lib64/nginx/modules
-rw-r--r--. 1 root root 643 5月 31 22:08 nginx.conf
-rw-r--r--. 1 root root 636 5月 31 22:09 scgi_params
-rw-r--r--. 1 root root 664 5月 31 22:09 uwsgi_params
-rw-r--r--. 1 root root 3610 5月 31 22:09 win-utf
8.實踐:
目的:修改服務名,接著從外部訪問這個服務
操作:
a.修改nginx配置檔案
[[email protected] nginx]# vim /etc/nginx/conf.d/default.conf
修改server_name部分:server_name yytest.com;
b.過載服務
[[email protected] nginx]# /usr/sbin/nginx -s reload
c.從外部訪問nginx服務(192.168.10.11)
如在客戶機(192.168.10.10)的瀏覽器訪問:http://yytest.com
d.你發現訪問不了,原因1,你沒有在hosts檔案做對映;原因2,及時你在hosts檔案中了對映,由於nginx伺服器的80埠堵塞或防火牆沒關
e.解決辦法:
步驟一:修改客戶機(192.168.10.10)的hosts檔案,使用SwitchHosts工具新增 192.168.10.11 yytest.com
步驟二:關閉防火牆,具體下文有說明
9.nginx常用操作
啟動:
$ /usr/sbin/nginx或任意路徑下執行service nginx start(centos7是systemctl start nginx.service )
重啟:
$ /usr/sbin/nginx –s reload
停止:
$ /usr/sbin/nginx –s stop
測試配置檔案是否正常:
$ /usr/sbin/nginx –t
可能遇到的問題:
具體情況如下 1。本機能ping通虛擬機器 2。虛擬機器也能ping通本機 3。虛擬機器能訪問自己的web 4。本機無法訪問虛擬己的web 這個問題的原因是伺服器的80埠沒有開啟或防火牆沒有關閉
解決辦法:
如果是centos6:
解決方法如下:
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
然後儲存:
/etc/rc.d/init.d/iptables save
重啟防火牆
/etc/init.d/iptables restart
CentOS防火牆的關閉,關閉其服務即可:
檢視CentOS防火牆資訊:/etc/init.d/iptables status
關閉CentOS防火牆服務:/etc/init.d/iptables stop
永久關閉防火牆:
chkconfig –level 35 iptables off
如果是centos7
[[email protected] ~]# systemctl status firewalld.service
[[email protected] ~]# systemctl stop firewalld.service
[[email protected] ~]# systemctl disable firewalld.service
[[email protected] ~]# systemctl status firewalld.service
擴充套件知識:
啟動一個服務:systemctl start firewalld.service
關閉一個服務:systemctl stop firewalld.service
重啟一個服務:systemctl restart firewalld.service
顯示一個服務的狀態:systemctl status firewalld.service
在開機時啟用一個服務:systemctl enable firewalld.service
在開機時禁用一個服務:systemctl disable firewalld.service
檢視服務是否開機啟動:systemctl is-enabled firewalld.service;echo $?
檢視已啟動的服務列表:systemctl list-unit-files|grep enabled
第二種方式:通過手動下載安裝包解壓安裝
1.下載nginx包。
[[email protected] ~]# wget http://nginx.org/download/nginx-1.10.1.tar.gz
2.複製包到你的安裝目錄
[[email protected] ~]# cp nginx-1.10.1.tar.gz /usr/local/
3.解壓
[[email protected] ~]# tar -zxvf nginx-1.10.1.tar.gz
[[email protected] ~]# cd nginx-1.10.1
4.啟動nginx
[[email protected] ~]# /usr/local/nginx/sbin/nginx
5.檢視版本s
[[email protected] ~]# nginx -v
6.url訪問nginx localhost或127.0.0.1