DEV Community

Cover image for Installing Apache2 on Ubuntu Linux Server Using Vagrant
Gbenga Ojo-Samuel
Gbenga Ojo-Samuel

Posted on

Installing Apache2 on Ubuntu Linux Server Using Vagrant

Introduction
Vagrant is a powerful tool for managing virtual machine (VM) environments and automating the setup of development and testing environments. It allows developers to create and configure VMs with ease. Vagrant allows you to work on any project, to install every dependency that project needs, and to set up any networking or synced folders, so you can continue working from the comfort of your own machine.
The Apache HTTP Server is a free and open-source cross-platform web server software, released under the terms of Apache License 2.0. It is developed and maintained by a community of developers under the auspices of the Apache Software Foundation.
In this article, we will walk you through the steps to install Apache2 on an ubuntu/trusty64 Linux server using Vagrant.

Prerequisites
Before you get started, make sure you have the following prerequisites in place:

  1. Vagrant: Download and install Vagrant from the official website (https://www.vagrantup.com/).
  2. VirtualBox (or another supported provider): Vagrant relies on a provider to create and manage VMs. VirtualBox is a popular choice, and you can download it from (https://www.virtualbox.org/).
  3. Basic understanding of Linux: Familiarity with Linux command-line basics will be helpful as you'll be working with a Linux server.

Step1 - Setting Up Your Vagrant Environment
1. Create a Project Directory:
Open your terminal and create a directory for your Vagrant project. Navigate to this directory using the 'cd' command.

mkdir vagrant-apache2
cd vagrant-apache2
Enter fullscreen mode Exit fullscreen mode

2. Initialize Vagrant: Inside your project directory, initialize a Vagrant environment with the specified Linux distribution using the following command:

 vagrant init ubuntu/trusty64
Enter fullscreen mode Exit fullscreen mode

3. Vagrantfile Configuration: The vagrant init ubuntu/trusty64 command generates a Vagrantfile with the required basic configuration for the specified Linux distribution. For ubuntu/trusty64, the vagranfile sample is as shown below.

Vagrant.configure("2") do |config|
config.vm.box = " ubuntu/trusty64"
config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.provider "virtualbox" do |vb|
vb.memory = "1024"
end
end
Enter fullscreen mode Exit fullscreen mode
  1. Start and Provision the VM: Use the following command to start the VM and apply the configuration:
vagrant up
Enter fullscreen mode Exit fullscreen mode

Step2 – Securing the Linux Server
Securing your Linux server by adding a firewall is a critical step to protecting it from unauthorized access and potential threats. In this section, we'll go over how to add a firewall to your Linux server, specifically using the ufw (Uncomplicated Firewall) utility.

1. Install UFW: If UFW is not already installed, you can install it using the package manager. Open a terminal window on your Linux server and run the commands below.

sudo apt update
sudo apt install ufw
Enter fullscreen mode Exit fullscreen mode

By default, UFW denies all incoming and outgoing connections, which is a safe starting point. However, it's important to allow necessary services and ports explicitly.

2. Allow SSH: We will allow SSH traffic since we will be connecting to the server via SSH. Run the following command to enable SSH traffic:

sudo ufw allow OpenSSH
Enter fullscreen mode Exit fullscreen mode

Depending on your server's configuration and the services it provides, you might need to allow the appropriate ports. For example, to allow HTTP and HTTPS traffic.

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Enter fullscreen mode Exit fullscreen mode

3. Review Rules: View the rules added to the firewall with the command:

sudo ufw show added
Enter fullscreen mode Exit fullscreen mode

4. Enable Firewall and Check Firewall Status: Enable UFW to start enforcing the firewall rules and verify that the firewall is in running state. Run the commands below:

sudo ufw enable
sudo ufw status
Enter fullscreen mode Exit fullscreen mode

5. Logging: UFW provides logging for your firewall rules. To enable logging, use:

sudo ufw logging on
Enter fullscreen mode Exit fullscreen mode

You can view the logs in /var/log/ufw.log

Step 3- Installing Apache2 on the Linux Server
Once your VM is up and running, you can proceed with the installation of Apache2.
1. SSH into the VM: Use the

vagrant ssh 
Enter fullscreen mode Exit fullscreen mode

command to SSH into your Vagrant VM:
2. Update Package Lists: It's a good practice to update the package lists to ensure you are installing the latest versions of software packages. Run the

sudo apt update
Enter fullscreen mode Exit fullscreen mode

command to update software packages.
3. Install Apache2: To install Apache2 on the VM, Run the

sudo apt install apache2
Enter fullscreen mode Exit fullscreen mode

command.

4. Start Apache2: After installation, start the Apache2 service with the command

sudo service apache2 start
Enter fullscreen mode Exit fullscreen mode

5. Enable Apache2 at Boot: To ensure Apache2 starts automatically on boot, run:

sudo update-rc.d apache2 enable
Enter fullscreen mode Exit fullscreen mode

6. Verify Apache2 Installation: Open a web browser and type http://localhost:8080/. You should see the default Apache2 page, indicating a successful installation.

Image description

Conclusion
Using Vagrant to set up a Linux server with Apache2 is a convenient way to create a development or testing environment. By automating the process, you can save time and ensure consistency in your development setup. With the steps outlined in this article, you can easily get started with Vagrant and have Apache2 up and running on your Linux server in no time. This approach is especially useful for developers looking to replicate server environments for web development or testing purposes.

Top comments (0)