Skip to content

Apache2

Filip Krzyżanowski edited this page Nov 12, 2022 · 1 revision

Virtual Hosts

You need to rename all domain.com with yours domain name

1. Create a domain directory

The -p flag create a /websites folder if you don't have it

sudo mkdir -p /websites/domain.com

2. Create a configuration file for a domain

sudo nano /etc/apache2/sites-available/domain.com.conf

3. Configure Virtual Host for your website

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName domain.com
    ServerAlias www.domain.com
    
    DocumentRoot /websites/domain.com
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    <Directory /websites/domain.com>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
     
    # SSL certificate, from HTTP to HTTPS
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =domain.com [OR]
    RewriteCond %{SERVER_NAME} =www.domain.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

4. Restart Apache2 server

sudo systemctl restart apache2

5. Enable site using a2ensite

sudo a2ensite domain.com.conf

6. Enable rewrite module in a2ensite

sudo a2enmod rewrite

7. Restart Apache2

sudo systemctl reload apache2

8. Setting up default Virtual Host to works correct

If you want host something on default host (000-deafult.conf) - for example: RoundCube - you need to set <Directory> tag in this Virtual Host like this:

sudo nano /etc/apache2/sites-available/000-default.conf

And under CustomLog ${APACHE_LOG_DIR}/access.log combined line you must put this lines

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