• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Archives

virtual host

Apache Virtual Host on Mac

December 26, 2013

Creating an Apache virtual host on your Mac is quite easy. Assuming you already have Apache installed and running on your Mac, you will now have to decide whether to serve your pages from the default web root, or create your own virtual host, and serve your pages from an alternate location.

This article is part of a series of articles on how to manually install MAMP or Apache, PHP, MySQL on the Mac OS. Installing the popular MAMP application is as simple as installing any other application on the Mac, but it will not give you any valuable learning experience, nor will you have complete control of your development environment. I recommend manually installing the individual applications to maximize your learning experience.

Create an Apache Virtual Host on Mac OS X

Edit the /etc/apache2/httpd.conf file using the nano editor.

$ sudo nano /etc/apache2/httpd.conf

$ sudo nano /etc/apache2/httpd.conf

Look for the text ‘# Virtual hosts’ located at the end of the file. Uncomment by removing # from the line that says:

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

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

Save file and exit. If your not familiar with nano, it’s Ctrl-O for save, and Ctrl-X for exit.

Now edit the https-vhosts.conf file.

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

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

Append the following at the end of the file:

<VirtualHost *:80>
	DocumentRoot "/Users/Ulysses/Sites/"
	ServerName ulysses.dev
	ErrorLog "/private/var/log/apache2/ulysses.dev.local-error_log"
	CustomLog "/private/var/log/apache2/ulysses.dev.local-access_log" common
	<Directory "/Users/Ulysses/Sites/">
		AllowOverride All
		Order allow,deny
		Allow from all
	</Directory>
</VirtualHost>

<VirtualHost *:80> DocumentRoot "/Users/Ulysses/Sites/" ServerName ulysses.dev ErrorLog "/private/var/log/apache2/ulysses.dev.local-error_log" CustomLog "/private/var/log/apache2/ulysses.dev.local-access_log" common <Directory "/Users/Ulysses/Sites/"> AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>

In this example, I am using an alternate web root located at ‘/Users/Ulysses/Sites/.’ In addition, I’m also using a made-up domain called ‘ulysses.dev.’ I used .dev because it’s not a real top level domain or TLD. There won’t be a conflict with a real existing domain on the Internet. You can use any domain or TLD that you like.

The final step. Edit your hosts file to add your domain. This step is very important since we are using a made-up domain that doesn’t exist. By editing our hosts file, we are spoofing our system that a host does exist.

$ nano /private/etc/hosts

$ nano /private/etc/hosts

Add the following domain at the end of the line. 127.0.0.1 is the loopback address of your system.

127.0.0.1  ulysses.dev

127.0.0.1 ulysses.dev

Save file and exit.

Restart Apache

$ sudo apachectl restart

$ sudo apachectl restart

From the terminal ping your domain.

$ ping ulysses.dev

$ ping ulysses.dev

Write permissions

The Apache user on the Mac is _www and is part of the staff group. To make the Sites folder writeable to the Apache _www user, perform the following commands from the Terminal.

cd /Users/Ulysses
$ sudo chown -R _www:staff Sites
$ sudo chmod -R 755 Sites

cd /Users/Ulysses $ sudo chown -R _www:staff Sites $ sudo chmod -R 755 Sites

Finally, open up your browser and access your domain. In this example, http://ulysses.dev.

Filed Under: Mac Tagged With: apache, virtual host

Virtual Apache Host

December 16, 2013

Creating a virtual host in Apache allows for multiple domains to be hosted on a single IP address. This is extremely useful in terms of hosting multiple domains on a single host or VPS. As far as I know, you can have an unlimited number of virtual hosts. The only limitation is the system resources in terms of CPU usage, memory and storage. This article will show you how to add Apache virtual hosts in Ubuntu.

Typically, the web root for Apache in Ubuntu is located in /var/www. Instead, we will use a directory under the /home/username directory. So, let’s now create a directory in /home/username/fakedomain.com/. We can achieve this by typing the following:

$ mkdir /home/username/fakedomain.com

$ mkdir /home/username/fakedomain.com

We will give it read and write permissions.

$ chmod 755 -R /home/username/fakedomain.com

$ chmod 755 -R /home/username/fakedomain.com

Now, let’s create a new virtual host file.

$ sudo touch /etc/apache2/sites-available/fakedomain.com

$ sudo touch /etc/apache2/sites-available/fakedomain.com

We will add the following configuration.

<VirtualHost *:80>
ServerAdmin webmaster@localhost
Servername fakedomain.com
ServerAlias www.fakedomain.com
DocumentRoot /home/username/fakedomain.com
</VirtualHost>

<VirtualHost *:80> ServerAdmin webmaster@localhost Servername fakedomain.com ServerAlias www.fakedomain.com DocumentRoot /home/username/fakedomain.com </VirtualHost>

Save the file by pressing Control-O followed by Control-X.

Activate our new host.

$ sudo a2ensite fakedomain.com

$ sudo a2ensite fakedomain.com

Restart Apache.

$ sudo service apache2 restart

$ sudo service apache2 restart

Edit your hosts file. The first line is for Ubuntu, the second for Mac OS.

$ sudo nano /etc/hosts
$ sudo nano /private/etc/hosts

$ sudo nano /etc/hosts $ sudo nano /private/etc/hosts

Add the fakedomain.com to your hosts file. Substitute the IP address of your host.

192.168.0.5  fakedomain.com

192.168.0.5 fakedomain.com

Save the file by pressing Control-O followed by Control-X.

You can now access fakedomain.com from your browser by typing ‘http://fakedomain.com’ on the address bar.

Filed Under: HTML, Linux Tagged With: apache, virtual host

  • Home
  • About
  • Archives

Copyright © 2023