Tuesday, July 3, 2018

Enable the "Sites" to be "http://localhost/~username" on macOS

Enable the "Sites" to be "http://localhost/~username" on macOS

There are actually two places where macOS serves website by default:

However, the second one needs setups to activate.

To make http://localhost/~username/ work, just follow the procedures below to configure:

1. Add a user’s config file /etc/apache2/users/username.conf.

<Directory "/Users/username/Sites/">
    Options Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

2. Turning on Modules in /etc/apache2/httpd.conf

First, copy a httpd.conf backup.

$ sudo cp httpd.conf httpd.conf.bak

Then edit the httpd.conf.

$ sudo vi httpd.conf

Uncomment the following lines:
( remove “#” at the beginning of the line.)

LoadModule authz_host_module libexec/apache2/mod_authz_host.so
LoadModule authz_core_module libexec/apache2/mod_authz_core.so
LoadModule userdir_module libexec/apache2/mod_userdir.so
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so

Include /private/etc/apache2/extra/httpd-userdir.conf
Include /private/etc/apache2/extra/httpd-vhosts.conf

Save and close vi while finished.

3. Edit /etc/apache2/extra/httpd-userdir.conf.

Go to /etc/apache2/extra/ folder and make a backup of httpd-userdir.conf.

$ sudo cp httpd-userdir.conf httpd-userdir.conf.bak

Then edit httpd-userdir.conf.

$ sudo vi httpd-userdir.conf

Uncomment the following line:
( remove “#” at the beginning of the line.)

Include /private/etc/apache2/users/*.conf

Save and close vi while finished.

4. Restart Apache

$ sudo apachectl restart

Type http://localhost/~username/index.html.en in the browser, it works! ( where username is the user’s name. )

Then type http://localhost/~username in the browser, it shows the index of the “Sites” and lists all files in the “Sites” directory. Now, I can put my web site in the “Sites” directory. Great!

No comments:

Post a Comment