Skip to main content
Version: 1.0

Guide

Apache is the short name of Apache HTTP Server

Tutorial

Domain binding

The precondition for binding a domain is that application can accessed by domain name, then complete below:

  1. Connect your Server by SFTP tool

  2. Modify Apache vhost configuration file, change the ServerName's value to your domain name

    <VirtualHost *:80>
    ServerName www.mydomain.com # Set your domain name
    DocumentRoot "/data/wwwroot/mysite2"
    ...
  3. Restart Apache service

    sudo systemctl restart apache

Set Rewrite

There are three steps to using and set Apache Rewrite:

  1. Open the Apache module configuration file, make sure Rewrite module is enabled
  2. Check your Apache vhost configuration file that VirtualHost section have AllowOverride All item
  3. Add .htaccess file to your application root directory and configure rewrite rules in the file

Example: 301


<IfModule mod_rewrite.c>
RewriteEngine On
Redirect 301 "/empirecmsall-image-guide" "/xdocs/empirecms-image-guide"
Redirect 301 "/wordpress-image-guide" "/xdocs/wordpressold-image-guide"

</IfModule>

Example: Hide suffix

<IfModule mod_rewrite.c>
RewriteRule ^test$ test.php
ErrorDocument 404 /404.txt

</IfModule>

Use special port for Apache

  1. Add VirtualHost section at Apache vhost configuration file

    <VirtualHost *:81>
    ServerName wordpress.example.com
    #ServerAlias example.com
    DocumentRoot "/data/wwwroot/web1"
    ErrorLog "logs/wordpress-error_log"
    CustomLog "logs/wordpress-access_log" common
    <Directory "/data/wwwroot/web1">
    Options Indexes FollowSymlinks
    AllowOverride All
    Require all granted
    </Directory>
    </VirtualHost>

    <VirtualHost *:82>
    ServerName wordpress.example.com
    #ServerAlias example.com
    DocumentRoot "/data/wwwroot/web2"
    ErrorLog "logs/wordpress-error_log"
    CustomLog "logs/wordpress-access_log" common
    <Directory "/data/wwwroot/web2">
    Options Indexes FollowSymlinks
    AllowOverride All
    Require all granted
    </Directory>
    </VirtualHost>

  2. Add below section at httpd.conf file

    #Listen 12.34.56.78:80
    Listen 80
    Listen 81
    Listen 82

  3. Restart Apache service

Forbidden special IP access

Edit VirtualHost section at Apache vhost configuration file, xxx.xxx.xxx.xxx is IP address

NameVirtualHost  xxx.xxx.xxx.xxx
<virtualhost xxx.xxx.xxx.xxx:80>
ServerName xxx.xxx.xxx.xxx
<Directory />
Order Allow,Deny
Deny from all
</Directory>
</virtualhost>

Set Apache MaxRequests

  1. Run the command httpd -V to check NPM work mode of Apache
    httpd -V
    AH00558: httpd: Could not reliably determine the server's fully qualified domain name
    Server version: Apache/2.4.6 (CentOS)
    Server built: Aug 8 2019 11:41:18
    Server's Module Magic Number: 20120211:24
    Server loaded: APR 1.4.8, APR-UTIL 1.5.2
    Compiled using: APR 1.4.8, APR-UTIL 1.5.2
    Architecture: 64-bit
    Server MPM: prefork
    threaded: no
    forked: yes (variable process count)
  2. Modify /etc/httpd/conf/httpd.conf and set the below items
    <IfModule prefork.c>
    StartServers 5
    MinSpareServers 5
    MaxSpareServers 10
    MaxClients 256
    MaxRequestsPerChild 3000
    </IfModule>

Set the default page

<VirtualHost *:80>
ServerName win.websoft9.com
<IfModule dir_module>
DirectoryIndex index.hmtl defalut.html README.html readme.html about.html
</IfModule>
DocumentRoot "/data/wwwroot/default/site"
...

Set the Cache for Apache

Refer to: Cache guide

Troubleshoot

Apache error: You don't have permission to access/on this server?

Solution:

  1. Check the permission of application root directory
  2. Check the Apache vhost file that have AllowOverride All Require all granted items

No spaces... warning restart Apache?

When this message appears, restarting the service was successful also.

Solution:

