If it responds with "Syntax OK," you're all set.
For Ubuntu, we first need to enable the site, then test the configuration like so:
sudo a2ensite test
This uses the Ubuntu tool a2ensite to enable the site you configured earlier. We also need to disable the default site if we're working without DNS, or if we want our test virtual host to be the only content served:
sudo a2dissite default
If we don't do this, then accessing the server by IP address with a browser will show the default site that was installed with Apache, rather than our test site. If you want to enable the default site at a later time, just run:
sudo a2ensite default
After this, you can run the following to test the configuration in Ubuntu:
sudo apachectl configtest
If all is well, you can now start or restart Apache to load the new configuration. In Fedora and CentOS:
service httpd restart
sudo /etc/init.d/apache2 restart
Once you've restarted the server, you can test the site by opening it with a browser, assuming that you've configured DNS to translate your site's name to the server's IP address. You should see "This is a test" in the browser, as the server delivers the index.html file you created.
Apache on Linux: Beyond the basicsWhat we've laid out above is an extremely simple example of an Apache configuration. While most installations will require only minimal modifications to the default configurations, there are a few details to know beyond the basics.
You may recall that we also installed PHP in the initial steps. Because of this, PHP has already been configured for use with Apache. You can test this by creating a file in the document root called test.php and typing in the following PHP code:
<?php phpinfo(); ?>
Save the file, then open your browser and access http://<server ip address>/test.php. You should get a listing of all the PHP environment variables, modules, and versions. This means that PHP is installed and functional.
An important Apache configuration element to know is the <Directory> statement. This allows you to configure specific permissions on directories shared by Apache. For instance, within our VirtualHost directive example, we might add the following:
<Directory /var/www/test> AllowOverride All Order deny,allow Deny from all Allow from 192.168. </Directory>
This directory statement accomplishes a few goals. First and foremost, AllowOverride All permits the use of .htaccess files within the directory /var/www/test to modify the server's behavior. Through .htaccess files, you can control access to the directory or implement any number of other configuration tweaks without modifying the virtual host configuration file itself. This can be very handy, they allow you to make configuration changes for the virtual host without reloading the server. The downside is that configuration errors within an .htaccess file can cause immediate problems.
(Note that the AllowOverride is subject to several different qualifiers that can grant specific override directives to the .htaccess file. The use of All here allows all of them. You can find a list with descriptions at the Apache Software Foundation.)
Secondly, we have Order deny, allow, and a few Deny and Allow statements. As configured above, the Web server would deny access to any client with an IP address outside the 192.168.0.0/16 range, or 192.168.0.0 through 192.168.255.255. It's a handy way to deny access to specific IP ranges or to ensure that only internal clients can access that particular resource.
Once you're comfortable with the basics, you can move on to other advanced configuration elements. For instance, it's relatively simple to password-protect a site or directory using .htaccess files. To do this, you need to create a file named .htaccess in the document root of the virtual host (or Apache server) and add these lines:
AuthUserFile /etc/httpd/webpasswds AuthName "My Web Auth" AuthType basic <Limit GET POST> </Limit>
Then create the password file. On Fedora or CentOS:
htpasswd -c /etc/httpd/webpasswds myuser
sudo htpasswd -c /etc/apache2/webpasswds myuser
Make sure you have set AllowOverride All, or at least AllowOverride Limit, in your configuration file as noted above. After you've done this and reloaded the server, users will be presented with a login dialog box before they can access the website. You can add more usernames and passwords using the same htpasswd command, though you don't need to use the -c flag if you're just adding more users.
To infinity and beyond With any luck, you've just succeeded in getting Apache up and running on a modern Linux server. There's much more to know about the Apache Web server, and the best way to learn is to experiment with different configurations. You'll want to bookmark Apache's documentation for the version you're running.
It's unwise to put sensitive information on any Web server until you're comfortable that the configuration is proper. It's too easy to make configuration errors that expose information inadvertently. If you're just starting out with Apache and Linux, take the time to research and educate yourself first, and perhaps find someone more knowledgeable to verify your configurations before placing your server into production.
This article, "How to install Apache on Linux," was originally published at InfoWorld.com. Follow the latest developments in applications and Web application development at InfoWorld.com. Get a digest of the key stories each day in the InfoWorld Daily newsletter. For the latest business technology news, follow InfoWorld on Twitter.
Read more about applications in InfoWorld's Applications Channel.
This story, "How to install Apache on Linux" was originally published by InfoWorld.