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

配置虚拟主机安装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下相对应的资源

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对你的问题:APACHE虚拟主机配置,以下是回答: 在Apache配置虚拟主机需要在httpd.conf或者其他的配置文件中添加如下代码: ``` <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/usr/local/apache2/htdocs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "/usr/local/apache2/logs/dummy-host.example.com-error_log" CustomLog "/usr/local/apache2/logs/dummy-host.example.com-access_log" common </VirtualHost> ``` 其中`*`表示监听所有的网络接口,`:80`表示监听80端口,`ServerAdmin`设置管理员邮箱,`DocumentRoot`设置虚拟主机的目录,`ServerName`设置虚拟主机名字,`ServerAlias`设置虚拟主机别名,`ErrorLog`和`CustomLog` 分别设置虚拟主机的错误日志和访问日志。 如果需要配置HTTPS,则需要在虚拟主机配置中添加以下代码: ``` <VirtualHost *:443> ServerAdmin [email protected] DocumentRoot "/usr/local/apache2/htdocs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "/usr/local/apache2/logs/dummy-host.example.com-error_log" CustomLog "/usr/local/apache2/logs/dummy-host.example.com-access_log" common SSLEngine on SSLCertificateFile "/path/to/www.example.com.cert" SSLCertificateKeyFile "/path/to/www.example.com.key" SSLCACertificateFile "/path/to/www.example.com.chain" </VirtualHost> ``` 在HTTPS虚拟主机配置中,需要添加以下代码: `SSLEngine on`表示开启SSL加密引擎,`SSLCertificateFile`指定证书位置,`SSLCertificateKeyFile`指定私钥位置,`SSLCACertificateFile`指定证书链位置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值