In the last post I described how to install and use MacPorts this time I will show you how to install and configure Apache web server with virtual host.
Installing Apache web server from MacPorts is as easy as installing any other package. You open the Terminal and issue command:
$ sudo port install apache2
Port will as usual download and install all Apache dependencies. It will take some time to install. How much time? It depends on your computer and Internet connection speed. When the installation is finished we have all the software to run Apache but we still need to do some configuration to make it work. For example if you go and try to start Apache right away you will probably get this:
$ sudo /opt/local/apache2/bin/apachectl start httpd: apr_sockaddr_info_get() failed for DevMac httpd: Could not reliably determine the server's fully qualified domain name, using DevMac for ServerName
This happens for example when you have fresh install of OS X 10.9 – Mavericks. If you don’t get any error messages – great! For those of you that got them here is how you solve them. The error messages are caused because hostname is not in the
/etc/hosts file. To fix it first check your hostname:
$ hostname DevMac
In my case the hostname is DevMac (yours will be different), remember it and open
/hosts/etc file with nano:
$ sudo nano /etc/hosts
Add your hostname to the end of all the lines where you see
localhost. After your edits
/etc/hosts file should look like this:
127.0.0.1 localhost devmac 255.255.255.255 broadcasthost ::1 localhost devmac fe80::1%lo0 localhost devmac
Instead of devmac you will of course have to put your host name.
If you’d like to change your hostname for some reason you can do it with:
$ sudo scutil –-set HostName DevMac
but do it before editing the
/etc/hosts file. Now when you start the Apache you should get only one message but it’s OK for now. We will take care of it later.
$ sudo /opt/local/apache2/bin/apachectl start httpd: Could not reliably determine the server's fully qualified domain name, using DevMac for ServerName
At this point Apache should be running. You can check it with command:
$ netstat -an | less tcp46 0 0 *.80 *.* LISTEN
If you don’t see *.80 anywhere Apache didn’t start. In that case go and read logs in
/opt/local/apache2/logs/error_log. The error messages are pretty informative. If they don’t make sense to you Google them. In our case Apache started OK and is listening on port 80 (I deleted not relevant lines form the netstat command). At this point you can open your web browser and go to
http://devmac. Both should work and you should see something like this:
The HTML code for the example site you see in your browser is located in
/opt/local/apache/htdocs directory and the file displayed is named
index.html. You can go there and edit it with
nano. Just make sure you use
sudo to edit it. After saving the file refresh the browser and you should see your changes. Cool :)
Creating virtual hosts
Now lets create some example virtual host. Go to your home directory and create folders as shown below:
$ cd ~ $ mkdir -p ws/site1
For the sake of this tutorial we will put there simple
index.html file to later check if the Apache virtual host configuration works properly. In
site1 directory create
index.html file with following content:
<html><body><h1>Hello World! Site1</h1></body></html>
OK we have our simple site to test virtual host configuration. Next step is to configure Apache virtual hosts.
Apache has many configuration files but the main one is
/opt/local/apache/conf/httpd.conf. This is where we will do our modifications first. Open it with
sudo nano /opt/local/apache/conf/httpd.conf. A lot of configuration options in it are self explanatory, like
Group the Apache server runs at, the port the server listens at (
Listen) and so on. Right now we are interested in one of the lines. Find line:
# Virtual hosts #Include conf/extra/httpd-vhosts.conf
it should be near the end of the file, and change it to:
# Virtual hosts Include conf/extra/httpd-vhosts.conf
# form the beginning of it. All lines starting with # are ignored when the configuration file is read. Removing it we asked Apache to include
conf/extra/httpd-vhosts.conf file with additional configurations during start. Save the file and exit. The path after
Include configuration statement is relative to where Apache was installed (in our case
Virtual hosts configuration
Virtual host configuration file
/opt/local/apache2/conf/extra/httpd-vhosts.conf is where we will configure our site1 virtual host. Open it with
$ sudo nano /opt/local/apache2/conf/extra/httpd-vhosts.conf
at the end of it you will find two example configurations for virtual hosts:
<VirtualHost *:80> ServerAdmin firstname.lastname@example.org DocumentRoot "/opt/local/apache2/docs/dummy-host.example.com" ServerName dummy-host.example.com ServerAlias www.dummy-host.example.com ErrorLog "logs/dummy-host.example.com-error_log" CustomLog "logs/dummy-host.example.com-access_log" common </VirtualHost> <VirtualHost *:80> ServerAdmin email@example.com DocumentRoot "/opt/local/apache2/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "logs/dummy-host2.example.com-error_log" CustomLog "logs/dummy-host2.example.com-access_log" common </VirtualHost>
Remove or comment then out and paste below configuration in.
Save the file and exit. We named our virtual host site1.dev so we have to make sure it’s in our
/etc/hosts file. Open it like you did before with
nono and add
site1.dev right after your hostname. When you are done it should look like this:
127.0.0.1 localhost devmac site1.dev 255.255.255.255 broadcasthost ::1 localhost devmac site1.dev fe80::1%lo0 localhost devmac site1.dev
Save it, exit and restart Apache.
$ sudo /opt/local/apache2/bin/apachectl restart
Now you should be able to browse to site1 in your favorite bowser by going to http://site1.dev
Taking care of startup error
httpd: Could not reliably determine the server's fully qualified domain name, using DevMac for ServerName
is displayed by Apache during each start, stop and restart due to lack of our hostname in the Apache main configuration file. To fix this open
$ sudo nano /opt/local/apache/conf/httpd.conf
and change it to:
If you don’t find it, add it to the end of the file.
macdev with your hostname of course. Now you shouldn’t get any errors during Apache start, stop and restart.
That’s it! Now you know how to do basic basic Apache web server configuration. If you like the post / video tutorial make sure to give me thumbs up on YouTube!