Every then ofttimes I find it good to accept a step back and go through the basics. Information technology non only helps to footing me as a tech author, but information technology helps a lot of people who are just learning the ropes of whatever piece of technology I'yard talking near.

This fourth dimension it's all about the Apache web server, a piece of software that's been around for decades, happily serving up modest and large websites without fail. Apache works seamlessly with MySQL, PHP, and a host of other packages, so you can serve upward simple static or incredibly dynamic websites.

How do yous install and configure the server? Where practise you place files?

Let's walk through this, 1 step at a time. I'll be demonstrating on Ubuntu Server xx.04.

But beginning, a bit more information.

SEE: Phone interview cheat sheet: Software developer (TechRepublic Premium)

The difference between Apache on Ubuntu and Red Hat-based distributions

The reason why I have to specify what Linux distribution I'one thousand using is because Ubuntu- and Red Chapeau-based variants Apache differently–from installation to configuration. For example, on Scarlet Lid-based distributions, Apache is installed via the httpd bundle, whereas on Ubuntu-based distributions, the apache2 package volition practise the trick. Another divergence is where and how Apache is configured. In Red Lid-based distributions, much of your Apache configuration will happen in /etc/httpd/conf/httpd.conf. In Ubuntu-based distributions, the configurations are in /etc/apache2/apache2.conf and /etc/apache2/sites-available/. In that location are even so more differences to be had, only you get the idea.

How to install Apache on Ubuntu Server

There are a number of ways y'all can install Apache on Ubuntu. If y'all but desire the basic server software, you can open a terminal and issue the command:

sudo apt-go install apache2 -y

Nonetheless, if y'all want a full-blown Linux Apache MySQL PHP (LAMP) stack, you'd issue the command:

sudo apt-get install lamp-server^

Once you run either of those commands, y'all'll have Apache upward and running. Yous'll also want to make sure to enable Apache to start upon a server reboot (or boot). To practice that, upshot the command:

sudo systemctl enable apache2

You can verify your installation past opening a web browser and pointing information technology to http://SERVER_IP (where SERVER_IP is the IP accost of the server hosting Apache). You should be greeted past the Apache Welcome Folio (Effigy A).

Figure A

The official Apache Welcome Folio running on Ubuntu Server.

What is that page Apache is serving up? If y'all look in /var/www/html, you lot'll find the index.html file–let's change it.

Dorsum at the last window, rename that alphabetize.html file with the command:

sudo mv /var/www/html/index.html /var/world wide web/html/alphabetize.html.bak

Now, let'due south create a new welcome file. Issue the command:

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

In that file, paste the following:

Hello, TechRepublic!

How are you doing?

Relieve and close the file. Reload the spider web page in your browser and you should see the change (Effigy B).

Figure B

Our new alphabetize.html page is being served by Apache.

How to create a site for Apache

What we're going to practise now is create a virtual host for Apache to serve upward. A virtual host is a fancy name for a website that's served by Apache. Yous can accept numerous virtual hosts served up on a single Apache server. In fact, you are only express to the power of your hosting server and the bandwidth of your network.

So let's create a virtual host chosen test.

The first thing we're going to do is create a directory to house examination with the control:

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

Next, we'll requite the new directory the proper buying with the control:

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

Finally, nosotros'll grant the proper permissions with the command:

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

Copy our new index.html file into the test directory with the control:

sudo cp /var/world wide web/html/index.html /var/www/html/test/

Now we take to create the virtual host configuration so Apache knows where examination is. This will be housed in /etc/apache/sites-available. To do that we'll create the test.conf file with the control:

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

In that file paste the following:


ServerAdmin admin@instance.com
ServerName instance.com
ServerAlias www.example.com
DocumentRoot /var/www/html/test
ErrorLog ${APACHE_LOG_DIR}/mistake.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

The most important line in a higher place begins with DocumentRoot, every bit that instructs Apache where the files for the virtual host will be found. Relieve and shut that file.

At this indicate, we've created the directory to house the files, given it the proper ownership and permissions, and created a configuration for the virtual host. However, Apache is all the same not aware of the new site. Why? Because the configuration file lives in sites-available. What we take to do is create a link from that configuration into the /etc/apache2/sites-enabled directory. Only those configurations plant in sites-enabled are active on the Apache server.

On non-Ubuntu servers, y'all have to use the ln (for link) command to do this. Nonetheless, on Ubuntu there'southward a handy utility that will create that site for you. Said utility is a2ensite. If we run the command:

sudo a2ensite exam.conf

Our examination virtual host volition then be enabled.

After that command succeeds, you then must reload Apache (which volition only reload the configuration files, non restart the spider web server) with the command:

sudo systemctl reload apache2

Now, if you betoken your browser to http://SERVER_IP/test (where SERVER_IP is the IP accost of the server) you should see the same Hello, TechRepublic welcome as you did with the bones index.html file, only it'south being served from our newly-created virtual host.

You've just installed the Apache web server, edited the index.html file, and so created your very ain virtual host. Y'all tin take this unproblematic how-to and use information technology as a basis for spinning upward all the Apache-served websites you need.

Subscribe to TechRepublic's How To Brand Tech Piece of work on YouTube for all the latest tech communication for business pros from Jack Wallen.


Prototype: Jack Wallen