Ubuntu下配置 Apache多站点(也叫虚拟主机)

1、首先配置host

用管理员权限打开Ubuntu的hosts文件(路径:/etc/hosts)

sudo vim /etc/hosts  //使用管理员权限sudo,用vim编辑器打开hosts文件进行编辑

在host文件的最下面添加 127.0.0.1 域名

127.0.0.1	localhost
127.0.1.1	dev-pc

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

127.0.0.1	www.testxmj.com
#在此处添加,我添加的域名是 www.testxmj.com

2、添加虚拟主机配置

打开 /etc/apache2/sites-available 文件夹,将其中的 000-default.conf 文件(该文件是Apache的例子文件,方便我们用来配置的)复制并重命名粘贴在当前文件夹中。

cd /etc/apache2/sites-available   //进入目录
sudo cp 000-default.conf test.conf  //复制并重命名为test.conf
sudo vim test.conf  //编辑test.conf,使用vim,vi,nano编辑器都可以

要编辑的内容如下:

<VirtualHost *:80>
    #管理员的邮箱,可不加
    #ServerAdmin [email protected]
    #服务器的名称,修改为 上一步hosts文件添加的域名,最好都加上www
    ServerName www.testxmj.com
    #服务器的别名,使用别名也可以访问到,可不加
    ServerAlias testxmj.com
    #虚拟主机的文件根目录,项目的目录,Ubuntu一般都在/var/www/html下
    DocumentRoot /var/www/html/estate_macau/public
    #打开站点/虚拟主机后,优先显示的页面
    DirectoryIndex index.php index.html index.htm Welcome.html 
    #错误日志的存放地址
    ErrorLog ${APACHE_LOG_DIR}/error.log
    #日志
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
题外:其中的DirectoryIndex (目录索引)作用:修改Apache默认起始/索引页面(localhost默认的打开页面)
①、Linux下:如上图
②、win下:需要修改httpd.conf文件

路径:(在apache的安装目录下)…/apache/conf/httpd.conf

<IfModule dir_module>
    DirectoryIndex index.php index.html index.htm
</IfModule>

Apache默认索引/打开(浏览器输入localhost打开)的页面是index.html,想修改成其他文件,需要修改DirectoryIndex后面的文件夹名称,然后重启Apache:
Apache会根据DirectoryIndex后面写的文件名,按从左到右的顺序在项目根目录查找同名文件。比如上图,如果找到index.php即加载index.php,没有的话,继续往右查找是否有index.html文件,有的话加载index.html,没有的话继续,以此类推。。。

如果上述设置后仍无法按顺序运行首页,那么还得修改一下php相关配置文件php.conf(路径: /etc/httpd/conf.d/php.conf)
找到DirectoryIndex项,修改同上。

3、将改文件连接到 sites-enabled 文集夹

sudo ln -s /etc/apache2/sites-available/test.conf  /etc/apache2/sites-enabled/   
//将改好的 test.conf 文件链接到sites-enabled文件夹
sudo /etc/init.d/apache2 restart   //重启服务即可

4、进行测试

输入我们添加的域名 www.testxmj.com 或者别名:testxmj.com 进行测试

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章