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
ServerAdmin [email protected]
DocumentRoot "/var/blog"
ServerName blog.etiantian.org
ServerAlias etiantian.org
ErrorLog "logs/blog-error_log"
CustomLog "logs/blog-access_log" common
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 <
baizuo's blog.
HI,I am baizuo,My blog address is
http://http://blog.csdn.net/baizuo_1/
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
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
#重启,并在浏览器端访问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
User daemon
Group daemon
ServerAdmin [email protected]
ServerName 127.0.0.1:80
DocumentRoot "/application/apache2.2.34/htdocs"
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
DirectoryIndex index.html
Order allow,deny
Deny from all
Satisfy All
ErrorLog "logs/error_log"
LogLevel warn
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
CustomLog "logs/access_log" common
ScriptAlias /cgi-bin/ "/application/apache2.2.34/cgi-bin/"
AllowOverride None
Options None
Order allow,deny
Allow from all
DefaultType text/plain
RequestHeader unset Proxy early
TypesConfig conf/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
Include conf/extra/httpd-vhosts.conf
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
基于域名的虚拟主机配置文件
[root@c601 conf]# cat /application/apache/conf/extra/httpd-vhosts.conf
NameVirtualHost *:80
ServerAdmin [email protected]
DocumentRoot "/var/blog"
ServerName blog.etiantian.org
ServerAlias etiantian.org
ErrorLog "logs/blog-error_log"
CustomLog "logs/blog-access_log" common
ServerAdmin [email protected]
DocumentRoot "/var/bbs"
ServerName bbs.etiantian.org
ErrorLog "logs/bbs-error_log"
CustomLog "logs/bbs-access_log" common