Lamp之-Apache服务生产实战应用指南(03.apache部署虚拟主机)
基于域名的虚拟主机实战配置1.开启主配置文件httpd.conf中包含httpd-vhosts.conf的配置。
[root@c601 conf]# cd /application/apache/conf/
[root@c601 conf]# ls
extra httpd.conf magic mime.types original
[root@c601 conf]# cp httpd.conf httpd.conf.20180128
[root@c601 conf]# vim httpd.conf
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
PS:( 大概在配置文件的405行左右,或者sed -i替换。)
2.开始配置extra/httpd-vhosts.con文件。
[root@c601 extra]# grep -v "\#" httpd-vhosts.conf
[root@c601 conf]# cd extra/
[root@c601 extra]# cp httpd-vhosts.conf httpd-vhosts.conf.baizuo20180128
[root@c601 extra]# grep -v "\#" httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 31333741@qq.com
DocumentRoot "/var/blog"
ServerName blog.etiantian.org
ServerAlias etiantian.org
ErrorLog "logs/blog-error_log"
CustomLog "logs/blog-access_log" common
</VirtualHost>
3.检测改过的apache配置文件的语法:
[root@c601 extra]# /application/apache/bin/apachectl -t
Warning: DocumentRoot [/var/blog] does not exist
httpd: apr_sockaddr_info_get() failed for c601.zte
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
Syntax OK
3.1解决上面的Warning问题。修改主配置文件的ServerName配置。
ServerName 127.0.0.1:80
检测语法
[root@c601 extra]# /application/apache/bin/apachectl -t
Warning: DocumentRoot [/var/blog] does not exist
Syntax OK
3.2解决/var/blog未创建错误。
[root@c601 extra]# mkdir /var/blog
[root@c601 extra]# /application/apache/bin/apachectl -t
Syntax OK
3.3创建虚拟主机站点首页。
cat > /var/blog/index.html <<EOF
<html>
<head><title>baizuo's blog.</title></head>
<body>
HI,I am baizuo,My blog address is
<a href="http://blog.csdn.net/baizuo_1/">http://http://blog.csdn.net/baizuo_1/</a>
</body>
</html>
EOF
执行graceful参数重启apache服务。
[root@c601 extra]# /application/apache/bin/apachectl graceful
提示:这里是graceful参数,而不是restart参数,graceful表示优雅重启或平滑重启。操作规范。
检测服务:
[root@c601 extra]# ps -ef |grep httpdd
root 27902 1217 0 22:20 pts/1 00:00:00 grep httpdd
[root@c601 extra]# ps -ef |grep httpd
root 27173 1 0 19:08 ? 00:00:01 /application/apache2.2.34/bin/httpd -k start
daemon 27812 27173 0 22:19 ? 00:00:00 /application/apache2.2.34/bin/httpd -k start
daemon 27813 27173 0 22:19 ? 00:00:00 /application/apache2.2.34/bin/httpd -k start
daemon 27814 27173 0 22:19 ? 00:00:00 /application/apache2.2.34/bin/httpd -k start
daemon 27815 27173 0 22:19 ? 00:00:00 /application/apache2.2.34/bin/httpd -k start
root 27904 1217 0 22:20 pts/1 00:00:00 grep httpd
[root@c601 extra]# netstat -lnt|grep 80
tcp 0 0 :::80 :::* LISTEN
3.5在WINDOWS做hosts解析。C:\Windows\System32\drivers\etc\hosts
192.168.0.252 www.etiantian.org blog.etiantian.org bbs.etiantian.org etiantian.org
3.6访问blog.etiantian.org出现了错误。
Forbidden
You don't have permission to access / on this server.
出现这个问题的原因是在主配置文件中没有配置目录许可。
在httpd.conf配置文件文尾添加如下配置:
[root@c601 conf]# tail httpd.conf
#vhosts blog
<Directory "/var/blog">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
#重启,并在浏览器端访问blog.etiantian.org。
[root@c601 conf]# /application/apache/bin/apachectl -t
Syntax OK
[root@c601 conf]# /application/apache/bin/apachectl graceful
[root@c601 conf]# ps -ef |grep httpd
root 27173 1 0 19:08 ? 00:00:01 /application/apache2.2.34/bin/httpd -k start
daemon 27956 27173 0 22:33 ? 00:00:00 /application/apache2.2.34/bin/httpd -k start
daemon 27957 27173 0 22:33 ? 00:00:00 /application/apache2.2.34/bin/httpd -k start
daemon 27958 27173 0 22:33 ? 00:00:00 /application/apache2.2.34/bin/httpd -k start
daemon 27959 27173 0 22:33 ? 00:00:00 /application/apache2.2.34/bin/httpd -k start
root 28042 1217 0 22:34 pts/1 00:00:00 grep httpd
结果如下
HI,I am baizuo,My blog address is http://http://blog.csdn.net/baizuo_1/
你可尝试添加一个bbs的虚拟主机呦。
可参考的配置文件.
主配置文件:
[root@c601 conf]# egrep -v "^.*#|^$" /application/apache/conf/httpd.conf
ServerRoot "/application/apache2.2.34"
Listen 80
<IfModule !mpm_netware_module>
<IfModule !mpm_winnt_module>
User daemon
Group daemon
</IfModule>
</IfModule>
ServerAdmin you@example.com
ServerName 127.0.0.1:80
DocumentRoot "/application/apache2.2.34/htdocs"
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
<Directory "/application/apache2.2.34/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
<FilesMatch "^\.ht">
Order allow,deny
Deny from all
Satisfy All
</FilesMatch>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" common
</IfModule>
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/application/apache2.2.34/cgi-bin/"
</IfModule>
<IfModule cgid_module>
</IfModule>
<Directory "/application/apache2.2.34/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
DefaultType text/plain
<IfModule headers_module>
RequestHeader unset Proxy early
</IfModule>
<IfModule mime_module>
TypesConfig conf/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
</IfModule>
Include conf/extra/httpd-vhosts.conf
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
<Directory "/var/blog">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Directory "/var/bbs">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
基于域名的虚拟主机配置文件
[root@c601 conf]# cat /application/apache/conf/extra/httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 31333741@qq.com
DocumentRoot "/var/blog"
ServerName blog.etiantian.org
ServerAlias etiantian.org
ErrorLog "logs/blog-error_log"
CustomLog "logs/blog-access_log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 31333741@qq.com
DocumentRoot "/var/bbs"
ServerName bbs.etiantian.org
ErrorLog "logs/bbs-error_log"
CustomLog "logs/bbs-access_log" common
</VirtualHost>