Skip to the content
Cloudfanatic Knowledge baseCloudfanatic Knowledge base
  • Home
  • Log In
  • System Status
  • Home
  • Log In
  • System Status

How can we help?

e.g. ssh keys, set up mysql, ubuntu 22 mysql php nginx, account settings

Account Functions

  • Your Dashboard
  • Accessing your server for the first time
  • Controlling you instance
  • Cloudfanatic Operating systems and Rebuilding your instance
  • DNS Manager
  • Custom ISO images

Sales & Billing

  • Cloudfanatic Crypto Payments
  • Frequently Asked Questions (FAQ)
  • Server billing
  • Automatic billing for your account
  • Server Upgrades
  • Cloudfanatic Affiliate Program
  • Automated Server backups

News & Announcements

  • RockyLinux 9 now generally available for all instances
  • AlmaLinux 9 now generally available for all instances
  • Servercheap is now Cloudfanatic and more news
  • Ubuntu 22 now generally available for all instances
  • Cloudfanatic/Servercheap has been featured on HostAdvice
  • New 1-Click App – CyberPanel
  • New location: North Carolina
  • Cloudfanatic/Servercheap has been featured on Hostingadvice.com
  • Centos Stream now generally available for all instances
  • Cloudfanatic receives an additional new direct IP allocation from ARIN
  • Cloudfanatic is Introducing 1-Click Apps

Tutorials

  • Secure Apache with Let’s Encrypt on Ubuntu 22.04
  • Setup Apache and Virtual Hosts on Ubuntu 22
  • Change SSH Port on Almalinux
  • Change SSH Port on Debian 10/11
  • Change SSH Port on Ubuntu 20/22
  • Firewalld Cheat Sheet
  • Ubuntu UFW Cheat sheet
  • How to copy my files to and from a linux vps
  • Create passwordless login with SSH keys and PUTTY
  • Accessing your server for the first time
  • DNS Manager
  • Controlling you instance
  • Home
  • Docs
  • Tutorials
  • Setup Apache and Virtual Hosts on Ubuntu 22

Setup Apache and Virtual Hosts on Ubuntu 22

Table of Contents
  • Overview
  • Step 1 — Installing Apache
  • Step 2 — Adjusting the Firewall to allow Apache web server
  • Step 3 — Checking your Web Server
  • Step 4 — Setting Up Virtual Hosts (Recommended)
  • Conclusion

Overview #

In this guide, you’ll learn how to install an Apache web server on your Ubuntu 22.04 server and setup a virtual host.

For Free SSL please follow this tutorial

Step 1 — Installing Apache #

sudo apt update
sudo apt install apache2

Step 2 — Adjusting the Firewall to allow Apache web server #

sudo ufw allow 'Apache'

Step 3 — Checking your Web Server #

sudo systemctl status apache2

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese>
     Active: active (running) since Tue 2022-04-26 15:33:21 UTC; 43s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 5089 (apache2)
      Tasks: 55 (limit: 1119)
     Memory: 4.8M
        CPU: 33ms
     CGroup: /system.slice/apache2.service
             ├─5089 /usr/sbin/apache2 -k start
             ├─5091 /usr/sbin/apache2 -k start
             └─5092 /usr/sbin/apache2 -k start

Test if your Apache is working correctly by opening your IP in your browser. You should see the default Ubuntu 22 Apache web page as in the below screenshot

Apache 2.4 default page
Apache 2.4 default page

This page indicates that Apache is working correctly. It also includes some basic information about important Apache files and directory locations.

Step 4 — Setting Up Virtual Hosts (Recommended) #

Apache on Ubuntu 22.04 has one server block enabled by default that is configured to serve documents from the /var/www/html directory. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html, create a directory structure within /var/www for a your_domain site, leaving /var/www/html in place as the default directory to be served if a client request doesn’t match any other sites.

Create the directory for your_domain as follows:

sudo mkdir /var/www/your_domain

Next, assign ownership of the directory to the user you’re currently signed in as with the $USER environment variable:

sudo chown -R $USER:$USER /var/www/your_domain

The permissions of your web root should be correct if you haven’t modified your umask value, which sets default file permissions. To ensure that your permissions are correct and allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others, you can input the following command:

sudo chmod -R 755 /var/www/your_domain

Next, create a sample index.html page using nano or your favorite editor:

sudo nano /var/www/your_domain/index.html

Inside, add the following sample HTML:

<html>
    <head>
        <title>Welcome to Your_domain!</title>
    </head>
    <body>
        <h1>Success!  The your_domain virtual host is working!</h1>
    </body>
</html>

Save and close the file.

In order for Apache to serve this content, it’s necessary to create a virtual host file with the correct directives.

sudo nano /etc/apache2/sites-available/your_domain.conf

Add in the following configuration block, which is similar to the default, but updated for your new directory and domain name:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName your_domain
    ServerAlias www.your_domain
    DocumentRoot /var/www/your_domain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Notice that we’ve updated the DocumentRoot to our new directory and ServerAdmin to an email that the your_domain site administrator can access. We’ve also added two directives: ServerName, which establishes the base domain that will match this virtual host definition, and ServerAlias, which defines further names that will match as if they were the base name.

Save and close the file when you are finished.

Now enable the file with the a2ensite tool:

sudo a2ensite your_domain.conf

Disable the default site defined in 000-default.conf:

sudo a2dissite 000-default.conf

Next, test for configuration errors:

sudo apache2ctl configtest
. . .
Syntax OK

Apache will now be serving your domain name. You can test this by navigating to http://your_domain, where you will see something like the following:

Success! The your_domain virtual host is working!

Conclusion #

Now that you have your web server installed, you have many options for the type of content you can serve and the technologies you can use to create a richer experience.

What are your Feelings
Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest
Still stuck? How can we help?

How can we help?

Updated on November 1, 2022
Secure Apache with Let’s Encrypt on Ubuntu 22.04Change SSH Port on Almalinux

Powered by BetterDocs

Table of Contents
  • Overview
  • Step 1 — Installing Apache
  • Step 2 — Adjusting the Firewall to allow Apache web server
  • Step 3 — Checking your Web Server
  • Step 4 — Setting Up Virtual Hosts (Recommended)
  • Conclusion

2023 Cloudfanatic Knowledge base

To the top ↑ Up ↑