Configure Apache Web Server

How to configure the apache web server and create several sites for http and https

Make changes to /etc/apache2/ports.conf and add ports and server name:

Listen 80
Listen 443
NameVirtualHost *:80
ServerName myserver.com

Add sites to /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/:


    ServerName myserver.com
    ServerAlias www.myserver.com
    DocumentRoot /var/www/myserver.com/
    
        Options None
        Order deny,allow
        Allow from all
    

Or let’s redirect all traffic to https:


    ServerName myserver.com
    ServerAlias www.myserver.com
    Redirect permanent / https://myserver.com/

Use this configuration for https:



        ServerName myserver.com
        ServerAlias www.myserver.com
        ServerAdmin webmaster@myserver.com

        DocumentRoot /var/www/myserver.com/
        
           Options None
           Order deny,allow
           Allow from all

           AuthType Basic
           AuthName "Restricted"
           AuthUserFile /etc/apache2/.htpasswd
           Require expr %{REQUEST_URI} =~ m#^/.well-known/acme-challenge/#
           Require expr %{REQUEST_URI} =~ m#^/apple-touch-icon.png#
           Require valid-user
        

        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_URI} !^/\.well\-known/acme\-challenge/
        RewriteRule ^ index.php [QSA,L]

        SSLEngine on
        SSLCertificateFile    /etc/letsencrypt/live/myserver.com/cert.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/myserver.com/privkey.pem
        SSLCertificateChainFile /etc/letsencrypt/live/myserver.com/fullchain.pem
        Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains"


Use this to remove www from the URL:

        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^www\.myserver\.comg$
        RewriteRule ^/(.*)$ https://myserver.com/$1 [R=301,L]

If you want to setup a reverse proxy, make sure to enable the modules proxy and proxy_http. After that, use this configuration:


       ServerName myserver.com
       ServerAlias www.myserver.com
       ProxyPass / http://serverttoforward.com/
       ProxyPassReverse / http://servertoforward.com/

This is a configuration for WordPress:


        ServerName myserver.com
        ServerAlias www.myserver.com
        ServerAdmin webmaster@myserver.com
        DocumentRoot /usr/share/wordpress
        Alias /wp/wp-content /var/lib/wordpress/wp-content
        Alias /wp /usr/share/wordpress
        
            Options FollowSymLinks
            AllowOverride Limit Options FileInfo
            DirectoryIndex index.php
            Order allow,deny
            Allow from all
            RewriteEngine On
            RewriteBase /
            RewriteRule ^index\.php$ - [L]
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule . /index.php [L]
        
        
            Options FollowSymLinks
            Order allow,deny
            Allow from all