当前位置首页 > Apache知识

apache虚拟主机基础配置(用户认证,域名跳转)

阅读次数:239 次  来源:admin  发布时间:

配置虚拟主机安装discuz

[root@tyrr www]# cd
[root@tyrr ~]# mkdir -p /data/www
[root@tyrr ~]# cd /data/www/
[root@tyrr www]# unzip Discuz_X3.2_SC_GBK.zip 
[root@tyrr www]# ls
Discuz_X3.2_SC_GBK.zip  readme  upload  utility
[root@tyrr www]# mv upload/* .
[root@tyrr www]# ls
admin.php  archiver     cp.php           Discuz_X3.2_SC_GBK.zip  group.php  install     plugin.php  robots.txt  static     uc_server    utility
api        config       crossdomain.xml  favicon.ico             home.php   member.php  portal.php  search.php  template   upload
api.php    connect.php  data             forum.php               index.php  misc.php    readme      source      uc_client  userapp.php
[root@tyrr www]# rm  -fr Discuz_X3.2_SC_GBK.zip  readme/ utility/
[root@tyrr www]# ls
admin.php  archiver     cp.php           favicon.ico  home.php   member.php  portal.php  source    uc_client  userapp.php
api        config       crossdomain.xml  forum.php    index.php  misc.php    robots.txt  static    uc_server
api.php    connect.php  data             group.php    install    plugin.php  search.php  template  upload
[root@tyrr www]# cd

编辑apache主配置文件开启支持虚拟主机

[root@tyrr ~]# vim /usr/local/apache2/conf/httpd.conf

Include conf/extra/httpd-vhosts.conf
-----------
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Allow from all
</Directory>

编辑虚拟主机配置文件

[root@tyrr ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 
<VirtualHost *:80>
    DocumentRoot "/data/www"
    ServerName www.aaa.com
    ServerAlias www.bbb.com
</VirtualHost>

检查配置文件并重启

[root@tyrr ~]# /usr/local/apache2/bin/apachectl -t
Syntax OK
[root@tyrr ~]# /usr/local/apache2/bin/apachectl restart

[root@tyrr ~]# ps aux |grep httpd
root       1813  0.0  1.0 193572 10728 ?        Ss   19:40   0:00 /usr/local/apache2/bin/httpd -k start
daemon     2116  0.0  0.5 193708  5644 ?        S    20:59   0:00 /usr/local/apache2/bin/httpd -k start
daemon     2117  0.0  0.5 193708  5824 ?        S    20:59   0:00 /usr/local/apache2/bin/httpd -k start
daemon     2118  0.0  0.8 194816  8976 ?        S    20:59   0:00 /usr/local/apache2/bin/httpd -k start
daemon     2119  0.0  0.6 193708  6220 ?        S    20:59   0:00 /usr/local/apache2/bin/httpd -k start
daemon     2120  0.0  0.5 193708  5644 ?        S    20:59   0:00 /usr/local/apache2/bin/httpd -k start
daemon     2121  0.0  0.5 193708  5644 ?        S    20:59   0:00 /usr/local/apache2/bin/httpd -k start
daemon     2137  0.0  0.5 193708  5644 ?        S    20:59   0:00 /usr/local/apache2/bin/httpd -k start
daemon     2138  0.0  0.5 193708  5644 ?        S    20:59   0:00 /usr/local/apache2/bin/httpd -k start
root       2154  0.0  0.0 103324   888 pts/0    S+   21:03   0:00 grep httpd

按照安装要求 修改相关配置

[root@tyrr ~]# cd /data/www/
[root@tyrr www]# ls
admin.php  archiver     cp.php           favicon.ico  home.php   member.php  portal.php  source    uc_client  userapp.php
api        config       crossdomain.xml  forum.php    index.php  misc.php    robots.txt  static    uc_server
api.php    connect.php  data             group.php    install    plugin.php  search.php  template  upload

[root@tyrr www]# chown -R daemon config data uc_client/data uc_server/data
[root@tyrr www]# which mysql
/usr/bin/which: no mysql in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@tyrr www]# vim /etc/profile.d/path.sh
#!/bin/bash
export PATH=$PATH:/usr/local/mysql/bin/:/usr/local/apache2/bin/


