歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> 如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理

如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理

日期:2017/3/3 17:47:00   编辑:學習Linux

如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理

如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理


導讀Nginx 是最快和最強大的 Web 服務器之一,以其高性能和低資源占用率而聞名。它既可以被安裝為一個獨立的 Web 服務器,也可以安裝成反向代理 Web 服務器。在這篇文章,我將討論在安裝了 cPanel 管理系統的 Centos 7 服務器上安裝 Nginx 作為 Apache 的反向代理服務器。Nginx 作為前端服務器用反向代理為靜態文件提供服務,Apache 作為後端為動態文件提供服務。這個設置將整體提高服務器的性能。

如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理

讓我們過一遍在已經安裝好 cPanel 11.52 的 CentOS 7 x86_64 服務器上配置 Nginx 作為反向代理的安裝過程。

首先,我們需要安裝 EPEL 庫來啟動這個進程

第一步: 安裝 EPEL 庫
root@server1 [/usr]# yum -y install epel-releaseLoaded plugins: fastestmirror, tsflags, universal-hooksLoading mirror speeds from cached hostfile* EA4: 66.23.237.210* base: mirrors.linode.com* extras: mirrors.linode.com* updates: mirrors.linode.comResolving Dependencies--> Running transaction check---> Package epel-release.noarch 0:7-5 will be installed--> Finished Dependency ResolutionDependencies 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.rpmLoaded plugins: fastestmirror, tsflags, universal-hooksnDeploy-release-centos-1.0-1.noarch.rpm | 1.7 kB 00:00:00Examining /var/tmp/yum-root-ei5tWJ/nDeploy-release-centos-1.0-1.noarch.rpm: nDeploy-release-centos-1.0-1.noarchMarking /var/tmp/yum-root-ei5tWJ/nDeploy-release-centos-1.0-1.noarch.rpm to be installedResolving Dependencies--> Running transaction check---> Package nDeploy-release-centos.noarch 0:1.0-1 will be installed--> Finished Dependency ResolutionDependencies 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 nDeployLoaded plugins: fastestmirror, tsflags, universal-hooksepel/x86_64/metalink | 9.9 kB 00:00:00epel | 4.3 kB 00:00:00ndeploy | 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:02Dependencies Resolved========================================================================================Package Arch Version Repository Size========================================================================================Installing:nDeploy noarch 2.0-11.el7 ndeploy 80 knginx-nDeploy x86_64 1.8.0-34.el7 ndeploy 36 MInstalling for dependencies:PyYAML x86_64 3.10-11.el7 base 153 klibevent x86_64 2.0.21-4.el7 base 214 kmemcached x86_64 1.4.15-9.el7 base 84 kpython-inotify noarch 0.9.4-4.el7 base 49 kpython-lxml x86_64 3.2.1-4.el7 base 758 kTransaction Summary========================================================================================Install 2 Packages (+5 Dependent packages)

通過以上這些步驟,我們完成了在我們的服務器上 Nginx 插件的安裝。現在我們可以配置 Nginx 作為反向代理和為已有的 cPanel 用戶賬戶創建虛擬主機,為此我們可以運行如下腳本。

第四步:啟動 Nginx 作為默認的前端 Web 服務器,並創建默認的配置文件
root@server1 [/usr]# /opt/nDeploy/scripts/cpanel-nDeploy-setup.sh enableModifying apache http and https port in cpanelhttpd 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:: saheethaConfGen:: satest

你可以看到這個腳本將修改 Apache 的端口從 80 到另一個端口來讓 Nginx 作為前端 Web 服務器,並為現有的 cPanel 用戶創建虛擬主機配置文件。一旦完成,確認 Apache 和 Nginx 的狀態。

Apache 狀態:
root@server1 [/var/run/httpd]# systemctl status httpd● httpd.service - Apache Web ServerLoaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)Active: active (running) since Mon 2016-01-18 06:34:23 UTC; 12s agoProcess: 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 startJan 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 runningJan 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 serverLoaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)Active: active (running) since Sun 2016-01-17 17:18:29 UTC; 13h agoDocs: 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 processJan 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 okJan 17 17:18:29 server1.centos7-test.com nginx[3804]: nginx: configuration file /etc/nginx/nginx.conf test is successfulJan 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 httpdtcp 0 0 0.0.0.0:4430 0.0.0.0:* LISTEN 17270/httpdtcp 0 0 0.0.0.0:9999 0.0.0.0:* LISTEN 17270/httpdtcp6 0 0 :::4430 :::* LISTEN 17270/httpdtcp6 0 0 :::9999 :::* LISTEN 17270/httpd

如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理

root@server1 [/usr/local/src]# netstat -plan | grep nginxtcp 0 0 127.0.0.1:80 0.0.0.0:* LISTEN 17802/nginx: mastertcp 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;# ServerNamesserver_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 eth03: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000inet 45.79.183.73/24 brd 45.79.183.255 scope global dynamic eth0root@server1 [/home]# nginx -vnginx version: nginx/1.8.0

如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理如何在 CentOS 7 用 cPanel 配置 Nginx 反向代理

Nginx 將會為任何最新在 cPanel 中創建的賬戶創建虛擬主機。通過這些簡單的的步驟,我們能夠在一台 CentOS 7 / cPanel 的服務器上配置 Nginx 作為反向代理。

Nginx 作為反向代理的優勢
  1. 便於安裝和配置。
  2. 效率高、性能好。
  3. 防止 Ddos 攻擊。
  4. 支持使用 .htaccess 作為 PHP 的重寫規則。

我希望這篇文章對你們有用。感謝你看它。我非常高興收到你的寶貴意見和建議,並進一步改善。

原文來自:https://linux.cn/article-7751-1.html

本文地址:http://www.linuxprobe.com/centos-cpanel-nginx.html


http://xxxxxx/Linuxjc/1184721.html TechArticle

Copyright © Linux教程網 All Rights Reserved