$ sudo apt install apache2
$ sudo systemctl status apache2.service
$ sudo ufw allow http
$ http://your_server_ip
Use hostname -I to get your sever ip
$ sudo systemctl start|stop|restart|reload apache2.service
$ sudo systemctl enable apache2.service
Enable Apach to start automatically when server boots.
$ sudo mkdir /var/www/your_domain
$ sudo vim /var/www/your_domain/index.html
<html>
<head>
<title>your_domain</title>
</head>
<body>
<h1>Welcome to your_domain</h1 >
</body>
</html>
$ sudo vim /etc/apache2/sites-available/your_domain.conf
<VirtualHost *:80>
ServerAdmin webadmin@localhost
ServerName your_domain
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
To force redirect to HTTPS then use below instead.
<VirtualHost *:80>
<IfModule mod_rewrite.c>
# Force https secure connection
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
ServerAdmin webadmin@localhost
ServerName your_domain
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
</VirtualHost>
<VirtualHost *:443>
ServerAdmin webadmin@localhost
ServerName your_domain
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
$ sudo apache2ctl configtest
Example Output
Syntax OK
$ sudo a2ensite your_domain.conf
Enable website
$ sudo systemctl restart apache2.service
$ http://your_domain
Check if your website opens.
Other related commands.
$ sudo a2dissite your_domain.conf
Disable website.
$ sudo systemctl disable apache2.service
Disable Apache to start automatically when server boots.