How to Use the Apache Web Server to Install and Configure a Website

How to Use the Apache Web Server to Install and Configure a Website

How to Use the Apache Web Server to Install and Configure a Website

Home » News » How to Use the Apache Web Server to Install and Configure a Website
Table of Contents

Occasionally, taking a step again and going via the fundamentals is nice. It not solely helps to floor me as a tech author, however it helps lots of people who’re simply studying the ropes of no matter piece of know-how I’m speaking about.

This time it’s all concerning the Apache net server, a chunk of software program that’s been round for many years, fortunately serving up small and huge web sites with out fail. Apache works seamlessly with MySQL, PHP, and a bunch of different packages, so you possibly can serve up easy static or extremely dynamic web sites.

How do you put in and configure the server? The place do you place recordsdata?

Let’s stroll via this, one step at a time. I’ll be demonstrating on Ubuntu Server.

However first, a bit extra info.

SEE: Methods to Host A number of Web sites on Linux with Apache (roosho Premium)

The distinction between Apache on Ubuntu and Pink Hat-based distributions

The rationale why I’ve to specify what Linux distribution I’m utilizing is as a result of Ubuntu- and Pink Hat-based variants Apache in a different way — from set up to configuration. For instance, on Pink Hat-based distributions, Apache is put in through the httpd package deal, whereas on Ubuntu-based distributions, the apache2 package deal will do the trick. One other distinction is the place and the way Apache is configured.

In Pink Hat-based distributions, a lot of your Apache configuration will occur in /and many others/httpd/conf/httpd.conf. In Ubuntu-based distributions, the configurations are in /and many others/apache2/apache2.conf and /and many others/apache2/sites-available/. There are nonetheless extra variations available, however you get the concept.

SEE: Apache Maven — Construct Automation Device Evaluation (roosho)

Methods to set up Apache on Ubuntu Server

There are a number of methods you possibly can set up Apache on Ubuntu. If you happen to merely need the essential server software program, you possibly can open a terminal and subject the command:

sudo apt-get set up apache2 -y

Nevertheless, if you would like a full-blown Linux Apache MySQL PHP (LAMP) stack, you’d subject the command:

sudo apt-get set up lamp-server^

When you run both of these instructions, you’ll have Apache up and working. You’ll additionally wish to make sure that to allow Apache to start out upon a server reboot (or boot). To try this, subject the command:

sudo systemctl allow apache2

You may confirm your set up by opening an online browser and pointing it to http://SERVER_IP (the place SERVER_IP is the IP handle of the server internet hosting Apache). You need to be greeted by the Apache Welcome Web page as proven beneath.

the official apache welcome web page working on ubuntu server picture jack wallen

What’s that web page Apache is serving up? If you happen to look in /var/www/html, you’ll discover the index.html file. Let’s change it.

Again on the terminal window, rename that index.html file with the command:

sudo mv /var/www/html/index.html /var/www/html/index.html.bak

Now, let’s create a brand new welcome file. Situation the command:

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

In that file, paste the next two strains:

How are you doing?

Save and shut the file. Reload the net web page in your browser and you must see the change as proven beneath.

our new indexhtml web page is being served by apache picture jack wallen

Methods to create a web site for Apache

What we’re going to do now could be create a digital host for Apache to serve up. A digital host is a flowery identify for a web site that’s served by Apache. You may have quite a few digital hosts served up on a single Apache server. The truth is, you’re solely restricted to the ability of your internet hosting server and the bandwidth of your community.

So let’s create a digital host known as check.

The very first thing we’re going to do is create a listing to accommodate check with the command:

sudo mkdir -p /var/www/html/check

Subsequent, we’ll give the brand new listing the correct possession with the command:

sudo chown -R $USER:$USER /var/www/html/check

Lastly, we’ll grant the correct permissions with the command:

sudo chmod -R 755 /var/www/html/check

Copy our new index.html file into the check listing with the command:

sudo cp /var/www/html/index.html /var/www/html/check/

Now we have now to create the digital host configuration so Apache is aware of the place check is. This will probably be housed in /and many others/apache/sites-available. To try this we’ll create the check.conf file with the command:

sudo nano /and many others/apache2/sites-available/check.conf

In that file paste the next:

ServerAdmin [email protected]
ServerName instance.com
ServerAlias www.instance.com
DocumentRoot /var/www/html/check
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/entry.log mixed

An important line above begins with DocumentRoot, as that instructs Apache the place the recordsdata for the digital host will probably be discovered. Save and shut that file.

At this level, we’ve created the listing to accommodate the recordsdata, given it the correct possession and permissions, and created a configuration for the digital host. Nevertheless, Apache continues to be not conscious of the brand new web site. Why? As a result of the configuration file lives in sites-available. What we have now to do is create a hyperlink from that configuration into the /and many others/apache2/sites-enabled listing. Solely these configurations present in sites-enabled are energetic on the Apache server.

On non-Ubuntu servers, you must use the ln (for hyperlink) command to do that. Nevertheless, on Ubuntu there’s a useful utility that may create that web site for you. Mentioned utility is a2ensite. If we run the command:

sudo a2ensite check.conf

Our check digital host will then be enabled.

After that command succeeds, you then should reload Apache (which is able to solely reload the configuration recordsdata, not restart the net server) with the command:

sudo systemctl reload apache2

Now, for those who level your browser to http://SERVER_IP/check (the place SERVER_IP is the IP handle of the server) you must see the identical “Good day, roosho!” welcome as you probably did with the essential index.html file, solely it’s being served from our newly-created digital host.

You’ve simply put in the Apache net server, edited the index.html file, after which created your very personal digital host. You may take this easy how-to and use it as a foundation for spinning up all of the Apache-served web sites you want.

This text was initially revealed in October 2020. It was up to date by Antony Peyton in January 2025.

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
share this article.

Enjoying my articles?

Sign up to get new content delivered straight to your inbox.

Please enable JavaScript in your browser to complete this form.
Name