Mac自带的Apache2增加自定义目录部署静态网站

Version: Apache2

Commands:
sudo apachectl start
sudo apachectl stop
sudo apachectl -k restart
相关文件夹Path

安装目录:/etc/apache2
默认放网页地址:/Library/WebServer/Documents
访问地址:http://localhost
自定义放网页地址:/Users/<your short user name>/Sites/
访问地址:http://localhost/~<your short user name>

增加自定义放网页的目录

  1. 创建目录
    mkdir ~/Sites

  2. 创建一个网页

echo "<html><body><h1>My site works!</h1></body></html>" > ~/Sites/index.html.en
  1. 打开配置文件sudo vi /etc/apache2/httpd.conf,做修改

    1. Uncomment the following line:
      #LoadModule php5_module libexec/apache2/libphp5.so
      to
      LoadModule php5_module libexec/apache2/libphp5.so

    2. Uncomment the following line:
      #LoadModule userdir_module libexec/apache2/mod_userdir.so
      to
      LoadModule userdir_module libexec/apache2/mod_userdir.so

    3. Uncomment the following line:
      #Include /private/etc/apache2/extra/httpd-userdir.conf
      to
      Include /private/etc/apache2/extra/httpd-userdir.conf

    4. 保存文件/etc/apache2/httpd.conf

  2. 打开配置文件sudo vi /etc/apache2/extra/httpd-userdir.conf,作修改

    1. uncomment the following line:
      #Include /private/etc/apache2/users/*.conf
      to
      Include /private/etc/apache2/users/*.conf

    2. 保存文件/etc/apache2/extra/httpd-userdir.conf

  3. 打开配置文件sudo vi /etc/apache2/users/<your short user name>.conf,作修改

    1. 添加内容
    <Directory "/Users/<your short user name>/Sites/">
        Options Indexes MultiViews
        AllowOverride None
        Order allow,deny
        Allow from localhost
        Require all granted
    </Directory>
    
    1. 保存文件/etc/apache2/users/<your short user name>.conf
  4. 测试

    1. 默认放网页地址:/Library/WebServer/Documents
      访问地址:http://localhost
      网页显示:
It works!
1. 自定义放网页地址:```/Users/<your short user name>/Sites/```  

访问地址:http://localhost/~<your short user name>
Note:注意不要忘了波浪号~
网页显示:

My site works!
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容