[root@tyrr www]# source /etc/profile.d/path.sh 


[root@tyrr www]# mysql -u root -p19950801
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type \'help;\' or \'\h\' for help. Type \'\c\' to clear the current input statement.

mysql> create database discuz;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on discuz.* to \'root@localhost\' identified by \'19950801\';
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

在浏览器中要步骤完成安装

在浏览器中要步骤完成安装

apache用户认证

[root@tyrr www]# mkdir abc
[root@tyrr www]# cd abc/
[root@tyrr abc]# cp /etc/passwd .
[root@tyrr abc]# ls
passwd
[root@tyrr abc]# cd
[root@tyrr ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
---------------------------------
<VirtualHost *:80>
    DocumentRoot "/data/www"
    ServerName www.aaa.com
    ServerAlias www.bbb.com

    <Directory /data/www/abc>
        AllowOverride Authconfig
        AuthName "自定义"
        AuthType Basic
        AuthUserFile /data/.htpasswd
        require valid-user
    </Directory>
</VirtualHost>
----------------------------

[root@tyrr ~]# htpasswd -c /data/.htpasswd zty
New password: 
Re-type new password: 
Adding password for user zty
[root@tyrr ~]# htpasswd /data/.htpasswd zzty
New password: 
Re-type new password: 
Adding password for user zzty
[root@tyrr ~]# apachectl -t
Syntax OK
[root@tyrr ~]# apachectl restart

在浏览器中打开要访问的目录时 需输入用户名和密码才能正常访问

设置默认虚拟主机

[root@tyrr ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 
#第一个
<VirtualHost *:80>
    DocumentRoot "/mnt/123"
    ServerName asd.com
</VirtualHost>

[root@tyrr ~]# mkdir /mnt/123
[root@tyrr ~]# chmod 600 /mnt/123
[root@tyrr ~]# apachectl -t
Syntax OK
[root@tyrr ~]# apachectl restart

这样设置的好处是 只解析虚拟主机设置的域名 而不会 输入ip也直接访问虚拟主机

域名跳转

[root@tyrr ~]# apachectl -M |grep  rewrite
 rewrite_module (shared)
Syntax OK
[root@tyrr ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
#--------
<VirtualHost *:80>
    DocumentRoot "/data/www"
    ServerName www.aaa.com
    ServerAlias www.bbb.com
    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.aaa.com$
        RewriteRule ^/(.*)$ http://www.bbb.com/$1 [R=301,L]
    </IfModule>
    <Directory /data/www/abc>
    AllowOverride Authconfig
    AuthName "自定义"
    AuthType Basic
    AuthUserFile /data/.htpasswd
    require valid-user
    </Directory>
</VirtualHost>
#----------
[root@tyrr ~]# apachectl -t
Syntax OK
[root@tyrr ~]# apachectl restart
[root@tyrr ~]# curl -x192.168.74.129:80 www.aaa.com/asdasdasd
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://www.bbb.com/asdasdasd">here</a>.</p>
</body></html>
[root@tyrr ~]# curl -x192.168.74.129:80 www.aaa.com/asdasdasd -I
HTTP/1.1 301 Moved Permanently
Date: Sat, 04 Mar 2017 16:15:29 GMT
Server: Apache/2.2.32 (Unix) PHP/5.4.36
Location: http://www.bbb.com/asdasdasd
Content-Type: text/html; charset=iso-8859-1

http状态码为301 在浏览器中访问www.aaa.com下的任何内容时 都会跳转到www.bbb.com下相对应的资源

上一篇:【Ubuntu】使用初体验
下一篇:Core在IIS的热发布问题或者报错文件已在另一个程序中打开