Table of Contents
Why Choose Apache Web Server?
Apache is a powerful and popular open-source web server. It’s a great choice if you want to host your own website on a Linux VPS, especially if you have a PHP or WordPress based site.
Prerequisites –
- A powerful Linux VPS
- Ability to connect to your server using SSH with administrative (sudo) access
- Basic command line knowledge
Step 1: A Fresh Start
Before we install anything, let’s update your server to make sure it has the latest software packages:
sudo apt update
sudo apt upgrade
Step 2: Install Apache
Install the Apache web server with this command:
sudo apt install apache2
Step 3: Apache’s File System
Apache keeps important configuration files in the /etc/apache2
directory. You’ll find settings here for global setup, modules, and virtual hosts.
- apache2.conf – global configuration file for apache webserveer
- conf-available – stores custom configuration snippets, not automatically used by apache2
- conf-enabled – contains sym links to the configs which we want to enable
- envars – stores env variables used by apache2 at startup
- magic – used to determine MIME types of files based on the content
- mod-variable – stores module configuration like rewrite.conf, php.conf
- mod-enabled – contains sym links for the mods we want to enable
- ports-conf – specifies port on which apache2 listens to.
- site-available – stores available virtual hosts
- site-enabled – contains sym links for the vhosts we want to enable
Step 4: Setting Up Virtual Hosts
Virtual hosts let you run multiple websites on one server. Here’s how it works:
- Requests: Your browser says which website it wants when it connects.
- Matching: Apache matches this to the “ServerName” in your virtual host files.
- Serving: Uses the settings for that website.
Step 5: Create Your Virtual Host
Let’s say your website is example.com. Create a file named /etc/apache2/sites-available/example.com.conf
. Paste in this basic config (replace ‘example.com’ with your real domain):
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
ServerAdmin [email protected]
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/app
<Directory /var/www/app>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com_error.log
CustomLog ${APACHE_LOG_DIR}/example.com_access.log combined
SSLEngine on
SSLCertificateFile /path/to/your/server.crt
SSLCertificateKeyFile /path/to/your/server.key
</VirtualHost>
- DocumentRoot: Points to the /var/www/app directory where your application resides.
- Directory: Necessary permissions for Apache to serve your PHP files.
- ErrorLog and CustomLog: Specifies where error and access logs for this website should be stored.
- SSLEngine on: Enables SSL/TLS
- SSLCertificateFile and SSLCertificateKeyFile: Specify the paths to your SSL certificate and private key files.’
Step 6: Enable Your Website
Before we starting visiting the website we need to enable the vhost we created using the below command
sudo a2ensite example.com.conf
Step 7: Restart Apache
To load the new configuration files:
sudo systemctl restart apache2
That’s it! You should now be able to visit your website in a web browser served by apache web server.