PHP Introduction | Server Setup | Using PHP on Mac OS
Mac OS includes Apache by default, so PHP can be used immediately by changing the configuration without installing a separate web server.
Check the Apache Version
If Apache and PHP are already installed, you can check their versions with the following commands.
$ apachectl -v
$ php -v
If these commands show version information, you can start the server.
Start Apache
Use the following command to start Apache.
$ sudo apachectl start
Starting Apache requires administrator privileges, so add sudo at the beginning of the command. After entering the command, you will be prompted for a password. Enter the administrator password.
Other Commands
Use the following commands to stop or restart Apache.
$ sudo apachectl stop
$ sudo apachectl restart
Confirm That Apache Is Running
After starting Apache, open a browser and enter http://localhost or http://127.0.0.1. You should see the following text.
It works!
This appears because the browser has opened the default index page. The default index page is in the /Library/WebServer/Documents folder, because that folder is configured as the default DocumentRoot.
If you open that folder, you can confirm that the index.html.en file exists.
Configure the Apache Web Server Environment
To change Apache’s basic settings, edit /etc/apache2/httpd.conf.
Change the DocumentRoot Folder
Earlier, the index.html.en file was described as being in /Library/WebServer/Documents/. This folder is configured as the default DocumentRoot.
To change the default DocumentRoot, open the httpd.conf file and edit the DocumentRoot line and the Directory line directly below it.
...
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
...
To edit httpd.conf, you must open the file with administrator privileges. However, changing DocumentRoot is not especially recommended. It is usually better to enable userdir instead of changing DocumentRoot.
Edit httpd-userdir.conf to Enable userdir
Enabling userdir means that you can use a URL based on your own account name in the browser, such as localhost/~username/.
To enable userdir, edit the httpd-userdir.conf file in the /etc/apache2/extra/ folder.
First, move to that folder and edit the file with the following command.
$ sudo vi httpd-userdir.conf
As the command shows, server configuration files can only be edited with administrator privileges. The file contains the following content.
... omitted ...
#
# Control access to UserDir directories. The following is an example
# for a site where these directories are restricted to read-only.
#
Include /private/etc/apache2/users/*.conf
<IfModule bonjour_module>
RegisterUserSite customized-users
</IfModule>
For now, remove the comment marker (#) from the Include /private/etc/apache2/users/*.conf line and save the file.
Edit httpd.conf to Enable userdir
The /etc/apache2/extra/httpd-userdir.conf file contains comments saying that the following three modules are required: mod_authz_core, mod_authz_host, and mod_userdir.
# Settings for user home directories
#
# Required module: mod_authz_core, mod_authz_host, mod_userdir
Remove the comments from the corresponding LoadModule settings in httpd.conf.
72 LoadModule authz_host_module libexec/apache2/mod_authz_host.so
78 LoadModule authz_core_module libexec/apache2/mod_authz_core.so
166 LoadModule userdir_module libexec/apache2/mod_userdir.so
- The default line numbers are 72, 78, and 166, but they may differ.
- Since Yosemite, the comments for the
mod_authz_hostandmod_authz_coremodules are usually already removed and the modules are already enabled. - Therefore, in many cases you only need to remove the comment for the
mod_userdirmodule.
Next, remove the comment from the following line so that httpd-userdir.conf is included.
493 Include /private/etc/apache2/extra/httpd-userdir.conf
- The default line number is 493, but it may differ.
Create a {username}.conf File to Enable userdir
Create a {username}.conf file in the /private/etc/apache2/users folder to specify access to the Sites directory. The content is as follows.
{username}is your user account name, so enter your own account name.
<Directory "/Users/{username}/Sites/">
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
Write the permission settings according to the Apache server version. The example above is configured for version 2.4.
Create a Sites Folder in the Home Directory
Finally, move to your home directory and create a Sites folder.
$ cd ~
$ mkdir Sites
If you check it in Finder, you can see that it is automatically recognized as a website folder.
If you create your own index.html file in this folder, it will be displayed as the first page. If there is no index.html, Apache’s default file index is opened.
Restart Apache
After changing Apache settings, restart Apache.
$ sudo apachectl restart
Now access localhost/~{username} and you will see the index.html file in the Sites folder.
Configure the PHP Web Server Environment
In the Apache web server configuration file (/etc/apache2/httpd.conf), remove the comment marker (#) from the PHP 5 module line below, then start Apache. If Apache is already running, restart it.
169 LoadModule php5_module libexec/apache2/libphp5.so
This loads the PHP 5 module.