How to Install and Configure Apache on a VPS? A Complete Guide with Examples

Hey fellow developer! Want to deploy your project on a VPS, but struggling with Apache? I understand, I’ve been there. The whole process can seem like a nightmare, especially if you’re a beginner. But don’t worry, I’m here to guide you through every step of installing and configuring Apache on your VPS. In this detailed guide, we’ll cover all the steps, from installation to virtual host configuration, with real-world examples of commands, configuration files, and, of course, a dash of humor and personal stories from my exciting life as a developer. Ready? Let’s go!

Server Preparation
How to Install and Configure Apache on a VPS? - Server Console

Before you start installing Apache, you need to prepare the ground. First, make sure you have SSH access to the server. This is usually done through a terminal. If you’re using Windows, you’ll need PuTTY or a similar program. Linux users have it easier – the built-in terminal is your friend! Next, let’s update the system. This is crucial! Otherwise, you risk encountering a bunch of compatibility issues. Trust me on this one…

sudo apt update && sudo apt upgrade -y

After updating the system, it’s a good idea to reboot it. This ensures that all changes take effect. Sometimes, some packages can be a bit temperamental…

sudo reboot

Now that the system is updated, we can move on to installing Apache. But before we do that, I’d check a couple more things. For example, the kernel version and available RAM. This will be useful for further optimization, if needed.

VPS Hosting

Virtual servers with guaranteed resources

Choose VPS

uname -a
free -h

If you encounter problems at this stage, don’t panic! Google the error, check the logs (journalctl -xe). Sometimes, trivial things like insufficient disk space can really ruin your mood.

Installing Apache
How to Install and Configure Apache on a VPS? - Apache Installation Screen

Okay, so, it’s time to install Apache itself. It’s pretty simple if you know what you’re doing. For Debian-based systems (Ubuntu, Debian, etc.), use the command:

sudo apt install apache2 -y

The -y flag automatically confirms all actions, which speeds up the installation. If something goes wrong – you’ll see error messages. This is where your Googling skills will come in handy! Sometimes, you may need to install additional packages that Apache depends on. For example, libapache2-mod-php to work with PHP.

After installation, you can check the Apache status:

sudo systemctl status apache2

You should see «active (running)». If not, something went wrong. Check the logs (sudo tail /var/log/apache2/error.log), restart Apache (sudo systemctl restart apache2) or even try reinstalling it.

Sometimes, after installation, you need to configure firewall rules so that Apache is accessible from the outside. This depends on which firewall is used. For UFW (Uncomplicated Firewall) it will look like this:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable

Configuring Apache

The main Apache configuration file is located in /etc/apache2/apache2.conf. But honestly, I usually don’t touch this file directly. It’s better to use virtual hosts. But some general settings can be changed here. For example, you can change ServerName and ServerAdmin.

ServerName your_domain.com
ServerAdmin admin@your_domain.com

Don’t forget to restart Apache after making changes:

sudo systemctl restart apache2

By the way, look at the DocumentRoot directive. This is the path to the root directory of VPS: Fast & Easy" class="internal-post-link">your website. By default, it’s /var/www/html. This is where all your website files are stored.

In this file, you can also configure various Apache modules. For example, the mod_rewrite module allows you to use .htaccess files for URL rewriting. But more on that later, when we configure virtual hosts.

Another important point is log configuration. By default, Apache logs are stored in /var/log/apache2/. If something breaks, first look in error.log. Sometimes, errors are so cryptographically encrypted that only… well, you get it.

Creating Virtual Hosts

Virtual hosts are what allow you to run multiple sites on one server. This is a powerful tool! Let’s create a virtual host for mysite.com. To do this, create a virtual host configuration file:

sudo nano /etc/apache2/sites-available/mysite.com

In this file, we will specify the following configuration:

<VirtualHost *:80>
    ServerName mysite.com
    ServerAlias www.mysite.com
    ServerAdmin admin@mysite.com
    DocumentRoot /var/www/mysite.com
    <Directory /var/www/mysite.com>
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Don’t forget to create the directory /var/www/mysite.com and put your website files there! After that, you need to enable this virtual host:

sudo a2ensite mysite.com

And, of course, restart Apache:

sudo systemctl restart apache2

This is where it gets interesting… Debugging virtual hosts is a story in itself. You might spend half a day, and then it turns out you just forgot to restart Apache… Or a typo in the configuration file… Or incorrect directory permissions… In short, be careful!

Setting Up an SSL Certificate

An SSL certificate is a must-have in our time. Without it, your site will look suspicious, and visitors may simply refuse to visit it. There are many ways to get an SSL certificate. The easiest way is to use Let’s Encrypt. It’s a free and automatic service. To install Let’s Encrypt, you need to install the certbot package:

sudo apt install certbot python3-certbot-apache -y

After installation, you can get a certificate:

sudo certbot --apache -d mysite.com -d www.mysite.com

This command will automatically configure everything you need. But sometimes, manual configuration may be required. In this case, certbot will tell you what to do. Usually, this is making some changes to the virtual host configuration file.

After successfully installing the certificate, you will see a message that everything went well. Check that your site is working over HTTPS. If everything is done correctly, a green lock should appear in the browser’s address bar. Boom! That’s it!

By the way, don’t forget to renew the certificate every 90 days! Certbot can do this automatically if configured correctly.

Verification and Testing

After you’ve configured everything, you need to check that everything is working correctly. First, check if your site is accessible via HTTP and HTTPS. Try opening it in different browsers. Check that all files are displayed correctly. If you are using PHP, check that it is working.

Look in the Apache logs (/var/log/apache2/error.log and /var/log/apache2/access.log) for errors. Sometimes, a small typo in the configuration file can lead to big problems. This is where the ability to read logs comes in handy… Ugh, this part always trips people up.

Check that the SSL certificate is installed and working correctly. You can use online services to check the SSL certificate. There are many such services on the Internet. They will show whether the certificate is valid, its expiration date, etc.

And finally, check the performance of your server. You can use the top or htop command to monitor resource usage. If your server is under heavy load, you may need to optimize your Apache configuration.

Remember, patience is key! Configuring Apache is not a quick process. Don’t get upset if something goes wrong. Try again, look for information on the Internet, and if you still can’t figure it out, ask for help from experienced colleagues. This setup is fire, but it hits different when you’re debugging it.

Official Apache Documentation

Let’s Encrypt

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” — Brian Kernighan

“The best error message is the one that never shows up.” — Anonymous

ServerAdvantagesDisadvantages
ApacheWide support, extensive documentation, stabilityCan be resource-intensive, complex configuration
NginxHigh performance, easy configurationFewer modules, smaller community

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” — Martin Golding

ParameterValue (example)
Apache Version2.4.52
PHP Version8.1
Response Time<100ms