To host a website on Ubuntu with Apache2, you can follow these steps:
- Install Apache2:
sudo apt-get update
sudo apt-get install apache2
- Configure Firewall to Allow Web Traffic:
sudo ufw app list
sudo ufw allow 'Apache'
- Create a new virtual host configuration file:
sudo nano /etc/apache2/sites-available/example.com.conf
Replace “example.com” with your domain name or IP address.
- Add the following configuration to the virtual host file:
<VirtualHost *:80>
ServerAdmin webmaster@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/html/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Replace “example.com” with your domain name or IP address.
- Create the document root directory:
sudo mkdir -p /var/www/html/example.com/public_html
- Set ownership of the document root directory:
sudo chown -R $USER:$USER /var/www/html/example.com/public_html
sudo chmod -R 755 /var/www/html
- Create an index.html file in the public_html directory:
enano /var/www/html/example.com/public_html/index.html
- Add some content to the index.html file, save and exit.
- Enable the virtual host:
sudo a2ensite example.com.conf
- Restart Apache2:
sudo systemctl restart apache2
That’s it! You should now be able to access your website by going to your domain name or IP address in a web browser.