安装和配置Apache
下载apache
配置文件解析
CentOS下/etc/httpd
文件夹内容如下
.
├── conf
│ ├── httpd.conf
│ └── magic
├── conf.d
│ ├── autoindex.conf
│ ├── README
│ ├── userdir.conf
│ └── welcome.conf
├── conf.modules.d
│ ├── 00-base.conf
│ ├── 00-dav.conf
│ ├── 00-lua.conf
│ ├── 00-mpm.conf
│ ├── 00-proxy.conf
│ ├── 00-systemd.conf
│ └── 01-cgi.conf
├── logs -> ../../var/log/httpd
├── modules -> ../../usr/lib64/httpd/modules
└── run -> /run/httpd
/etc/httpd
文件夹内容如下
.
├── conf
│ ├── httpd.conf
│ └── magic
├── conf.d
│ ├── autoindex.conf
│ ├── README
│ ├── userdir.conf
│ └── welcome.conf
├── conf.modules.d
│ ├── 00-base.conf
│ ├── 00-dav.conf
│ ├── 00-lua.conf
│ ├── 00-mpm.conf
│ ├── 00-optional.conf
│ ├── 00-proxy.conf
│ ├── 00-systemd.conf
│ ├── 01-cgi.conf
│ ├── 10-h2.conf
│ ├── 10-proxy_h2.conf
│ └── README
├── logs -> ../../var/log/httpd
├── modules -> ../../usr/lib64/httpd/modules
├── run -> /run/httpd
└── state -> ../../var/lib/httpd
/var/www
文件夹下
conf/httpd.conf
部分参数含义如下:
ServerRoot “/etc/httpd“ //服务配置文件目录
PidFile run/httpd.pid //PID文件
Listen 80 //默认监听端口
Include conf.modules.d/*.conf //包含模块目录的配置文件
User apache //启动用户
Group apache //启动组
ServerAdmin root@localhost //管理员邮箱
ServerName www.example.com:80 //域名主机名
DocumentRoot “/var/www/html“ //默认主页存放目录
DirectoryIndex index.html index.html.var //索引文件
<Directory "/var/www"> //规定网站根目录的位置
AllowOverride None
# Allow open access:
Require all granted
</Directory>
ErrorLog logs/error_log //错误日志
CustomLog logs/access_log combined //访问日志
AddDefaultCharset UTF-8 //默认字符集
IncludeOptional conf.d/*.conf //包含了 conf.d/*.conf 的配置文件
<IfModule dir_module>
DirectoryIndex index.html //默认索引
</IfModule>
配置文件案例
以下将列出集中常见的配置文件写法,需要注意的是,下方案例仅包含配置文件中的部分内容,请勿直接复制并替换配置文件!
同IP,多域名
通过不同域名访问不同站点
<VirtualHost 192.168.2.2:80>
ServerAdmin [email protected]
ServerName www.111cn.net
DocumentRoot "g:/www1"
<Directory "g:/www1">
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.2.2:80>
ServerAdmin [email protected]
ServerName www.111cn.net
DocumentRoot "g:/www2"
<Directory "g:/www2">
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
多端口,多站点
通过不同端口来访问不同站点(根目录)
Listen 80
Listen 8080<VirtualHost *:80>
ServerAdmin [email protected]
ServerName localhost:80
DocumentRoot "/www1"
<Directory "/www1">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin [email protected]
ServerName localhost:8080
DocumentRoot "/www2"
<Directory "/www2">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
多IP,同端口
透过多个IP访问统一站点(根目录)
<VirtualHost 192.168.1.68:80>
ServerAdmin [email protected]
ServerName localhost:80
DocumentRoot "g:/www1"
<Directory "g:/www1">
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost 192.168.2.2:80>
ServerAdmin [email protected]
ServerName localhost:80
DocumentRoot "g:/www2"
<Directory "g:/www2">
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>