Before installing Apache2, ensure that nginx is not present on the server.

To install Apache2 on a VDS, use the following command:

apt-get install apache2



Apache2 Configuration:

To configure Apache2, navigate to /etc/apache2/sites-available and create a file following the site.conf template:

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


Enable the site; replace site.conf with the name of the configuration file you created:

a2ensite site.conf


If you need to disable the site, replace site.conf with the configuration file's name:

a2dissite site.conf


Reload Apache2:

service apache2 reload


Connecting PHP to Apache2:

To enable Apache to handle PHP files correctly, install the package:

apt-get install libapache2-mod-php -y


Reload Apache2:

service apache2 reload


Enabling Rewrite:

Without this option, 70% of CMS for websites will not work. Most websites contain an .htaccess file, and to make it work, you need to enable:

a2enmod rewrite


Reload Apache2:

service apache2 reload


Enabling SSL.

SSL connection is not mandatory and is implemented only if available and desired. First, enable the module responsible for SSL:

a2enmod ssl


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

<VirtualHost *:443>
       ServerName spacecore.pro # Specify the website's domain
       ServerAdmin admin@spacecore.pro # Your email
       DocumentRoot /var/www/html # Path to the website's 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:

service apache2 reload


Checking for Nginx:

If Apache2 is installed alongside Nginx, they may conflict due to port 80. Therefore, you need to remove one of the web servers. Check if Nginx is installed:

service nginx status


If you do not see a large message with information, it means Nginx is not installed. If Nginx is installed, remove it:

apt-get remove --purge nginx* -y


Removing Apache2:

To remove Apache2, use the following command:

apt-get remove --purge apache2* -y