"The Great Principles of Computing" - Cloud Computing

Summary: Nginx is one of the fastest and most powerful web servers, known for its high performance and low resource usage. It can be installed either as a standalone web server or as a reverse proxy web server. In this article, I will discuss installing Nginx as a reverse proxy server for Apache on a Centos 7 server with cPanel management system installed.

Nginx is one of the fastest and most powerful web servers, known for its high performance and low resource usage. It can be installed either as a standalone web server or as a reverse proxy web server. In this article, I will discuss installing Nginx as a reverse proxy server for Apache on a Centos 7 server with cPanel management system installed.

Nginx serves as a front-end server with a reverse proxy to serve static files, and Apache serves as a back-end for dynamic files. This setting will improve server performance overall.


Let's walk through the installation process of configuring Nginx as a reverse proxy on a CentOS 7 x86_64 server with cPanel 11.52 installed.

First, we need to install EPEL library to start this process

Step 1: Install EPEL library
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
Step 2: nDeploy can be installed CentOS RPM repository to install the nDeploy web class software and Nginx plugins we need
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)
Through the above steps, we have completed the installation of Nginx plugin on our server. Now we can configure Nginx as a reverse proxy and create virtual hosts for existing cPanel user accounts by running the following script.

Step 4: Start Nginx as the default front-end web server and create a default configuration file
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
You can see that this script will modify Apache's port from 80 to another port to allow Nginx to be the front-end web server, and create a virtual host configuration file for existing cPanel users. Once done, confirm the status of Apache and Nginx.

Apache status:
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 runs as front-end server on port 80, Apache configuration is changed for listening on http port 9999 and https port 4430. Look at them:

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
apacheport
apacheport

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:
The configuration file of the virtual host created by the master for an existing user is in "/etc/nginx/sites-enabled". This file path contains the main Nginx configuration files.

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
Example virtual host for a domain name:
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;
}
We can start the browser to view the website to determine the working status of the web server. After installation, read the web service information on the server.

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
webserver-status
webserver-status

Nginx will create virtual hosts for any newly created accounts in cPanel. With these simple steps, we were able to configure Nginx as a reverse proxy on a CentOS 7 / cPanel server.

The advantage of Nginx as a reverse proxy is
easy to install and configure.
High efficiency and good performance.
Prevent Ddos attacks.
Supports the use of .htaccess as a rewrite rule for PHP.
The original release time is: 2016-09-07

This article is from Yunqi Community Partner "Linux China"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326177787&siteId=291194637