cpanel是当前世上功能最强大,最容易使用,最受用户欢迎的虚拟主机控制系统,很多国外主机主机都使用这套控制面板。
首先,我们需要安装 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
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: 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
对该句话进行重写,可得到:对于在 cpanel 中新创建的所有账户,nginx 将会为它们创建虚拟主机。通过这些简单的的步骤,我们能够在一台 centos 7 / cpanel 的服务器上配置 nginx 作为反向代理。
nginx 作为反向代理的优势
便于安装和配置。
效率高、性能好。
防止 ddos 攻击。
支持使用 .htaccess 作为 php 的重写规则。
以上就是centos 7中怎么使用cpanel配置nginx反向代理的详细内容。