echo "fs.inotify.max_user_watches=262144" >> /etc/sysctl.conf 

sysctl -p

[so:warn] [pid 14645] AH01574: module ssl_module is already loaded when running httpd -t command?

Reason: mod_ssl reload
Solution: Check the below files and found mod_ssl item, just retain one

  • /etc/httpd/conf.modules.d/00-base.conf
  • /etc/httpd/conf.modules.d/00-ssl.conf

Parameters

Path

Different Linux distributions have different installation paths:

CentOS/RedHat

Apache main directory: /etc/httpd
Apache vhost configuration file: /etc/httpd/conf.d/vhost.conf
Apache main configuration file: /etc/httpd/conf/httpd.conf
Apache logs file: /var/log/httpd
Apache module configuration file: /etc/httpd/conf.modules.d/00-base.conf

Ubuntu/Debian

Apache main directory: /etc/apache2
Apache vhost configuration file: /etc/apache2/sites-available/000-default.conf
Apache main configuration file: /etc/apache2/apache2.conf
Apache logs file: /var/log/apache2
Apache module directory: /etc/apache2/mods-available

CLI

Apache HTTP 服务器包含的所有命令行(可执行程序)如下:

命令说明
httpdApache 服务器
apachectlApache HTTP 服务器控制工具。
abApache HTTP 服务器性能基准工具。
apxsApache 扩展工具。
configure配置源代码。
dbmmanage为基本认证创建和更新 DBM 格式的用户认证文件。
fcgistarter启动 FastCGI 程序。
htcacheclean清理磁盘缓存。
htdigest为摘要认证创建和更新用户认证文件。
htdbm操作 DBM 密码数据库。
htpasswd为基本认证创建和更新用户认证文件。
httxt2dbm为 RewriteMap 创建 dbm 文件。
logresolve将 Apache 日志文件中的 IP 地址解析到主机名称。
log_server_status周期性的记录服务器状态。
rotatelogs不关闭 Apache 而切换日志文件。
split-logfile将多个虚拟主机的日志文件按照主机拆分。
suexec执行外部程序前切换用户。

Service

#CentOS/Redhat/Fedora
sudo systemctl start httpd
sudo systemctl stop httpd
sudo systemctl restart httpd
sudo systemctl status httpd

# Ubutnu/Debian
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl status apache2

# Docker
sudo docker start apache
sudo docker stop apache
sudo docker restart apache
sudo docker stats apache

VirtualHost Template

All items in the VirtualHost must be correct, any error may cause Apache can't start and application not accessible

VirtualHost ItemUseNecessity
ServerNamePrimary domainRequired
ServerAliasSencondOptional
DocumentRootThe real website storage directoryRequired and must be correct
DirectoryThe real website storage directoryRequired and must be correct
ErrorLogerror logs directorySuggestion
CustomLogvisit logs directorySuggestion

The following are common VirtualHost templates for various Apache use cases:

HTTP VirtualHost

<VirtualHost *:80>
ServerName www.mydomain.com
ServerAlias other.mydomain.com
DocumentRoot "/data/wwwroot/zdoo"
ErrorLog "/var/log/httpd/www.mydomain.com_error_apache.log"
CustomLog "/var/log/httpd/www.mydomain.com_apache.log" common
<Directory "/data/wwwroot/zdoo">
Options Indexes FollowSymlinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

HTTPS VirtualHost

<VirtualHost *:443>
ServerName www.mydomain.com
DocumentRoot "/data/wwwroot/zdoo"
#ErrorLog "logs/www.mydomain.com-error_log"
#CustomLog "logs/www.mydomain.com-access_log" common
<Directory "/data/wwwroot/zdoo">
Options Indexes FollowSymlinks
AllowOverride All
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile /data/cert/www.mydomain.com.crt
SSLCertificateKeyFile /data/cert/www.mydomain.com.key
SSLCertificateChainFile /data/cert/root_bundle.crt
</VirtualHost>

HTTP Alias

Alias /path /data/wwwroot/zdoo
<Directory "/data/wwwroot/zdoo">
Options Indexes FollowSymlinks
AllowOverride All
Require all granted
</Directory>

HTTP Proxy

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName youdomain.com
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://172.21.172.27:8069/
ProxyPassReverse / http://172.21.172.27:8069/
</VirtualHost>