How to add new domains on ubuntu apache server

Posted on May 14, 2020
To add new domains in your ubuntu server but not in the public folder (/var/www/html), you can follow these steps:
First, create the folder for the new website, and give the ownership to user www-data that will run the webpages:
sudo mkdir /var/www/mydomain.com

sudo chown -R www-data:
www-data /var/www/mydomain.com

Then add these lines in the file /etc/apache2/sites-available/000-default.conf or in anew file(mysite.com.conf)
<VirtualHost mysite.com:80>
ServerName
mysite.com
ServerAlias *.mysite.com # redirect all subdomains
DocumentRoot "/var/www/mysite.com"

<Directory /var/www/mysite.com/>
Require all granted
AllowOverride All
</Directory>

</VirtualHost>
Enable the new file
sudo a2ensite mysite.com.conf

For the change to take effect, restart the server
sudo service apache2 restart

For more information, or if you want to add the new virtual hosts with different files, checkout the links:

https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-14-04-lts


https://httpd.apache.org/docs/current/vhosts/name-based.html