Apache Configs

Mar 25, 2018 18:50 · 209 words · 1 minute read Apache

General Config file

<VirtualHost *:80>
    # Basic settings
    ServerAdmin webmaster@test.com
    ServerName www.test.com
    ServerAlias test.com
    DocumentRoot /var/www

    # Setting custom logs
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # Example on how to allow clean urls for drupal
    <Directory /var/www>
        RewriteEngine on
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} !=/favicon.ico
        RewriteRule ^ index.php [L]
    </Directory>

    # Set a 404 Page
    ErrorDocument 404 /404.html

    # Setting 301 Re-directs
    Redirect 301 /projects http://test.com

    # Setting 410 Permanently Removed
    Redirect gone /filename.ext

    #if the host doesn't contain 'example.com' then redirect to example.
    RewriteCond %{HTTP_HOST} !example.com$ [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

    # Set up htpasswd on directory
    <Directory /var/www/mysite/_site>
        AuthType Basic
        AuthName 'Dev Site'
        AuthUserFile '/var/www/users'
        Require valid-user
    </Directory>

    # Setting up a Symfony Project on apache
    <Directory /var/www/symfony/web>
        AllowOverride All
        Order Allow,Deny
        Allow from All
    </Directory>


</VirtualHost>

SSL Example

<IfModule mod_ssl.c>
<VirtualHost *:443>

        ServerAdmin webmaster@test.com
        ServerName www.test.com
        ServerAlias test.com
        DocumentRoot /var/www

        ErrorLog ${APACHE_LOG_DIR}/error-ssl.log
        CustomLog ${APACHE_LOG_DIR}/access-ssl.log combined

        #   SSL Engine Switch:
        #   Enable/Disable SSL for this virtual host.
        SSLEngine on


        # SSl Files
        SSLCertificateFile /etc/apache2/ssl/czuar/czuar.crt
        SSLCertificateKeyFile /etc/apache2/ssl/czuar/czuar.key

</VirtualHost>
</IfModule>

Drupal example

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        <Directory /var/www/html>
                RewriteEngine on
                RewriteBase /
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteCond %{REQUEST_URI} !=/favicon.ico
                RewriteRule ^ index.php [L]
            </Directory>
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>