[Versions]
OS: | Mac OS X 10.11.3 |
Apache: | 2.4.16 |
PHP: | 5.5.30 |
In order to develop and test web pages on the local web server, I googled some blogs to figure out what I have to do. It took me a couple of hours to search and test which approach is workable. Finally, I found that Configure Apache for PHP in OS X 10.10 Yosemite / 10.11 El Capitan written by Donat shows me how to set web pages up under home folder of the current user and Configuring Apache Virtual Hosts on Mac OS X posted by McCreary indicates how to set a domain name pointing to localhost. In this post, I am going to demonstrate how to setup both approach in my Mac.
Before Setup:
What have to be done:
Check the version of Apache by the following command:
sudo apachectl -v
Check the version of PHP by the following command:
sudo apachectl -v
Setup:
For the following demonstration, {username} represents the current username used to login operating system and {servername} represents the target hostname used to access the local website.
- Make sure there is a Sites folder in the home directory. If not, create it and ensure the permission is 644(dr-xr--r--). If it is not, use the following command to change it:
sudo chmod 644 Sites
- Make sure /etc/apache2/users/{username}.conf is existed. If not, create it. In addition, update this file as follows:
<Directory "/Users/{username}/Sites/"> Options Indexes Multiviews FollowSymLinks Require all granted AllowOverride All Order allow,deny Allow from all </Directory>
- Add virtual host into /etc/apache2/extra/httpd-vhosts.conf as following:
<VirtualHost *:80>
ServerName {servername}
DocumentRoot /Users/{username}/Sites
</VirtualHost>
Configuration:
- Uncomment the following lines in /etc/apache2/httpd.conf:
LoadModule deflate_module libexec/apache2/mod_deflate.so LoadModule userdir_module libexec/apache2/mod_userdir.so LoadModule rewrite_module libexec/apache2/mod_rewrite.so LoadModule php5_module libexec/apache2/libphp5.so Include /private/etc/apache2/extra/httpd-userdir.conf Include /private/etc/apache2/extra/httpd-vhosts.conf
- Then add index.php to the DirectoryIndex
- Uncomment the following line in /etc/apache2/extra/httpd-userdir.conf
- Restart Apache:
- If there is any problem happened, run the following command to see the error messages:
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
Include /private/etc/apache2/users/*.conf
sudo apachectl restart
sudo apachectl configtest
Test:
So far, the web server has been established and the pages can be accessed by the following URL:
http://localhost/~{username}
If there is no web page in this folder, it will show the index of web pages. Put the HTML file or the PHP file to test it.
Mapping to the specific domain name:
With regard to the URL shown above, it is not convenient for me to access every website with the long typing because I might have plenty of website on my local machine. In order to reduce the expenses of typing, I would rather use a shortened link to do it. The following steps show how to setup a specific domain name for the website.
- Open /etc/hosts as follows command:
sudo vim /etc/hosts
- Append the server name and target IP at the end of this file. In this case, 127.0.0.1 can be set for multiple server names.
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost 127.0.0.1 {servername}
- Clear the local DNS cache by the following command:
dscacheutil -flushcache
- Restart Apache.
sudo apachectl restart
Just type:
http://{servername}
[Reference]