Installing Apache2

2 min read
On this page Installing Apache2Installing Apache2:Configuring Apache2:Connecting PHP to Apache2:Enabling Rewrite:Enabling SSL:Checking for Nginx:Removing Apache2

Installing Apache2

Apache is cross-platform software that supports operating systems such as Linux, BSD, Mac OS, Microsoft Windows, Novell NetWare, and BeOS.

Before installing Apache2, make sure that nginx is not present on the server!

Installing Apache2:

To install Apache2 on a VDS, you need to execute:

apt-get install apache2

Configuring Apache2:

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

<VirtualHost *:80>
ServerName site.com # Specify the site's domain
ServerAdmin admin@site.com # Your email
DocumentRoot /var/www/html # Path to the website folder

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the site, where site.conf should be the name of the config you created:

a2ensite site.conf

If you need to disable a site, replace site.conf with the config name:

a2dissite site.conf

Reload Apache2:

service apache2 reload

Connecting PHP to Apache2:

To ensure Apache can correctly display PHP files, you need to 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 won't work. Most websites contain a .htaccess file, and to make it work, you need to enable rewrite:

a2enmod rewrite

Reload Apache2:

service apache2 reload

Enabling SSL:

Enabling SSL is optional and is done if you have an SSL certificate and want to use it. You need to enable the module responsible for SSL:

a2enmod ssl

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

<VirtualHost *:443>
ServerName site.com # Specify the site's domain
ServerAdmin admin@site.com # Your email
DocumentRoot /var/www/html # Path to the website folder

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 you install Apache2 alongside Nginx, they will conflict due to port 80. So, you need to remove one of the web servers. Check if Nginx is installed:

service nginx status

If you don't see a large message with information, it means Nginx is not installed.

If Nginx is installed, you need to remove it:

apt-get remove --purge nginx* -y

Removing Apache2

To remove Apache2, use:

apt-get remove --purge apache2* -y
Didn’t find the answer?Contact support — we’ll help you sort it out.
Open a ticket →