① centos 安裝完Nginx後,為什麼訪問不了
【解決方法】
首先看看是不是防火牆開啟了;
本地試下埠是否可訪問telnet 192.168.4.155 80;
CentOS 7默認使用的是firewall作為防火牆,這里改為iptables防火牆。
firewall:
systemctl start firewalld.service#啟動firewall
systemctl stop firewalld.service#停止firewall
systemctl disable firewalld.service#禁止firewall開機啟動
在centos下嘗試使用$curl http://127.0.0.1命令訪問nginx。確定是否可以訪問。
② centos7怎麼編譯安裝nginx
安裝環境為:最小化安裝的centos7,關閉seliunx。
最小化安裝centos:
關閉selinux
sed –i 『s/SELINUX=enforcing/SELINUX=disabled/g』 /etc/selinux/config
開始安裝nginx1.7.8
創建群組
groupadd www
創建一個用戶,不允許登陸和不創主目錄
useradd -s /sbin/nologin -g www -M www
#下載最新版nginx
wget -C http://nginx.org/download/nginx-1.7.8.tar.gz
tar zxvf nginx-1.7.8.tar.gz
#編譯基本能運行的nginx
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_mole --with-http_ssl_mole --with-http_gzip_static_mole
make
make install
如果有錯誤提示:
./configure: error: C compiler cc is not found
解決方法:
yum install gcc gcc-c++
如果有錯誤提示:
./configure: error: the HTTP rewrite mole requires the PCRE library.
You can either disable the mole by using –without-http_rewrite_mole
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using –with-pcre=<path> option.
解決方法:
yum install pcre-devel
如果有錯誤提示:
./configure: error: SSL moles require the OpenSSL library.
You can either do not enable the moles, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using –with-openssl=<path> option.
解決方法:
yum install openssl-devel
以上錯誤提示依次解決後:再一次的運行
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_mole --with-http_ssl_mole --with-http_gzip_static_mole
make
meke install
編譯參數解釋:
#指定運行許可權的用戶
--user=www
#指定運行的許可權用戶組
--group=www
#指定安裝路徑
--prefix=/usr/local/nginx
#支持nginx狀態查詢
--with-http_stub_status_mole
#開啟ssl支持
--with-http_ssl_mole
#開啟GZIP功能
--with-http_gzip_static_mole
因此要順利的通過nginx編譯安裝必須安裝的依賴關系有:
yum install gc gcc gcc-c++ pcre-devel zlib-devel openssl-devel
2、在 centos7 中為nginx的啟動、重啟、重載配置添加腳本
nginx直接啟動的方法:
/usr/local/nginx/sbin/nginx
但是不是很方便,因此使用下面的腳本來控制nginx的啟動關閉重載更加合理一些。
編輯文件:vim /usr/lib/systemd/system/nginx.service 添加下面的腳本,注意路徑 !
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
systemctl的一些使用方法:
systemctl is-enabled servicename.service #查詢服務是否開機啟動
systemctl enable xxx.service #開機運行服務
systemctl disable xxx.service #取消開機運行
systemctl start xxx.service #啟動服務
systemctl stop xxx.service #停止服務
systemctl restart xxx.service #重啟服務
systemctl reload xxx.service #重新載入服務配置文件
systemctl status xxx.service #查詢服務運行狀態
systemctl --failed #顯示啟動失敗的服務
因此,添加上面腳本後,centos7 中操作nginx的方法有
systemctl is-enabled nginx.service #查詢nginx是否開機啟動
systemctl enable nginx.service #開機運行nginx
systemctl disable nginx.service #取消開機運行nginx
systemctl start nginx.service #啟動nginx
systemctl stop nginx.service #停止nginx
systemctl restart nginx.service #重啟nginx
systemctl reload nginx.service #重新載入nginx配置文件
systemctl status nginx.service #查詢nginx運行狀態
systemctl --failed #顯示啟動失敗的服務
③ 如何在 centos 7/cpanel 伺服器上配置 nginx 反向代理
註:本配置環境在CentOS下實現,其他方法請參考官方幫助文件
一、安裝Nginx軟體
Nginx官方網站:http://nginx.org
我們使用yum安裝(配置和升級方便),需要配置官方的yum源,Nginx官方源配置提供的配置方法如下:
1.創建一個更新源
#vim /etc/yum.repos.d/nginx.repo
2.加入如下內容:
[nginx]
name=nginx repo baseurl=http://nginx.org/packages/OS/EOSRELEAS/$basearch/
gpgcheck=0
enabled=1
3.把上面藍色欄位換成的操作系統類型,比如rhel或者centos,把綠色部分替換成發行的主版本號,比如「5」 或者 「6」, 分別代表 5.x 或 6.x 對應的版本。
其他版本的系統(Debian/Ubuntu)也可以參考nginx官方的方法配置(英文內容)。
4.配置完以上三部,就可以開始用我們熟悉的yum命令安裝nginx了(默認安裝最新的nginx穩定發行版本)。
#yum install nginx
5.安裝完畢看看都生成了哪些文件,配置文件都放在哪裡
#rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/conf.d/example_ssl.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/rc.d/init.d/nginx
/etc/sysconfig/nginx
/usr/sbin/nginx
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx
以上是我安裝完的配置文件位置,安裝的版本是1.4.2版本(查看版本:nginx -v) ,如下圖:
6.檢查是否安裝成功
啟動nginx服務,輸入伺服器ip訪問是否能打開默認網站:
#service nginx restart
如果nginx服務啟動成功,但打不開網站,排除如果不是網路問題,可能是因為iptables,iptables開放80埠即可:
#vim /etc/sysconfig/iptables
加入:-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT 如下圖:
二、配置文件
1.由上面的步驟,我們看到配置文件放置在/etc/nginx/目錄下:
主要配置文件:/etc/nginx/nginx.conf 內容如下圖
擴展配置文件:/etc/nginx/conf.d/*.conf
圖中的主配置文件的末尾,載入所有擴展配置文件裡面以.conf結尾的文件。所以我們不要修改主要配置文件(不需要修改),用戶配置都放到了/etc/nginx/conf.d/目錄下,裡面默認有兩個配置文件,一個普通的配置,一個是ssl配置。
2.為一個域名配置一個文件(文件名任意,以.conf結尾即可)
#cd /etc/nginx/conf.d/
#vim www.test.com.conf
把內容修改如下:
---------------------------------------------------------------------------
server {
listen 80;
server_name www.test.com;
charset utf8;
access_log /var/log/nginx/www.test.com.access.log main;
location / {
proxy_pass http://192.168.1.20:80;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
---------------------------------------------------------------------------
如圖:
3.重啟nginx服務,檢查配置文件並生效(nginx -t)
#service nginx restart
4.如果沒出意外,你應該已經成功實現Nginx反向代理服務了!
三、其他幫助
1.幫助命令
2.官方幫助文檔:http://nginx.org/en/docs/
④ 如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理
第一步: 安裝 EPEL 庫
root@server1 [/usr]# yum -y install epel-release
Loaded plugins: fastestmirror, tsflags, universal-hooks
Loading mirror speeds from cached hostfile
* EA4: 66.23.237.210
* base: mirrors.linode.com
* extras: mirrors.linode.com
* updates: mirrors.linode.com
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-5 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================
Package Arch Version Repository Size
========================================================================================
Installing:
epel-release noarch 7-5 extras 14 k
第二步: 安裝 nDeploy 的 CentOS RPM 庫
可以安裝 nDeploy 的 CentOS RPM 庫來安裝我們所需的 nDeploy Web 類軟體和 Nginx 插件
root@server1 [/usr]# yum -y install http://rpm.piserve.com/nDeploy-release-centos-1.0-1.noarch.rpm
Loaded plugins: fastestmirror, tsflags, universal-hooks
nDeploy-release-centos-1.0-1.noarch.rpm | 1.7 kB 00:00:00
Examining /var/tmp/yum-root-ei5tWJ/nDeploy-release-centos-1.0-1.noarch.rpm: nDeploy-release-centos-1.0-1.noarch
Marking /var/tmp/yum-root-ei5tWJ/nDeploy-release-centos-1.0-1.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package nDeploy-release-centos.noarch 0:1.0-1 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================
Package Arch Version Repository Size
========================================================================================
Installing:
nDeploy-release-centos noarch 1.0-1 /nDeploy-release-centos-1.0-1.noarch 110
第三步:安裝 nDeploy 和 Nginx nDeploy 插件
root@server1 [/usr]# yum --enablerepo=ndeploy install nginx-nDeploy nDeploy
Loaded plugins: fastestmirror, tsflags, universal-hooks
epel/x86_64/metalink | 9.9 kB 00:00:00
epel | 4.3 kB 00:00:00
ndeploy | 2.9 kB 00:00:00
(1/4): ndeploy/7/x86_64/primary_db | 14 kB 00:00:00
(2/4): epel/x86_64/group_gz | 169 kB 00:00:00
(3/4): epel/x86_64/primary_db | 3.7 MB 00:00:02
Dependencies Resolved
========================================================================================
Package Arch Version Repository Size
========================================================================================
Installing:
nDeploy noarch 2.0-11.el7 ndeploy 80 k
nginx-nDeploy x86_64 1.8.0-34.el7 ndeploy 36 M
Installing for dependencies:
PyYAML x86_64 3.10-11.el7 base 153 k
libevent x86_64 2.0.21-4.el7 base 214 k
memcached x86_64 1.4.15-9.el7 base 84 k
python-inotify noarch 0.9.4-4.el7 base 49 k
python-lxml x86_64 3.2.1-4.el7 base 758 k
Transaction Summary
========================================================================================
Install 2 Packages (+5 Dependent packages)
通過以上這些步驟,我們完成了在我們的伺服器上 Nginx 插件的安裝。現在我們可以配置 Nginx 作為反向代理和為已有的 cPanel 用戶賬戶創建虛擬主機,為此我們可以運行如下腳本。
第四步:啟動 Nginx 作為默認的前端 Web 伺服器,並創建默認的配置文件
root@server1 [/usr]# /opt/nDeploy/scripts/cpanel-nDeploy-setup.sh enable
Modifying apache http and https port in cpanel
httpd restarted successfully.
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/ndeploy_watcher.service to /usr/lib/systemd/system/ndeploy_watcher.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/ndeploy_backends.service to /usr/lib/systemd/system/ndeploy_backends.service.
ConfGen:: saheetha
ConfGen:: satest
你可以看到這個腳本將修改 Apache 的埠從 80 到另一個埠來讓 Nginx 作為前端 Web 伺服器,並為現有的 cPanel 用戶創建虛擬主機配置文件。一旦完成,確認 Apache 和 Nginx 的狀態。
Apache 狀態:
root@server1 [/var/run/httpd]# systemctl status httpd
● httpd.service - Apache Web Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2016-01-18 06:34:23 UTC; 12s ago
Process: 25606 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 24760 (httpd)
CGroup: /system.slice/httpd.service
‣ 24760 /usr/local/apache/bin/httpd -k start
Jan 18 06:34:23 server1.centos7-test.com systemd[1]: Starting Apache Web Server...
Jan 18 06:34:23 server1.centos7-test.com apachectl[25606]: httpd (pid 24760) already running
Jan 18 06:34:23 server1.centos7-test.com systemd[1]: Started Apache Web Server.
Nginx 狀態:
root@server1 [~]# systemctl status nginx
● nginx.service - nginx-nDeploy - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2016-01-17 17:18:29 UTC; 13h ago
Docs: http://nginx.org/en/docs/
Main PID: 3833 (nginx)
CGroup: /system.slice/nginx.service
├─ 3833 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
├─25473 nginx: worker process
├─25474 nginx: worker process
└─25475 nginx: cache manager process
Jan 17 17:18:29 server1.centos7-test.com systemd[1]: Starting nginx-nDeploy - high performance web server...
Jan 17 17:18:29 server1.centos7-test.com nginx[3804]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jan 17 17:18:29 server1.centos7-test.com nginx[3804]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jan 17 17:18:29 server1.centos7-test.com systemd[1]: Started nginx-nDeploy - high performance web server.
Nginx 作為前端伺服器運行在 80 埠,Apache 配置被更改為監聽 http 埠 9999 和 https 埠 4430。請看他們的情況:
root@server1 [/usr/local/src]# netstat -plan | grep httpd
tcp 0 0 0.0.0.0:4430 0.0.0.0:* LISTEN 17270/httpd
tcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 17270/httpd
tcp6 0 0 :::4430 :::* LISTEN 17270/httpd
tcp6 0 0 :::9999 :::* LISTEN 17270/httpd
root@server1 [/usr/local/src]# netstat -plan | grep nginx
tcp 0 0 127.0.0.1:80 0.0.0.0:* LISTEN 17802/nginx: master
tcp 0 0 45.79.183.73:80 0.0.0.0:* LISTEN 17802/nginx: master
為已有用戶創建的虛擬主機的配置文件在 「/etc/nginx/sites-enabled」。 這個文件路徑包含了 Nginx 主要配置文件。
root@server1 [/etc/nginx/sites-enabled]# ll | grep .conf
-rw-r--r-- 1 root root 311 Jan 17 09:02 saheetha.com.conf
-rw-r--r-- 1 root root 336 Jan 17 09:02 saheethastest.com.conf
一個域名的示例虛擬主機:
server {
listen 45.79.183.73:80;
#CPIPVSIX:80;
# ServerNames
server_name saheetha.com www.saheetha.com;
access_log /usr/local/apache/domlogs/saheetha.com main;
access_log /usr/local/apache/domlogs/saheetha.com-bytes_log bytes_log;
include /etc/nginx/sites-enabled/saheetha.com.include;
}
我們可以啟動瀏覽器查看網站來確定 Web 伺服器的工作狀態。安裝後,請閱讀伺服器上的 web 服務信息。
root@server1 [/home]# ip a | grep -i eth0
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
inet 45.79.183.73/24 brd 45.79.183.255 scope global dynamic eth0
root@server1 [/home]# nginx -v
nginx version: nginx/1.8.0
Nginx 將會為任何最新在 cPanel 中創建的賬戶創建虛擬主機。通過這些簡單的的步驟,我們能夠在一台 CentOS 7 / cPanel 的伺服器上配置 Nginx 作為反向代理。
Nginx 作為反向代理的優勢
便於安裝和配置。
效率高、性能好。
防止 Ddos 攻擊。
支持使用 .htaccess 作為 php 的重寫規則。
⑤ 如何安裝nginx
nginx
windows版
v1.13.3免費版http://www.ddooo.com/softdown/29113.htm
nginx
windows安裝配置方法
第一步、安裝方法
一、下載好後,把nginx
windows版軟體解壓到c盤根目錄。
二、雙擊nginx.exe圖標,可見黑窗口一閃而過,啟動完畢。
三、按下win+R快捷鍵,輸入cmd打開命令行。
四、命令行到nginx目錄,輸入nginx啟動。(注,此方式命令行窗口無任何提示,且被鎖定)
五、打開瀏覽器,輸入http://127.0.0.1,如果看到下圖片則安裝成功。
六、以後啟動軟體直接雙擊圖標即可。
七、啟動後,默認情況下(無修改配置),可見到有兩個nginx的進程,一個是master
process,一個是worker
processes測試。
第二步、配置方法
配置目標:能正常運行PHP腳本程序
大部分情況下,我們需要修改的配置文件只有一個,那就是nginx.conf,該文件位於conf目錄下。具體配置項目為:
1.
server_tokens
off;
出於安全方面的考慮,最好是隱藏nginx版本號信息
2.
listen
8088;
8088為監聽埠,根據需要可以填寫其它埠號
3.
server_name
localhost;
localhost為伺服器訪問名稱,也就是我們在瀏覽器里輸入的那個url地址
4.
charset
utf-8;
字元集編碼
5.
工作目錄
將如下配置
修改為:
root
定義了工作空間,也就是我們php項目所在的目錄。
加入index.php是為了讓nginx能夠識別php腳本,否則,在訪問php文件時,會出現直接下載的情況。
6.
整合php
將location
~
\.php配置部分的注釋全部去掉,最終配置如下:
注意這裡面的$document_root變數,它對應的內容就是root參數值,如果我們沒有定義root參數或者把root注釋掉,在訪問php的時候,頁面上就會出現No
input
file
specified.提示。
7.
啟動php-cgi
打開cmd命令窗口,切換到php的安裝目錄,執行php-cgi
-b
127.0.0.1:9000,即可啟動php-cgi,啟動完成後,cmd窗口切勿關閉,否則php-cgi也會被關掉的。
特別提醒:只有在開啟php-cgi的情況下,nginx才能正常訪問php。
8.
重啟nginx
打開cmd命令窗口,切換到nginx所在目錄,執行nginx
-s
reload即可重啟nginx。其它相關nginx相關命令如下:
啟動:start
nginx
停止:nginx
-s
stop
退出:nginx
-s
quit
⑥ nginx如何配置https的301重定向,以及nginx的檢查重啟命令是什麼
首先需要配置好SSL證書,網上有很多相關教程,具體不多說了,
配置好SSL證書後,(Nginx配置)會有兩個文件,一個以.crt結尾,一個以.key結尾的。
網上有很多配置http跳轉到https得到教程,都是下面rewrite的方法,這里不建議使用rewrite規則跳轉,容易出現重定向次數過多的問題
rewrite ^(.*)$ https://$host$1 permanent;
我們可以使用return來實現
return 301 https://www.000000.com$request_uri;
Nginx文件更改好要在重啟下才會生效,否則還會變成原來的內容,這里建議重啟前先檢查下配置文件是否爭取:
首先找到Nginx的所在目錄,目錄下有sbin文件,進入到sbin文件下執行以下命令:
執行: ./nginx -t
如果出現……ok和……success語句,則正面文件配置正確
執行: ./nginx -s reload
這個時候不會出現任何提示,Nginx的301重定向就已經完成了。
注意事項:進入sbin目錄下執行命令,前面一定要加上./
⑦ centos7重啟nginx
如果你是以服務模式啟動的nginx,重啟nginx的命令為:systemctl restart nginx.service如果你是通過命令行啟動的nginx,重啟命令為ngnx -s restart 望採納
⑧ centos7怎麼查看已經安裝nginx
如果你nginx是rpm包安裝的,直接用如下命令:
nginx
-V
如果你是源碼包編譯安裝,假如你的安裝路徑是/usr/local/nginx,那麼你可以使用:
/usr/local/nginx/sbin/nginx
-V
注意是大寫的V,這樣你就可以看到nginx已經載入的模塊了。