Streamline your Apache2 administration

Yes, scripting virtualhost creation would save even more time, but doing a find and replace on the 10-15 lines at the bottom and then paste into ssh means that I can a new virtual host set up in under a minute and its so infrequent these days that I've not invested the time in scripting it.

Create yourself templates for your standard VirtualHost configurations, (I've left in the options ready to enable cgi-bin just in case I ever feel nostalgic enough to want to back and write some Perl) eg

/etc/apache2/sites-available/template.apache2http
<VirtualHost *:80>        ServerAdmin webadmin@myemaildomain.ext
        ServerName [placeholder]
 #       ServerAlias [placeholder]
        DocumentRoot /var/www/[placeholder]/
        <Directory />                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/[placeholder]/>
                Require all granted
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
#               ErrorDocument 404 /
        </Directory>
#        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
#        <Directory "/usr/lib/cgi-bin">
#                AllowOverride None
#                Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
#                Order allow,deny
#                Allow from all
#        </Directory>

        ErrorLog /var/log/apache2/[placeholder]/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn
        CustomLog /var/log/apache2/[placeholder]/access.log combined
        ServerSignature On
#       RewriteEngine on
##      RewriteCond %{SERVER_NAME} =[placeholder] [OR]
#       RewriteCond %{SERVER_NAME} =[placeholder]
#       RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
/etc/apache2/sites-available/template.apache2proxy
<VirtualHost *:80>
ServerAdmin webadmin@myemaildomain.ext
ServerName [placeholder]
#ServerAlias [placeholder]
        ProxyPreserveHost On
        ProxyPass / http://10.15.11.1/
        ProxyPassReverse / http://10.15.11.1/
</VirtualHost>
/etc/apache2/sites-available/template.apache2SSL
<IfModule mod_ssl.c>
#NameVirtualHost *
<VirtualHost *:443>

        ServerName [placeholder]
#        ServerAlias [placeholder]
        ServerAdmin webadmin@myemaildomain.ext

        DocumentRoot /var/www/[placeholder]/
        <Directory />
                Require all granted
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/[placeholder]/>
                Require all granted
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
#               ErrorDocument 404 /

        </Directory>

#        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
#        <Directory "/usr/lib/cgi-bin">
#                AllowOverride None
#                Options ExecCGI -MultiViews +SymLinksIfOwnerMatch
#                Order allow,deny
#                Allow from all
#        </Directory>

        ErrorLog /var/log/apache2/[placeholder]/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /var/log/apache2/[placeholder]/access.log combined
        ServerSignature On

SSLCertificateFile /etc/letsencrypt/live/[SSLplaceholder]/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/[SSLplaceholder]/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
Header always set Strict-Transport-Security "max-age=31536000"
Header always set Content-Security-Policy upgrade-insecure-requests
</VirtualHost>
</IfModule>
/etc/apache2/sites-available/template.Net
<VirtualHost *:80>
        ServerAdmin webadmin@myemaildomain.ext
        ServerName [placeholder]
##      ServerAlias [placeholder]

        ProxyPreserveHost On
        ProxyPass / http://127.0.0.1:[.NETportplaceholder]/
        ProxyPassReverse / http://127.0.0.1:[.NETportplaceholder]/

        ErrorLog /var/log/apache2/[placeholder]/error.log
        CustomLog /var/log/apache2/[placeholder]/access.log combined

#       RewriteEngine on
##      RewriteCond %{SERVER_NAME} =[placeholder] [OR]
#       RewriteCond %{SERVER_NAME} =[placeholder]
#       RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
/etc/apache2/sites-available/template.NetSSL
<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerAdmin webadmin@myemaildomain.ext
        ServerName [placeholder]
        #ServerAlias [placeholder]

        ProxyPreserveHost On
        ProxyPass / http://127.0.0.1:[.NETportplaceholder]/
        ProxyPassReverse / http://127.0.0.1:[.NETportplaceholder]/

        ErrorLog /var/log/apache2/[placeholder]/error.log
        CustomLog /var/log/apache2/[placeholder]/access.log combined

        Include /etc/letsencrypt/options-ssl-apache.conf
        SSLCertificateFile /etc/letsencrypt/live/[SSLplaceholder]/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/[SSLplaceholder]/privkey.pem
</VirtualHost>
</IfModule>

If you are doing .Net core with Kestrel, why not

/etc/systemd/system/kestrel-template
[Service]
WorkingDirectory=/var/www/[placeholder]
ExecStart=/usr/bin/dotnet /var/www/[placeholder]/[placeholder].dll
Restart=always
RestartSec=10
SyslogIdentifier=[placeholder]
User=www-data
# Environment settings define which appsettings.[environemnt].json file is used and are
# case sensitive and filename must match case of ASPNETCORE_ENVIRONMENT variable here.
Environment=ASPNETCORE_ENVIRONMENT=Production
#Environment=ASPNETCORE_ENVIRONMENT=Staging
#Environment=ASPNETCORE_ENVIRONMENT=Development
[Install]
WantedBy=multi-user.target

Then you can simply

sudo cp /etc/apache2/sites-available/template.Net /etc/apache2/sites-available/www.domain.ext.conf
sudo sed -i -e 's/\[placeholder\]/www.domain.ext/g' /etc/apache2/sites-available/www.domain.ext.conf
sudo sed -i -e 's/\[.NETportplaceholder\]/5000/g' /etc/apache2/sites-available/www.domain.ext.conf
sudo mkdir /var/www/www.domain.ext
sudo chown www-data:www-data /var/www/www.domain.ext
sudo mkdir /var/log/apache2/www.domain.ext
sudo chown root:adm /var/log/apache2/www.domain.ext
sudo a2ensite www.domain.ext
sudo systemctl reload apache2

Populate your content and Configure your Kestrel for .Net

sudo cp /etc/systemd/system/kestrel-template /etc/systemd/system/kestrel-www.domain.ext.service
sudo sed -i -e 's/\[placeholder\]/www.domain.ext/g' /etc/systemd/system/kestrel-www.domain.ext.service
sudo systemctl enable kestrel-www.domain.ext.service
sudo systemctl start kestrel-www.domain.ext.service

Done.


Quick apache2 admin Last updated 09/03/2020 10:18:05