Ubuntu Linux
方法一
一、修改/etc/apache2/sites-available/
1. 打开目录 /etc/apache2/sites-available/, 发现 default 和 default-ssl 两个文件, 其中 default 是 http 虚拟主机服务的配置文件, default-ssl 是配置 https 服务使用的. 可以复制一份 default 文件. 并修改配置文件名, 文件名必须与域名一致 (如:www.1117.com)
2. 打开新建的配置文件, 修改 DocumentRoot, ServerName 和对应的配置目录. 例子如下:
# # DocumentRoot 是网站文件存放的根目录 # ServerName 是网站域名, 需要跟 DNS 指向的域名一致 # <VirtualHost *:最好自定义端口(如801、802)不要和80冲突> ServerAdmin webmaster@dummy-host.example.com DocumentRoot /var/www/1117/ ServerName www.1117.com ErrorLog ${APACHE_LOG_DIR}/www.1117.com-error.log CustomLog ${APACHE_LOG_DIR}/www.1117.com-access.log combined </VirtualHost> |
sudo a2ensite www.1117.com.com
|
sudo a2dissite www.1117.com.com
|
sudo /etc/init.d/apache2 restart
|
二、配置/etc/hosts 下的主机hosts的文件
1、修改hosts (ubuntu下只能使用特级权限打开编写)
sudo gedit /etc/hosts
2.添加
127.0.0.1 www.1117.com
先保存、后重启服务器
sudo /etc/init.d/apache2 restart
方法二、
一、虚拟目录
现场模拟:
本来的访问路径为:localhost/demo/demo1
想把访问路径改为:localhost:701/demo1
1、找到sites-available目录
cd /etc/apache2/sites-available
2、创建一个文档demo,把default文档的内容拷贝过去
default文档内容如下(需要修改的地方用红色标记):
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
3、修改
<VirtualHost *:80>改为<VirtualHost *:701>
DocumentRoot /var/www修改成DocumentRoot
/var/www/demo
<Directory
/var/www/>修改成<Directory /var/www/demo>
4、链接到sites-enabled
sudo ln -s /etc/apache2/sites-available/demo /etc/apache2/sites-enabled/demo
5、修改端口监听文件
打开/etc/apache2的ports.conf,找到NameVirtualHost *:80,在下行添加Listen 801,即
NameVirtualHost *:80
Listen 80
Listen 80
NameVirtualHost *:801
Listen 801
Listen 801
6、重启rewrite服务,重启apache2
sudo a2ensite demo
sudo a2enmod rewrite
sudo service apache2 restart 或者 sudo apachectl restart
ok
二、设置虚拟域名
现场模拟:将localhost改成www.dxw.com
1、打开/etc/的hosts文件
2、在文档上面相应位置加上一行
127.0.0.1 www.dxw.com
ok