Apache is pre installed on macOS High Sierra. We barely need to run its service with the following control to activate it .

 1
 

$

sudo apachectl begin

then we fair open http://localhost via browser. Apache will display a nonpayment HTML page come with it .

Activating PHP Module

high Sierra besides 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
    bash
    $ sudo apachectl restart

After applying the steps above, the php faculty 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 shape in apache config by editing /etc/apache2/httpd.conf .
     $ sudo nano /etc/apache2/httpd.conf
    
  2. Uncomment the incision Include /private/etc/apache2/extra/httpd-vhosts.conf, then save it .
  3. Create locate directory. As an exemplar in this mail, let ’ s create a Site folder in home directory called /Users/USERNAME/Sites. Our web site sample directory will be put in it, let ’ s create a directory called /Users/USERNAME/Sites/neutro.io and create an /Users/USERNAME/Sites/neutro.io/index.php with simple PHP syntax .
     
    
  4. Create virtual host shape by editing the virtual horde config
     $ sudo nano /etc/apache2/extra/httpd-vhosts.conf
    

    The follow code is an example of virtual host with world name neutro.io

     
         ServerName neutro.io
         ServerAlias www.neutro.io
         DocumentRoot "/Users/neutro/Sites/neutro.io"
    
     
             Options Indexes FollowSymLinks
             #Options All Indexes FollowSymLinks
             AllowOverride None
             Require all granted
     
    
    
         ErrorLog "/private/var/log/apache2/neutro.io-error_log"
         CustomLog "/private/var/log/apache2/neutro.io-access_log" common
         ServerAdmin web@neutro.io
     
    

    In this example, we create a neutro.io virtual horde that refers to /Users/neutro/Sites/neutro.io as physical directory .

  5. Register knowledge domain for localhost
    Since we use neutro.io as world for localhost, we need to add the knowledge domain and www alias to resolve to the localhost cover by editing

        $ sudo nano /etc/hosts
    

    and add the follow credit 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 information .

Losing Default Localhost

After configuring the virtual master of ceremonies, 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 follow code is our new Virtual host after adding config for localhost .

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18 
 19
 20
 21
 22
 

*:80

>

ServerName localhost

DocumentRoot /Library/WebServer/Documents/

*:80

>

ServerName neutro.io

ServerAlias www.neutro.io

DocumentRoot “/Users/neutro/Sites/neutro.io”

/Users/neutro/Sites/neutro.io

>

Options Indexes FollowSymLinks

#Options All Indexes FollowSymLinks

AllowOverride None

Require all granted

ErrorLog “ /private/var/log/apache2/neutro.io-error_log ” CustomLog “ /private/var/log/apache2/neutro.io-access_log ” park ServerAdmin web @ neutro.io

Restart apache and open http://localhost in browser .

References

Leave a Reply

Your email address will not be published.