How to set up the Virtual Host on MacOS

How to set up the Virtual Host on MacOS

Posted on:February 24, 2020 at 10:00 AM

Hello

If you want to set up the Virtual Host on Mac OS, then this post might be helpful for you. I have a local project on PHP where I want to set the virtual host for that.

For example, I want to set http://tisuchi.bd as my domain instead of localhost/site-project.

Table of Contents

Open Table of Contents

1. Activate VirtualHost on Apache

First, uncomment the following line (remove the #) in /private/etc/apache2/httpd.conf

Run sudo nano /private/etc/apache2/httpd.conf first, and then remove # from the beginning of the line.

#Include /private/etc/apache2/extra/httpd-vhosts.conf

2. Map your domain first

To map your domain http://tisuchi.bd you need to open /etc/hosts file. To do so, run the following command on the terminal.

sudo nano /etc/hosts

Then add the following line on the file-

127.0.0.1       tisuchi.bd

3. Add the virtual host in apache

Add the following VHost entry to the /private/etc/apache2/extra/httpd-vhosts.conf file-

<VirtualHost *:80>
    DocumentRoot "/Users/username/site-project"
    ServerName tisuchi.bd
</VirtualHost>

4. Restart Apache

To restart apache, you need to run the following command.

sudo apachectl restart

Note: Some other apache commands.

You may need some other apache commands.

sudo apachectl start
sudo apachectl stop

5. Run PHP Server

Run the php server locally by running the following command-

php -S 127.0.0.1:80

Now, if you visit http://tisuchi.bd on your machine, you should able to see your site.

Forbidden 403, You don’t have permission to access /~username/ on this server

If you have issue of Forbidden 403, You don’t have permission to access /~username/ on this server , then

  • Go to cd /etc/apache2/users/
  • Run ls to check whether you have {Username}.conf file or not.
  • If there is no file {Username}.conf, then you need to add a file by using this command sudo nano Username.conf.
  • Then add the following code
<Directory "/Users/username/site-project/">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
  • Once you have saved successfully, if you run this command la, you should able to see something like this-
-rw-r--r--   1 root  wheel  298 Feb 24 16:47 username.conf

Finally, run sudo apachectl restart to restart apache.

Hope it will work for you.

Thank you.