Web Develop Environment - Apache in macOS Sierra 10.12
So recently I need to develop Hourglass website, in order to do so, I need to setup a local web server for that. I know that many people uses MAMP, however, I don’t like that. I think it installs way too much things compare to what I need, and a simple Apache in this case is what all I want.
So, I found this post, by @John Foderaro posted on Medium. It is very helpful, and below is just a summary of the important part in that post. If anything is confusing, you can always go to original post and take a look at the detail.
Create Site Folder
Create a site folder under home directory
1 | mkdir ~/Sites |
You can link any projects file using softlink
1 | cd ~/Sites |
Config Apache
The apache is already installed on Mac by default, you only need to config it properly to enable user directory sites. First if you don’t know your userId, use whoami command.
Apache Users Config File
Navigate to /etc/apache2/users and create a <username>.conf file under that directory. Note that whoami. Copy everything below into newly created file.
REMEMBER TO REPLACE
1 | <Directory "/Users/<username>/Sites/"> |
Give the file you just created a 644 permission
1 | sudo chmod 644 <username>.conf |
Modules Config
Now navigate to /etc/apache2, edit file httpd.conf. Before you edit that, it would be a good behaviour to make a backup of that file with extension .bak
1 | cd /etc/apache2 |
In that file, many modules are excluded with # comment tag at front. Here is a list of modules we want to enable. Some may already enabled some may not. Remove the # comment tag at front to enable them.
1 | LoadModule authz_host_module libexec/apache2/mod_authz_host.so |
If you want to enable php modules, when enable modules, also enable the following one. This is optional. By doing that you can now serve any files with the .php extension. If you don’t want to execute any php file, then ignore this step.
1 | LoadModule php5_module libexec/apache2/libphp5.so |
Once you finish search above lines, remove the prepending #, save and close httpd.conf
Edit Extra User Directory Configuration
Now navigate to /etc/apache2/extra and edit the file httpd-userdir.conf, also it is a good behaviour to make a backup of that file too.
1 | cd /etc/apache2/extra |
Uncomment the following line:
1 | Include /private/etc/apache2/users/*.conf |
Restarting Apache
1 | sudo apachectl restart |
Access Your Sites
Now you can access your website repository by going to http://localhost/~username, replace username with result from whoiam. Because we link different repositories into the root site folder, you will need to navigate into child folders to access different web repositories.
