Configuring Apache

Configuring Apache is done by editing its configuration file, which will be located in one of two places, depending on your distribution.

Use the following command on CentOS:

/etc/httpd/conf/httpd.conf

Use the following command on Debian:

/etc/apache/apache2.conf

The default web document directory, /var/www/html, can be changed. While /var/www/html is fairly standard, there's nothing stopping you from changing it should you decide to store your web files elsewhere. If you peruse the configuration file in CentOS, you'll see this directory called out within a configuration block that begins on line 131. If you take a look at the configuration file in Debian, you won't see this called out at all. Instead, you'll see a directory within /etc/apache2 called sites-available. Within the directory, there will be two default files, 000-default.conf and default-ssl.conf. Both of these files designate /var/www/html as the default path, but how they differ is that the 000-default.conf file designates configuration for port 80, while default-ssl.conf is responsible for the configuration on port 443. As you probably know, port 80 references standard HTTP traffic, while port 443 corresponds to secure traffic. So essentially, each type of traffic has its own configuration file on Debian systems.

In all those cases, the document root is being set to /var/www/html. If you'd like to change that to a different directory, you would change the code to point to the new directory. For example, if you wanted to change the path to something like /srv/html, there are a few changes you would need to make to the file.

First, look for the following line:

DocumentRoot /var/www/html

Change it to point to the new directory:

DocumentRoot /srv/html

On my test systems, I found the DocumentRoot callout in the following configuration file on Debian:

/etc/apache2/sites-available/000-default

On CentOS, I found that on line 119 in the default configuration file:

/etc/httpd/conf/httpd.conf

After you change that, we have to set our options for the new directory. On Debian, we need to make these changes in the following file:

/etc/apache2/apache2.conf

On CentOS, we need to make these changes in the following file:

/etc/httpd/conf/httpd.conf

Open up one of those files, depending on which distribution you're using. The code we need to change looks like this:

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

Change the following accordingly:

<Directory "/srv/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

Note

There may be some comments intermixed with the code shown in the previous example, but the basic idea is the same. Find the line that starts with <Directory "/var/www/html"> and ensure the uncommented code within that block matches the example. As long as you do that, you should be fine.

Finally, it probably goes without saying, but to save you a headache you should make sure that you have set the permissions to /srv/html such that the directory and contents are readable by everyone. Also, ensure you created or copied an example HTML file (such as index.html) into this directory. Once you restart Apache, you should be able to serve web content from this new directory.

In addition to setting up the document root, the Apache configuration file also allows you to configure a few very important security settings as well. For example, access to the entirety of the server's file system is disabled by default. This is a good thing. The following code is an example taken from a CentOS system, and it is responsible for preventing filesystem-wide access. The code is as follows:

<Directory />
    AllowOverride none
    Require all denied
</Directory>

Remotely viewing the .htaccess files are also disabled by default with the following configuration block:

<Files ".ht*">
    Require all denied
</Files>

Other options, such as the default location of Apache's log files, can also be set. By default, the following default line of configuration directs the log files to /etc/httpd/logs:

ErrorLog "logs/error_log"

However, this may be misleading, as the /etc/httpd/logs directory on CentOS systems is actually a symbolic link to /var/log/httpd, which is where you would actually find the log files should you need to view them. By default, the logging is set to warn, and this can also be changed within the Apache configuration file and set to any one of debug, info, notice, warn, error, and crit.

It's important to note that for any change you make to Apache, you will need to reload or restart the daemon. If you restart the daemon, it will shut down Apache and start it back up again. Reload simply causes Apache to reread its configuration file. In most cases, reload is the better option. By doing this, you can apply new configuration without disrupting access to your website. As with most systemd units, Apache uses the following commands to manage the running state of the daemon:

  1. Start the Apache daemon with the following command:
    # systemctl start apache2
    
  2. Stop the Apache daemon with the following command:
    # systemctl stop apache2
    
  3. Enable the Apache daemon at boot time with the following command:
    # systemctl enable apache2
    
  4. Reload the Apache daemon while attempting to maintain its running state:
    # systemctl reload apache2
    
  5. Restart the Apache daemon with the following command:
    # systemctl restart apache2
    

If you're using CentOS, replace apache2 with httpd in each case. Now that you understand how Apache is installed and configured, we can move on to using modules.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset