Apache Installation and Basic Management
To install Apache (httpd) on a CentOS/RHEL system:
yum install httpd
Start the service:
systemctl start httpd
If the web page at http://192.168.1.109/ is unreachable despite the service running, verify port 80 is active:
netstat -tulnp | grep :80
If the port is open but access is blocked, temporarily disable the firewall:
systemctl stop firewalld
Stop Apache with:
systemctl stop httpd
Apache Virtual Hosts and URL Rewriting
Virtual hosts allow hosting multiple domains from one server. Edit /etc/httpd/conf/httpd.conf and define a virtual host:
<VirtualHost *:80>
ServerName www.zhangbiao.com
DocumentRoot /data/www
<Directory "/data/www">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
Create the document root and an index file:
mkdir -p /data/www
echo "<h1>hello world</h1>" > /data/www/index.html
Add an entry to /etc/hosts:
192.168.1.109 www.zhangbiao.com
Restart Apache:
systemctl restart httpd
If accesss fails, ensure:
DocumentRootand<Directory>paths match exactly.- Directory permissions are at least 755.
- SELinux is not blocking access (temporarily disable with
setenforce 0or permanently via/etc/selinux/config).
To host multiple sites, add additional <VirtualHost> blocks with unique ServerName and DocumentRoot values.
Enable URL rewriting by loading the module in httpd.conf:
LoadModule rewrite_module modules/mod_rewrite.so
Then add rewrite rules inside the virtual host:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)\.htmp$ /index.html [L]
</IfModule>
After restarting, requests like /1.htmp will serve index.html.
Nginx Installation and Core Operations
Install Nginx by first adding its official repository:
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx
Manage the service:
systemctl start nginx # Start
systemctl stop nginx # Stop
systemctl reload nginx # Reload config without downtime
The main configuration resides in /etc/nginx/nginx.conf, which includes files from /etc/nginx/conf.d/.
Nginx Virtual Hosts and Advanced Routing
Create a new site config at /etc/nginx/conf.d/zhang.conf:
server {
listen 80;
server_name www.zhangbiao.com;
root /data/www;
index index.html;
}
For multiple domains or ports:
server {
listen 80;
listen 9999;
server_name www.zhangbiao.com www.zhangtao.com;
root /data/www;
index index.html;
}
Add both names to /etc/hosts and reload Nginx to apply changes.
Nginx URL Rewriting
Implement pseudo-static URLs using rewrite in a location block:
location / {
rewrite ^(.*)\.htmp$ /index.html last;
}
This redirects any .htmp request to index.html.
Custom Log Formatting in Nginx
Define a custom log format in /etc/nginx/nginx.conf:
log_format zhang '$remote_addr - "$http_user_agent"';
Apply it globally or per server block:
access_log /var/log/nginx/access_zhang.log zhang;
Reload Nginx to activate logging. Each virtual host can have its own log file.
Reverse Proxy and Load Balacning with Nginx
Configure reverse proxying by defining an upstream group and using proxy_pass:
upstream backend {
server 118.89.106.129:80;
}
server {
listen 80;
server_name www.zhangbiao.com;
location / {
proxy_set_header Host $host;
proxy_pass http://backend;
}
}
For load balancing, list multiple servers in the upstream block:
upstream balanced_hosts {
server 118.89.106.129:80 weight=5;
server 192.168.1.109 weight=1;
}
Request are distributed based on weight (e.g., 5:1 ratio).
Debugging Nginx Configuration
To verify configuration logic, use the return directive for testing:
location /debug {
add_header Content-Type "text/plain;charset=utf-8";
return 200 "$http_host\n";
}
Accessing /debug returns the requested hostname, confirming routing works.
If encountering 502 errors during proxying, SELinux may block outbound connections. Allow it with:
setsebool -P httpd_can_network_connect 1