Last active
November 30, 2025 08:06
-
-
Save honsa/9bd501d235aaeab53d08e9996572a7d0 to your computer and use it in GitHub Desktop.
addvhost.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Check if the script is running as root | |
| if [ "$EUID" -ne 0 ] | |
| then echo "Please run as root: sudo -i" | |
| exit | |
| fi | |
| # Check if domain name is provided | |
| if [ "$1" == '' ] | |
| then echo "Please provide the domain name" | |
| exit | |
| fi | |
| # Define domain name, username and working directory | |
| domain_name=$1 | |
| wd=$PWD | |
| username=${SUDO_USER:-$(whoami)} | |
| web_public=${2:-"public"} | |
| php_version=${3:-"8.4"} | |
| echo "Web public folder: $web_public" | |
| echo "PHP Version: $php_version" | |
| # Create certificate | |
| echo "Create certificate" | |
| cd /etc/ssl/certs/ | |
| mkcert $domain_name | |
| cd $wd | |
| # Add log dir | |
| mkdir /var/log/apache2/$domain_name | |
| # Define document root | |
| echo "Create apache2 config" | |
| doc_root="/var/www/html/$domain_name/$web_public" | |
| # Add doc_root | |
| mkdir -p $doc_root | |
| # Add index.html | |
| index_content="<html> | |
| <head> | |
| <title>Public of $domain_name</title> | |
| </head> | |
| <body> | |
| <h1>Public of $domain_name</h1> | |
| </body></html>" | |
| echo $index_content > $doc_root/index.html | |
| # Create apache config for the domain | |
| echo "<VirtualHost *:80> | |
| ServerName $domain_name | |
| DocumentRoot $doc_root | |
| Redirect permanent / https://$domain_name/ | |
| </VirtualHost> | |
| <VirtualHost *:443> | |
| ServerName $domain_name | |
| DocumentRoot $doc_root | |
| # Enable HTTP/2 | |
| Protocols h2 http/1.1 | |
| SSLEngine on | |
| SSLCertificateFile /etc/ssl/certs/$domain_name.pem | |
| SSLCertificateKeyFile /etc/ssl/certs/$domain_name-key.pem | |
| <Directory $doc_root> | |
| Options Indexes FollowSymLinks | |
| AllowOverride All | |
| Require all granted | |
| </Directory> | |
| <FilesMatch \.php$> | |
| SetHandler \"proxy:unix:/run/php/php$php_version-fpm.sock|fcgi://localhost\" | |
| </FilesMatch> | |
| ErrorLog /var/log/apache2/$domain_name/error.log | |
| CustomLog /var/log/apache2/$domain_name/access.log combined | |
| </VirtualHost>" > /etc/apache2/sites-available/$domain_name.conf | |
| # Update the hosts file | |
| echo "127.0.0.1 $domain_name" >> /etc/hosts | |
| echo "::1 $domain_name" >> /etc/hosts | |
| # Enable the site | |
| a2ensite $domain_name | |
| # Enable SSL module | |
| a2enmod ssl | |
| # Set permissions to user | |
| chown $username:www-data /var/www/html/$domain_name/ -R | |
| chmod 2770 /var/www/html/$domain_name/ -R | |
| # Reload Apache to make these changes take effect | |
| service apache2 reload | |
| echo "You can find and adjust the site configuration in: /etc/apache2/sites-available/$domain_name.conf" | |
| echo "Your document root location: $doc_root" | |
| echo "Virtual host $domain_name set up complete. You can access it over: https://$domain_name" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment