crossorigin="anonymous"> How to use Apache Web Server to install and configure a website. – Subrang Safar: Your Journey Through Colors, Fashion, and Lifestyle

How to use Apache Web Server to install and configure a website.


Every once in a while, it’s good to take a step back and go over the basics. Not only does it help ground me as a tech writer, but it helps a lot of people who are learning the ropes of the technology I’m talking about.

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

How do you install and configure the server? Where do you keep the files?

Let’s walk through it, one step at a time. I will demonstrate on an Ubuntu server.

But first, a little more information.

See: How to Host Multiple Websites on Linux with Apache (TechRepublic Premium)

Differences between Apache on Ubuntu and Red Hat based distributions

The reason I have to tell you which Linux distribution I’m using is because the Ubuntu- and Red Hat-based variants of Apache run differently — from installation to configuration. For example, on Red Hat-based distributions, Apache is installed via the httpd package, while on Ubuntu-based distributions, the apache2 package will do the job. Another difference is where and how Apache is configured.

On a Red Hat-based distribution, most of your Apache configuration will be in /etc/httpd/conf/httpd.conf. In Ubuntu-based distributions, the configurations are in /etc/apache2/apache2.conf and /etc/apache2/sites-available/. There are still more differences, but you get the idea.

See: Apache Maven – Build Automation Tool Review (Tech Republic)

How to Install Apache on Ubuntu Server

There are several ways to install Apache on Ubuntu. If you just want the basic server software, you can open a terminal and issue the command:

sudo apt-get install apache2 -y

However, if you want a fully-fledged Linux Apache MySQL PHP (LAMP) stack, you would issue the command:

sudo apt-get install lamp-server^

Once you run one of these commands, you’ll have Apache running. You’ll also want to make sure Apache is enabled to start on server reboot (or boot). To do this, issue the command:

sudo systemctl enable apache2

You can verify your installation by opening a web browser and pointing to it. http://SERVER_IP (where SERVER_IP is the IP address of the server hosting Apache). The Apache Welcome page shown below will welcome you.

Official Apache Welcome Page running on Ubuntu Server. Photo: Jack Wallen

What is the page that Apache is serving? If you look in /var/www/html, you’ll find an index.html file. Let’s change that.

Back in the terminal window, rename this index.html file with the command:

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

Now, create a new welcome file. Issue the command:

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

In this file, paste the following two lines:

how are you

Save and close the file. Reload the web page in your browser and you should see a change like below.

Our new index.html page is being served by Apache. Photo: Jack Wallen

How to build a site for Apache

What we are going to do now is create a virtual host for the Apache service. A virtual host is a fancy name for a website that is served by Apache. You can host multiple virtual hosts on a single Apache server. In fact, you are only limited by the power of your hosting server and the bandwidth of your network.

So let’s create a virtual host named Test.

The first thing we’re going to do is create a directory to house tests with the command:

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

Next, we’ll give the new directory the appropriate ownership with the command:

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

Finally, we will grant the appropriate permissions with the command:

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

Copy our new index.html file to the test directory with the command:

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

Now we need to create a virtual host configuration so Apache knows where the test is. It will be placed in /etc/apache/sites-available. To do this we will create a test.conf file with the command

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

Paste the following into this file:

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

The most important line above begins. document rootAs it instructs Apache where to find the virtual host’s files. Save and close this file.

At this point, we’ve created a directory to hold the files, given it the appropriate ownership and permissions, and created a configuration for the virtual host. However, Apache is not yet aware of the new site. Why? Because the configuration file resides inside. Sites are available.. What we need to do is create a link in this configuration. /etc/apache2/sites-enabled directory found only those configurations. Sites active are enabled on the Apache server.

On non-Ubuntu servers, you must use ln (for The link) command to do so. However, there is a simple utility on Ubuntu that will create that site for you. It is useful a2ensite. If we run the command:

sudo a2ensite test.conf

Our test virtual host will then be active.

After this command succeeds, you must reload Apache (which will only reload the configuration files, not restart the web server) with the command:

sudo systemctl reload apache2

Now, if you point to your browser. http://SERVER_IP/test (where SERVER_IP is the server’s IP address) You get the same “Hello, TechRepublic!” Should be visible. Welcome as you did with the basic index.html file, only it’s being served from our newly created virtual host.

You’ve just installed the Apache web server, edited the index.html file, and then created your virtual host. You can take this simple approach and use it as the basis for spinning up all the websites that Apache serves you.

This article was originally published in October 2020. It was updated in January 2025 by Antony Peyton.



Source link

Leave a Reply

Translate »