Setting up Virtual Hosts in Apache on on macOS Big Sur is straight forward after you have your local Web Development environment up and running – get your web development up and running first including Apache, PHP and MySQL on macOS following this macOS Big Sur Apache/PHP/MySQL guide here if required.
- Install Apache On Macos Catalina
- Apache Macos Catalina Update
- Restart Apache Mac Catalina
- Apache Macos Catalina Download
- Disable Apache Mac Catalina
- Apache Macos Catalina Free
- Macos Catalina Apache
This guide will also work in macOS Catalina, Mojave, High Sierra and Sierra.
If you have installed Apache, PHP, and MySQL for Mac OS Mojave, read my post on Updating Apache, PHP, and MySQL for macOS Catalina. I am aware of the web server software available for macOS, notably MAMP, as well as package managers like brew. Question or issue on macOS: I have Apache webserver (the one supplied by Apple) running on my Macbook and it ran flawlessly, until I upgraded to Catalina 10.15. I used the same httpd.conf, but now it is displaying You don’t have permission to access this resource.Server unable to read htaccess file, denying access to be.
The process of setting up Virtual Hosts is done easier in the Terminal either using nano or vi with sudo or as a root user, or you can you a GUI visual editor like BBEdit which allows access to the /private/etc directory by clicking ‘Show Everything” in the open dialog box.
Allow the vhosts configuration from the Apache configuration file httpd.conf
Open the httpd.conf
Search for ‘vhosts‘ and uncomment the include line
Edit the vhosts.conf file
Open this file to add in the virtual host.
An example domain in the file is given of the format required to add in additional domains, just follow this to create your new virtual host:
We can take this example and extend on it, if you wanted a domain named apple.com for example, you can copy the existing text block and edit to suit:
So in the example above a vhost for apple.com is created and the document root is in the Sites/apple folder, in the text block above I have also added in some log files, what you need to change is the document root location username and domain name to suit your needs. Finish and save the file.
Now also you need to map the IP address to be the localhost.
Map Your IP address to localhost
Add the Domain and ‘www‘ alias to resolve to the localhost address
Restart Apache
Check out your local vhost domain in the browser
Losing Localhost
One caveat to note about virtual hosts is that once set up you lose your older document root previously at /Library/WebServer/Documents or accessed in the browser at http://localhost what happens is that you get a 403 Forbidden Error. But the ~/username document root is still compatible.
To get around this, you need to add in a vhost for localhost and declare this vhost before any of the others, in the same file:
Install Apache On Macos Catalina
Add in:
Apache Macos Catalina Update
Restart Apache
Changing the WebServer Default User
One of the frustrations of using the Users/username/Sites folder for vhosts is the permissions issues with things like updates and authentication.
This is because the default webserver user which runs httpd is known as _www, which will not be the user in your local account. If your machine is only in use by you and the webserver will run only under your account then you can change the user.
Find Your User and Group
In the Terminal use the id command to see your username and group
You will get a bunch of user groups, you need your primary user uid and group gid names
Change this back in /etc/apache2/httpd.conf
Restart Apache
Restart Apache and now you are running httpd as your local account.
Restart Apache Mac Catalina
That’s it, the guide will also work on macOS Catalina, Mojave, High Sierra and Sierra.
In this tutorial we will learn to install Apache, MySQL, PHP on macOS Catalina 10.15.
About macOS Catalina
Apple released macOS Catalina 10.15 on 7th October 2019 and it includes Apache and PHP. So, all we have to do is enable them. Then install MySQL and we are ready for development. So, lets get started.
Note! Support for 32 bit apps is removed in Catalina so, all your 32 bit applications will no longer work. Kindly upgrade your applications.
Apple has also made zsh as the default shell. You can learn more about it here.
To change the default shell check the tutorial How to change default shell to bash on macOS Catalina.
We will be using the pre-installed Apache and PHP and we will download and setup MySQL database.
If you are using macOS Mojave then check out this tutorial How to install Apache, MySQL, PHP on macOS Mojave 10.14
Lets go ahead and configure our LAMP stack development environment on macOS Catalina.
Apache
The new macOS Catalina comes with Apache pre-installed. All we have to do is switch it on.
Open Terminal using macOS Spotlight or go to /Applications/Utilities and open Terminal.
To check the version of Apache installed run the following command in the Terminal.
Note! macOS Catalina comes with Apache 2.4.41
To start Apache web server run the following command.
This command will start Apache server.
When you use sudo
in the terminal then you will be prompted to enter your admin password to proceed.
If you want to stop the Apache server then run the following command.
And to restart Apache server run the following command.
After starting Apache server go ahead and test it by opening a browser like Safari or Chrome and visit http://localhost
.
You will get to see the following output in your browser.
By default, the file is getting served from /Library/WebServer/Documents
directory. We will change this is the following sections.
In case you don't get to see the above output then run the following command to check the error.
Document Root
This is the location in the computer file system from where the files are accessed when we visit the http://localhost in a browser.
Document Root is a directory where we put our website files.
On Mac we have two document root. One is at the system level and the other is at the user level.
System level document root
The system level document root in macOS Catalina is located in the following directory.
User level document root
For the user level we can create a directory called Sites
in user directory. So, open Terminal and get ready to create the user level document root directory.
Creating Sites
directory
Run the following command in the Terminal to switch to the user home directory.
Now, run the following command to create the Sites
directory.
You can merge the above two commands into one by running the following command.
For me the user document root path is the following.
You will get a similar path for your Mac.
Creating username.conf
file
Now, we will create a username.conf
file to configure our document root.
Note! Replace the username with your username.
For example, my username is yusufshakeel so, my file is yusufshakeel.conf
.
Type the whoami
command in the terminal and it will tell you your username.
Open terminal and go to the following directory.
Now create the configuration file username.conf inside the users directory.
In the following example I am using vi editor. You can use other editors like vim or nano.
Press the i key to enter into INSERT mode and then type the following in the file.
Don't forget to replace YOUR_USER_NAME
with your username.
Here is what I have written in my yusufshakeel.conf
file.
Now, come out of the INSERT mode by pressing the Esc key. Now save the file and exit by typing the following :wq and then hit Enter.
The permission of this file should be the following.
You can change the permission by typing the following command.
Where, username is your username that you have set.
Configuring the httpd.conf
file
Now we switch to apache2 directory by typing the following command in the Terminal.
Inside this directory we have the httpd.conf
file.
As a good practice we will make a backup copy of the httpd.conf
file by typing the following command.
Now open the httpd.conf file using vi
and uncomment the following lines.
To uncomment the lines remove the #
from the start of the line.
Uncomment the following line for User home directories.
Now change the DocumentRoot.
Find the following lines and comment them by adding #
at the beginning of the line.
Apache Macos Catalina Download
And add the following two lines below the commented lines.
Don't forget to replace YOUR_USERNAME
with your username.
And set the AllowOverride None
to AllowOverride All
.
Your DocumentRoot should now look something like the following.
Now, come out of the INSERT mode by pressing the Esc key. And save and exit the file by typing :wq key and then Enter.
Configuring the httpd-userdir.conf
file
Now, time to make some changes in the httpd-userdir.conf file.
In the terminal type the following command to go to the extra directory.
As a good practice create a backup copy of the httpd-userdir.conf
file.
Now open the file using vi
.
Enter into INSERT mode and uncomment the following line.
Now, come out of the INSERT mode and save the changes.
Now, check if everything is configured properly by typing the given command.
Now restart Apache using the following command.
PHP
macOS Catalina comes with PHP 7.3.8 pre-installed.
To check the version of PHP in the Terminal type the following command.
Now we go to the apache2 directory.
Next, we open the httpd.conf
file.
Now, enter into INSERT mode and uncomment the following line to run PHP 7 by removing the #
sign from the start of the line.
Now, save the changes and exit the file by typing :wq keys and hit Enter.
Disable Apache Mac Catalina
You can now restart Apache by running the following command.
Printing phpinfo
Create index.php
file inside the Sites directory.
Now open the file in your favourite text editor or PHP IDE and write the following code.
Now, visit http://localhost
and you will get to see a similar output.
Download MySQL community server
Head over to mysql.com website and download the latest version of the MySQL Community Server.
Apache Macos Catalina Free
Download and run the installer and follow the steps to install MySQL database on your Mac.
Set the root password when prompted and note it down.
To run MySQL server open System Preferences and go to MySQL.
Click on the Start MySQL Server button to start the server.
Macos Catalina Apache
You will be asked to enter your admin password. Enter the password and the MySQL server will start running.
Extra
To access your MySQL database tables you can either use phpMyAdmin, MySQL Workbench or Sequel Pro.
Alright, this brings us to the end of this tutorial. Hope this helped. Please share if you find this website useful. Have fun developing. See you in the next tutorial.