Installing and Configuring Apache2 on Linux

9 min readUpdated December 6, 2025

Apache is cross-platform software and supports the Linux, BSD, macOS, Microsoft Windows, Novell NetWare and BeOS operating systems.

Before installing Apache2, make sure that Nginx is not present on the server.

#Installing Apache2

To install Apache2 on a VDS, run:

bash
apt-get install apache2

#Configuring Apache2

To configure Apache2, go to /etc/apache2/sites-available and create a file similar to site.conf:

apache
<VirtualHost *:80> ServerName spacecore.pro # Specify the site domain ServerAdmin admin@spacecore.pro # Your email address DocumentRoot /var/www/html # Path to the site directory ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

Enable the site; instead of site.conf, use the name of the configuration file you created:

bash
a2ensite site.conf

If you need to disable the site, use the configuration file name instead of site.conf:

bash
a2dissite site.conf

Reload apache2:

bash
service apache2 reload

#Connecting PHP to Apache2

For Apache to correctly serve PHP files, you need to install the following package:

bash
apt-get install libapache2-mod-php -y

Reload apache2:

bash
service apache2 reload

#Enabling rewrite

Without this option, about 70% of website CMSs will not work.

Most sites include an .htaccess file; to make it work, run:

bash
a2enmod rewrite

Reload apache2:

bash
service apache2 reload

#Enabling SSL

Enabling SSL is optional and can be done if you have a certificate and want to use HTTPS.

You need to enable the module that is responsible for SSL:

bash
a2enmod ssl

Create another configuration file in /etc/apache2/sites-available that will handle SSL, for example site-ssl.conf:

apache
<VirtualHost *:443> ServerName spacecore.pro # Specify the site domain ServerAdmin admin@spacecore.pro # Your email address DocumentRoot /var/www/html # Path to the site directory SSLEngine on SSLCertificateFile /path/to/your_domain_name.pem # Path to the public certificate SSLCertificateKeyFile /path/to/your_private.key # Path to the private key ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

Reload apache2:

bash
service apache2 reload

#Checking for Nginx

If apache2 is installed together with Nginx, they will conflict over port 80. Therefore, one of the web servers must be removed. Check whether Nginx is installed:

bash
service nginx status

If you do not see a long status message, it means Nginx is not installed.

If Nginx is installed, you need to remove it:

bash
apt-get remove --purge nginx* -y

#Removing Apache2

To remove Apache2, use:

bash
apt-get remove --purge apache2* -y

Did not find the answer? We are online 24/7.Contact support