Ubuntu 14.04 LTS Apache2 设置虚拟主机

来源:互联网 发布:如何修改sql数据库名称 编辑:程序博客网 时间:2024/05/16 12:19

为了实现在本地局域网使用虚拟域名访问服务器(因为一般情况下都用IP地址访问)。

1. 创建目录

Apache默认的文件路径是 /var/www/html/,现在在 /var/www/ 目录下新建一个 zhangyi.geek.com 目录,目录下面写一个 index.html 文件。

2. 修改apache配置文件

进入配置文件目录:/etc/apache2/sites-available ,将 000-default.conf 配置文件拷贝一份。

cp 000-default.conf zhangyi.geek.com.conf

修改新建的配置文件:sudo vi zhangyi.geek.com.conf

<VirtualHost *:80>    # The ServerName directive sets the request scheme, hostname and port that    # the server uses to identify itself. This is used when creating    # redirection URLs. In the context of virtual hosts, the ServerName    # specifies what hostname must appear in the request's Host: header to    # match this virtual host. For the default virtual host (this file) this    # value is not decisive as it is used as a last resort host regardless.    # However, you must set it for any further virtual host explicitly.    #ServerName www.example.com    ServerName zhangyi.geek.com    ServerAdmin webmaster@zhangyi.geek.com    DocumentRoot /var/www/zhangyi.geek.com    ServerAlias www.zhangyi.geek.com    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,    # error, crit, alert, emerg.    # It is also possible to configure the loglevel for particular    # modules, e.g.    #LogLevel info ssl:warn    ErrorLog ${APACHE_LOG_DIR}/error.log    CustomLog ${APACHE_LOG_DIR}/access.log combined    # For most configuration files from conf-available/, which are    # enabled or disabled at a global level, it is possible to    # include a line for only one particular virtual host. For example the    # following line enables the CGI configuration for this host only    # after it has been globally disabled with "a2disconf".    #Include conf-available/serve-cgi-bin.conf</VirtualHost># vim: syntax=apache ts=4 sw=4 sts=4 sr noet


3. 链接配置文件

sudo ln zhangyi.geek.com.conf ../sites-enabled/zhangyi.geek.com.conf


4. 重启apache

sudo service apache2 restart


5. 远程访问

配置好以后,打开本地浏览器,输入 zhangyi.geek.com 就可以访问了。 

局域网其他用户访问, 修改 host 文件。

打开文件:sudo vi /etc/host

添加一行:192.168.1.30 zhangyi.geek.com 即可!


6. 问题解决

6.1

问题:重启 apache 出现127.0.1.1 警告。。。

解决:sudo vi /etc/apache2/apache2.conf  添加一行 ServerName localhost ,在重启就不会报警告了。

6.2 

问题:访问不到 或者 内容为空

解决:可能是文件权限的问题。

sudo chown -R $USER:$USER /var/www/zhangyi.geek.com

sudo chmod -R 755 /var/www/


参考文章:https://linux.cn/article-3164-1.html#4_4095

0 0