If you want to configure new domain at your Ubuntu server, here are the steps you neet to follow.
Create new Virtual Host configuration
First create a configuration file:
sudo nano /etc/apache2/sites-available/yourdomain.conf
Then put following configuration to the configuration file:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/html/yourdomain/public
<Directory /var/www/html/yourdomain/public>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Once it’s done, let’s enable new configuration:
sudo a2ensite yourdomain.conf && sudo systemctl reload apache2
Protect it with Let’s Encrypt free certificate
If you have not installed it before, you have to install it with following command:
sudo apt install python3-certbot-apache
Now we can generate a certificate for our domain:
sudo certbot --apache --agree-tos --redirect -m [email protected] -d yourdomain.com -d www.yourdomain.com
That’s it. Command above will do all the stuff. It will generate certificate, install it in your apache, modify apache configuration so all the traffic will be redirected to secure connection. Nothing to do more.
The only downside to the solution is the certificate vilidity period – only 90 days. So around certificate expiration time you have to renew it with following command:
sudo certbot renew --dry-run
And that’s it. Enjoy secured website 🙂
There should be sudo a2ensite yourdomain.conf not sudo a2ensite domainname.conf