Configure PHP and Virtual Host macOS High Sierra

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

  1. Edit /etc/apache2/httpd.conf
  2. Uncomment / remove remark of #LoadModule php7_module libexec/apache2/libphp7.so
  3. 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 :

  1. Enabling virtual host configuration in apache config by editing /etc/apache2/httpd.conf.

     $ sudo nano /etc/apache2/httpd.conf
    
  2. Uncomment the section Include /private/etc/apache2/extra/httpd-vhosts.conf, then save it.

  3. Create site directory. As an example in this post, let’s create a Site folder 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.php with simple PHP syntax.

     <?php
     phpinfo();
     ?>
    
  4. Create virtual host configuration by editing the virtual host config

     $ sudo nano /etc/apache2/extra/httpd-vhosts.conf
    

    The 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.io virtual host that refers to /Users/neutro/Sites/neutro.io as physical directory.

  5. Register domain for localhost

    Since we use neutro.io as domain for localhost, we need to add the domain and www alias to resolve to the localhost address by editing

        $ sudo nano /etc/hosts
    

    and add the following line

        127.0.0.1   neutro.io   www.neutro.io
    
  6. 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

  1. https://websitebeaver.com/set-up-localhost-on-macos-high-sierra-apache-mysql-and-php-7-with-sslhttps
  2. https://coolestguidesontheplanet.com/set-up-virtual-hosts-in-apache-on-macos-high-sierra-10-13/