Skip to content

The Dummy Programmer

Stories of daily programming

  • Home
  • My other dummy projects
  • Games
    • Space Y: An Asteroids clone
  • Services
    • What’s my user agent
    • What’s my IP address
  • About
  • Toggle search form

Add a new website to an Apache installation

Posted on January 12, 2022January 1, 2024 By No Comments on Add a new website to an Apache installation

Hello my friends! Today I would like to talk about the Linux world once again. In a previous post we saw how to set up a LAMP server in Linux Ubuntu. Basically the installation of LAMP provides a default website, or a “virtualhost” as they are called in Apache, with a default web page. This is the default page we are talking about:

Apache default page

If you are planning to have a single website running in your server, you can simply work with this default virtualhost. But, if you need to host multiple websites in your Apache installation, you have to add a new virtual host for each site. We will see how in the next paragraphs.

The Apache configuration file

To create a new virtualhost in our Apache installation we have to modify the Apache configuration file, which is usually located in /etc/apache2/apache2.conf. But pay attention…

In some installations, and this is true also for Linux Ubuntu, the apache2.conf doesn’t contains the complete Apache configuration. Some configuration pieces are stored in separate files and this applies to virtualhosts. So to create a new virtualhost, we have to create a new file in the correct location.

Add a new virtualhost

You can find virtualhost configuration files in the directory /etc/apache2/sites-available.

If you list the content of the directory with the command

ls -l

you will see the following files:

-rw-r--r-- 1 root root 1332 Sep 30  2020 000-default.conf
-rw-r--r-- 1 root root 6338 Sep 30  2020 default-ssl.conf

This is what they contain:

  • 000-default.conf: is the configuration of the virtualhost that is listening on http port 80
  • default-ssl.conf: is the configuration of the virtualhost that is listening on ssl port 443

We want to create a new virtualhost that responds on port 80 to a certain URL, for example “www.mytestsite.com”.

The website name is random, you can use whatever name you want and, don’t worry, you don’t have to buy a domain to see it working because we will see a little trick to make it work with our local virtual machine.

Let’s start by making a copy of the first file, so run the following command:

cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mytestsite.com.conf 

We need to modify the file to contain the content below. Since we copied it from the default file, a part of it should already be ready.

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName mytestsite.com
        ServerAlias www.mytestsite.com
        ServerAdmin webmaster@mytestsite.com
        DocumentRoot /var/www/mytestsite.com/public_html

        <Directory /var/www/mytestsite.com/public_html>
           Options -Indexes +FollowSymLinks
           AllowOverride All
        </Directory>

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn
        ErrorLog ${APACHE_LOG_DIR}/mytestsite_error.log
        CustomLog ${APACHE_LOG_DIR}/mytestsite_access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

Now we are going to create the directory that will contain our new website. Note that this must match the directory specified in the option “DocumentRoot” and in the tag “Directory” of the configuration file.

sudo mkdir /var/www/mytestsite.com
sudo mkdir /var/www/mytestsite.com/public_html

In the folder “/var/www/mytestsite.com/public_html” create a file “index.html” with this simple content:

<!DOCTYPE html>
<html>
<title>This my test site!</title>
<body>
   <h1>This is my test site!</h1>
</body>
</html>

Execute the following command to “register” our new virtualhost. Note that parameter “mytestsite.com” must match the file name you created in /etc/apache2/sites-available, but without the suffix “.conf”.

sudo a2ensite mytestsite.com

And then restart the Apache service:

sudo service apache2 restart

To make the website reachable from our Windows host operating system, we have to add the following line to the Windows host file, which is located under “C:\Windows\System32\drivers\etc”:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost


192.168.1.90		www.mytestsite.com    # this the new line

The line added is the last one. Remember that in my case 192.168.1.90 is the address of my VirtualBox Host-Only Network adapter, your address may be different.

That’s it, now if you type the address www.mytestsite.com in your Windows host browser, you should see test page we created above:

Our test page running through the new virtualhost

For the moment we can close here … I hope this post was useful. Let me know your comments!

Apache, LAMP, Linux Tags:lamp, linux, ubuntu, virtualbox

Post navigation

Previous Post: Sample code of a cross platform analog clock using .NET 6, C# and Avalonia UI
Next Post: Run an Apache website under SSL

Related Posts

Redirect to HTTPS using Apache .htaccess file Apache
Install Imagick extension for PHP on Linux CentOS 7 CentOS
Plesk panel update error on Linux CentOS 7: The GPG keys listed for the “dell-system-update_dependent” repository are already installed but they are not correct for this package CentOS
Run an ASP.NET web API in CentOS 7 .NET 7
Install LAMP in Ubuntu Server LAMP
Run an Apache website under SSL Apache

Leave a Reply Cancel reply

Please do not insert personal or sensitive data in the comment.


Search

Related posts

  • Run an Apache website under SSL
  • Sql Server add constraint WITH NOCHECK
  • FuelPHP & PHP installation issues
  • Put an ASP.NET website under maintenance

Categories

  • .NET 6 (1)
  • .NET 7 (1)
  • AJAX (1)
  • Android (2)
  • Apache (4)
  • ASP.NET (9)
  • ASP.NET MVC (3)
  • Avalonia UI (1)
  • BCP (1)
  • Bitlocker (2)
  • C# (14)
  • CentOS (4)
  • ClosedXML (1)
  • CLR (1)
  • DNS (1)
  • Encryption (3)
  • Excel (2)
  • FuelPHP (3)
  • Games (2)
  • Google Chrome (1)
  • GSuite (1)
  • HTML (1)
  • Imagick (2)
  • Javascript (1)
  • Kindle (1)
  • LAMP (3)
  • Linux (7)
  • MariaDB (2)
  • Mathematics (2)
  • MySql (4)
  • NPOI (1)
  • Office 365 (1)
  • Perl (1)
  • PHP (6)
  • Programming (1)
  • Remote desktop (1)
  • SFTP (2)
  • Sockets (3)
  • Sql Server (20)
  • Sql Server 2016 (14)
  • Sql Server 2019 (1)
  • SSL (4)
  • Task scheduler (1)
  • Telerik ASP.NET AJAX (2)
  • The Dummy Programmer Chat (2)
  • Threading (5)
  • Tools (1)
  • TPL (3)
  • TypeScript (3)
  • Ubuntu (4)
  • Virtualization software (3)
  • Visual Studio (1)
  • Visual Studio Code (2)
  • VueJS (1)
  • Web fonts (1)
  • Web programming (6)
  • Windows (12)
  • Windows 10 (15)
  • Windows Forms (1)
  • Windows Server (6)

Copyright © 2024 The Dummy Programmer | Privacy Policy | Terms of use |

Powered by PressBook Masonry Dark

Manage Cookie Consent
This site doesn’t collect user personal data and doesn’t install profiling or analytical cookies, either its own or from third parties. Read our privacy policy for more info.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
View preferences
{title} {title} {title}