Mac OS X is shipped with a built-in and ready to use Apache 1.3 web server. So we won't have to install it since it's already there... All we'll have to do is to activate it. To do that, open the System Preferences (found in the Apple menu). Then choose Sharing. You'll get a screen like this:
Here you see all the sharing options and protocols which come with OS X. The one we need to activate is «Personal Web Sharing». There you go. You just launched the Apache daemon.
Now, your machine is accessible via the HTTP protocol. To test it, open your favorite browser and type the address http://127.0.0.1/ or http://localhost/. Those addresses are the local addresses of your server, available only from your Macintosh. To know how to access your freshly activated server from the outside, please read the next chapter about Networking.
Apache comes with a program accessible through the terminal. It is used to control and test the Apache environment. The name of that binary is «apachectl», for Apache Control. You'll find below a short description of it's features. Note that you must have root access to use it, so use sudo before the command. The default syntax is:
apachectl function
Function: | Description: |
|---|---|
start | Start the Apache daemon. |
stop | Stop the Apache daemon. |
restart | Restart the Apache daemon. |
graceful | Restart the Apache daemon gracefully. That means that the server will wait for any existing request to end before restarting. |
status | Display a short status of the server activities (require mod_status enabled / see the configuration section). |
fullstatus | Display the complete status of the server activities (require mod_status enabled / see the configuration section). |
configtest | Test the configuration file and report any error. |
Now that Apache is running, we are going to see the configuration file. It's called httpd.conf, and it's found in the /etc/httpd/ directory. That directory, as other system directories, is hidden, so you can't see it from the Finder.
First, in the terminal, go to the Apache configuration directory:
cd /etc/httpd/
We are first going to make a backup copy of the file, for some security reasons:
sudo cp httpd.conf httpd.conf.save
Then open that file using pico. You'll need to use sudo if you want to save your changes, since the file belongs to the root user:
sudo pico httpd.conf
Now we are going to study that file. At line 63, you'll find that:
ServerRoot "/usr"
It means that the core files of Apache are located in the /usr/ directory. Then, at line 190, you'll find that:
# Dynamic Shared Object (DSO) Support
That line is a comment (marked with the # sign). It won't be interpreted by Apache. The DSO part is really important. After the comments, you'll see a serie of instructions:
#LoadModule vhost_alias_module libexec/httpd/mod_vhost_alias.so
#LoadModule env_module libexec/httpd/mod_env.so
LoadModule config_log_module libexec/httpd/mod_log_config.so
[...]
#AddModule mod_vhost_alias.c
#AddModule mod_env.c
AddModule mod_log_config.c
[...]
Notice that some of those lines are marked with a comment sign as well. The LoadModule instruction tells Apache to load an additional module. In other words, it extends the functionalities of Apache. The AddModule instruction is complementary to LoadModule. Only the lines with no comment sign will be read by Apache, so only the corresponding modules will be loaded. To activate an other module, you just have to erase the # sign in both the LoadModule and AddModule lines, corresponding to the module you want to activate.
That's what we are going to do right now. We'll add some more modules, needed to run a CMS like Typo3. Activate the following lines by erasing the # sign:
#LoadModule status_module libexec/httpd/mod_status.so
#LoadModule info_module libexec/httpd/mod_info.so
[...]
#LoadModule rewrite_module libexec/httpd/mod_rewrite.so
[...]
#LoadModule perl_module libexec/httpd/libperl.so
#LoadModule php4_module libexec/httpd/libphp4.so
[...]
#AddModule mod_status.c
#AddModule mod_info.c
[...]
#AddModule mod_rewrite.c
[...]
#AddModule mod_perl.c
#AddModule mod_php4.c
That's it. We've just enabled the use of Perl and PHP in our server, with the modules to get status and informations reports about the server, needed for example when using «apachectl status» command. We also enabled the module used for rewriting URLs. You'll see why on the Typo3 chapter.
Then comes the «Main Server» configuration part. At line 330 and 331, you'll see this:
User www
Group www
www is the user that will be used by the Apache daemon. It will be important for the Typo3 installation chapter. Below, at line 363, you'll find that:
DocumentRoot "/Library/WebServer/Documents"
That's the default location of the documents you'll put on the server. We are going to change this line, to change the location of your future sites. When you update Mac OS X, sometimes it also includes an update for Apache. So to avoid any problem, we won't use the default location. Change the line in question to:
DocumentRoot "/Library/Servers/Server-1/Documents"
We'll need to create the new direcrories. Open a new terminal window and type:
sudo mkdir /Library/Servers /Library/Servers/Server-1 /Library/Servers/Server-1/Documents /Library/Servers/Server-1/CGI-Executables
And now, we'll set the correct permissions for the new directories. Type:
sudo chown -R your_username:www /Library/Servers
sudo chmod -R g+w /Library/Servers
Replace «your_username» by your short username (the one in the prompt). Now back in the configuration file. At line 388:
<Directory "/Library/WebServer/Documents">
We also need to change that line with the new location (/Library/Servers/Server-1/Documents). The <Directory> instruction indicates a folder configuration section. Below, you will see that:
Options Indexes FollowSymLinks MultiViews
That line configures the options of the directory. For example, the «Indexes» option allows Apache to produce a list of the directory's files, when no index.html file is found. To deactivate that option, add a minus sign before:
Options -Indexes FollowSymLinks MultiViews
Then you got:
AllowOverride None
You need to change this line to «AllowOverride All». It enables the use of .htaccess files somewhere in /Library/Servers/Server-1/Documents/. A .htaccess file can contain Apache instructions, to configure the server without having to touch the main configuration file. We'll use such a file in the Typo3 chapter. Just after that, there is:
Order allow,deny
Allow from all
We don't need to change that. Actually it tells Apache that everyone can read the files in the directory. As an example, if you would like to restrict access to your own machine, you'll need to change those lines to:
Order deny,allow
Deny from all
Allow from 127.0.0.1
Refer to the Apache manual for more details about access restrictions. Then, on line 659, we also need to change the reference to the new CGI-Executables directory.
<Directory "/Library/WebServer/CGI-Executables">
Next, on line 864:
#AddHandler cgi-script .cgi
We have previously activated the Perl module, so we need to uncomment that line in order to use CGI programs. That's all for now. You can save the configuration file by pressing [ctr-o], then return, and close it by pressing [ctrl-x]. Now you're back in the shell. We need to restart Apache, for the configuration file to be processed:
sudo apachectl graceful
Now we have a working web server running. To publish your files, just put them in /Library/Servers/Server-1/Documents/. But what if you want one or more other servers?
Apache supports multi-hosting. That means that you can have as many sites as you want on your server, each one having a different document root. There are different ways to do that, but in that case, we will use a method called «name-based» virtual hosts. First, we'll copy the /Livrary/Servers/Server-1/ directory:
sudo cp -prf /Library/Servers/Server-1 /Library/Servers/Server-2
Now, open again the Apache configuration file. The virtual hosts section is found at line 1029. We'll add a few lines after the comments:
NameVirtualHost *
Listen 80
<VirtualHost *:80>
DocumentRoot "/Library/Servers/Server-1/Documents"
</VirtualHost>
We are adding our first server as a virtual host. Notice the «Listen» instruction. That's the port number of the virtual host. 80 is the default port for the HTTP protocol. Now we'll add the second server. It can't be on the same port number as the first one, so we'll put it on another one (9000). So to access that server from your browser, just type http://127.0.0.1:9000/. In the configuration file, add those lines:
Listen 9000
<VirtualHost *:9000>
DocumentRoot "/Library/Servers/Server-2/Documents"
<Directory "/Library/Servers/Server-2/Documents">
Options -Indexes FollowSymLinks MultiViews
AllowOverride None
</Directory>
<IfModule mod_alias.c>
ScriptAlias /cgi-bin/ "/Library/Servers/Server-2/CGI-Executables/"
<Directory "/Library/Servers/Server-2/CGI-Executables">
AllowOverride None
Options None
</Directory>
</IfModule>
</VirtualHost>
That's it. We've added the references and the configuration for the second server's directories. Save and close the file, restart Apache, and we're done. Now you have a second server running, where you can host a web site.To add more virtual hosts, just repeat this step, choosing each time a different port. Don't choose 9001, 9002, 9003, etc... It just won't work. Put a little more space between the ports, something like 9005, 9010, 9015 etc... Also, you shouldn't choose a port under 9000, because a lot of ports are already assigned, and needed by some softwares. You can get a list of all the ports at the IANA website (http://www.iana.org/assignments/port-numbers).
To get additional documentation about the Apache web server, you can get the complete manual on the Apache Software Foundation website (http://httpd.apache.org/docs/).