Apache is pre installed on macOS High Sierra. We just need to run its service with the following command to activate it.
$ sudo apachectl start
Then we just open http://localhost via browser. Apache will display a default HTML page come with it.
Activating PHP Module
High Sierra also comes with PHP 7. So we don’t need to install it manually. To activate PHP module
- Edit
/etc/apache2/httpd.conf - Uncomment / remove remark of
#LoadModule php7_module libexec/apache2/libphp7.so - Save it and restart apache using
$ sudo apachectl restart
After applying the steps above, the php module should be activated and ready to use. In this post we will test it after configuring virtual host.
Configuring Virtual Host
The steps of configuring apache virtual host are :
-
Enabling virtual host configuration in apache config by editing
/etc/apache2/httpd.conf.$ sudo nano /etc/apache2/httpd.conf -
Uncomment the section
Include /private/etc/apache2/extra/httpd-vhosts.conf, then save it. -
Create site directory. As an example in this post, let’s create a
Sitefolder in home directory called </code>/Users/USERNAME/Sites</code>. Our website sample directory will be put in it, let’s create a directory called </code>/Users/USERNAME/Sites/neutro.io</code> and create an/Users/USERNAME/Sites/neutro.io/index.phpwith simple PHP syntax.<?php phpinfo(); ?> -
Create virtual host configuration by editing the virtual host config
$ sudo nano /etc/apache2/extra/httpd-vhosts.confThe following code is an example of virtual host with domain name
neutro.io<VirtualHost *:80> ServerName neutro.io ServerAlias www.neutro.io DocumentRoot "/Users/neutro/Sites/neutro.io" <Directory /Users/neutro/Sites/neutro.io> Options Indexes FollowSymLinks #Options All Indexes FollowSymLinks AllowOverride None Require all granted </Directory> ErrorLog "/private/var/log/apache2/neutro.io-error_log" CustomLog "/private/var/log/apache2/neutro.io-access_log" common ServerAdmin web@neutro.io </VirtualHost>In this example, we create a
neutro.iovirtual host that refers to/Users/neutro/Sites/neutro.ioas physical directory. -
Register domain for localhost
Since we use
neutro.ioas domain for localhost, we need to add the domain andwwwalias to resolve to the localhost address by editing$ sudo nano /etc/hostsand add the following line
127.0.0.1 neutro.io www.neutro.io -
Restart apache
$ sudo apachectl restart
When we open in browser http://neutro.io, we should get a page that display PHP info.
Losing Default Localhost
After configuring the virtual host, we may lose the previous default localhost that points to /Library/WebServer/Documents/ directory. We may get a 403 Forbidden Error when visiting http://localhost. To get around this, we need to add in a vhost for localhost and declare this vhost before any of the others. The following code is our new Virtual host after adding config for localhost.
<VirtualHost *:80>
ServerName localhost
DocumentRoot /Library/WebServer/Documents/
</VirtualHost>
<VirtualHost *:80>
ServerName neutro.io
ServerAlias www.neutro.io
DocumentRoot "/Users/neutro/Sites/neutro.io"
<Directory /Users/neutro/Sites/neutro.io>
Options Indexes FollowSymLinks
#Options All Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog "/private/var/log/apache2/neutro.io-error_log"
CustomLog "/private/var/log/apache2/neutro.io-access_log" common
ServerAdmin web@neutro.io
</VirtualHost>
Restart apache and open http://localhost in browser.
References
- https://websitebeaver.com/set-up-localhost-on-macos-high-sierra-apache-mysql-and-php-7-with-sslhttps
- https://coolestguidesontheplanet.com/set-up-virtual-hosts-in-apache-on-macos-high-sierra-10-13/