Skip to main content
Creating a vhost

What are vhosts?

A vhost, short for “virtual host”, is used to enable a web server to manage several distinct websites on a single physical machine. Here are some of its main functions:

Web site isolation: A vhost allows different web sites to be isolated from each other, meaning that each site can have its own configuration, files and settings without interfering with other sites hosted on the same server.

Multiple domain name management: Using vhosts, a single server can respond to requests for several different domain names. This means that separate websites with unique domain names can be hosted on the same machine.

Use of SSL/TLS certificates: vhosts also enable the use of separate SSL/TLS certificates for each website, which is essential to ensure secure connections via HTTPS.

Resource optimization: By grouping several websites together on a single machine, vhosts enable more efficient use of hardware and software resources, which can be particularly advantageous for servers with a large number of websites to host.

In short, vhosts are an essential tool for web server administrators, offering an efficient solution for hosting multiple websites on a single infrastructure while ensuring isolation, security and ease of management.
 

What is a hosts file?

The hosts file allows you to easily map IP addresses to domain names, so you don't have to remember the server's IP address every time.

nb : for this tutorial, our site will be opendata.tag
 

How do I configure the Linux hosts file?


1- open a terminal.


To open the hosts file and modify it, type the following command:

sudo nano /etc/hosts


it is presented as follows:


 

2. Add a new entry to hosts


Each line in the hosts file is a mapping for a single IP address and takes the following form:

       IP address Domain name

For example, you could map the address 127.0.0.1 to the domain opendata.tag. This entry would look like this (and would be added to the bottom of the file):


  
   127.0.0.1 opendata.tag
To complete the creation of our virtual server, let's finish with one last element

create a directory to store site files


we'll proceed as follows:



open a terminal

  

  1. cd /

    to go to the root of the computer
     

  2. issue the command

    cd /var/www

     

    to access the www folder if it exists, otherwise create it with the command then access it.

  3. sudo mkdir www

     

  4. then create a directory with the name of our local site

    sudo mkdir opendata

     

  5. then access it with the

    cd opendata

     

  6. then we give him all the privileges

    sudo chmod 777 .

     

     

move site files to this directory

 

create .conf file

 

 

 

 

  1. go to the root of our machine 

    cd /

     

  2. access the etc directory

    cd /etc

     

  3. then to the apache2 directory

    cd apache2

     

  4. inside the apache2 directory we'll access the “sites-available” directory if it exists, otherwise we'll create a “sites-available” directory.

    sudo mkdir sites-available

     

  5. access this directory 

    cd sites-available


 

  1.   then modify the .conf file if it exists

    sudo nano monsite.conf


 

  1. otherwise create a
touch monsite.conf


the following configuration is used:
 

<VirtualHost *:80>
        ServerAdmin usermail
        ServerName opendata.tag
        ServerAlias www.opendata.tag
        DocumentRoot /var/www/opendata
        <Directory /var/www/opendata>
                Options Indexes FollowSymLinks
                AllowOverride All
                Order Deny,Allow
                Allow from all
                DirectoryIndex disabled
           DirectoryIndex index.php
        </Directory>
    # preciser la version de PHP
           #<FilesMatch ".+\.php$">
           #        SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost"
       #    </FilesMatch>
       ErrorLog /var/www/opendata/logs/error.log
        CustomLog /var/www/opendata/logs/access.log combined
</VirtualHost>


then Ctrl+o to save and Ctrl+x to close.
 

Activating the configuration file


simply issue the command

a2ensite opendata.conf

Restart the Apache service with the command

sudo service apache2 restart

go to your site address to test example http://opendata.tag


good to know:
instead of creating a directory to import site files into the var folder, you can also create a symbolic link to your site's folder
via the command.

f my folder is in /home/user/opendata

ln -s /home/user/opendata /var/www/


explanation of configuration file elements

 <VirtualHost *:80> : this line indicates the start of the virtual host definition. The * means that the virtual host listens on all server IP addresses, and the 80 means that it uses port 80, which is the default port for the HTTP protocol.

•  ServerAdmin usermail : this line specifies the server administrator's e-mail address, which will be displayed in the event of an error.

•  ServerName opendata.tag : this line specifies the main domain name of the virtual host, which must match that registered in the DNS system.

•  ServerAlias www.opendata.tag : this line specifies one or more alternative domain names for the virtual host, which will be treated in the same way as the main name.

•  DocumentRoot /var/www/opendata : this line specifies the virtual host's root directory, which contains the website files to be served.

•  <Directory /var/www/opendata> : this line indicates the beginning of a section that contains directives specific to the virtual host's root directory.

•  Options Indexes FollowSymLinks : this line specifies directory options, which control its behavior. Here, Indexes means that if no index page is found, the server displays the list of files in the directory, and FollowSymLinks means that the server follows symbolic links in the directory.


•  AllowOverride All : chis line specifies that the server allows .htaccess files in the directory, which may contain additional or different directives to those in the global configuration.

•  Order Deny,Allow : this line specifies the order in which the server evaluates Deny and Allow directives, which control access to the directory. Here, Deny,Allow means that the server first denies access to all, then allows access to those specified by the Allow directive.

•  Allow from all : this line specifies that the server authorizes access to the directory for all visitors, without restriction.


•  DirectoryIndex disabled : this line specifies that the server does not look for an index page in the directory, such as index.html or index.php.

•  DirectoryIndex index.php :this line specifies that the server uses the index.php file as the index page in the directory, if it exists.

•  </Directory> : this line indicates the end of the section containing directives specific to the virtual host's root directory.

•  # specify PHP version : this line is a comment, which is not interpreted by the server. It is used to indicate to the developer that it is necessary to specify the PHP version used by the virtual host.

•  #<FilesMatch ".+\.php$"> : this line is also a comment, which contains the beginning of a section containing directives specific to the PHP files of the virtual host. The # means that the line is disabled, and the .+\.php$ means that the section applies to all files whose names end in .php.

•  # SetHandler "proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost" : this line is also a comment, containing a directive that specifies the handler to be used for PHP files on the virtual host. The # means that the line is disabled, and the proxy:unix:/var/run/php/php8.1-fpm.sock|fcgi://localhost means that the server uses the FastCGI protocol to communicate with the PHP-FPM process, which executes PHP files with PHP version 8.1.

•  #</FilesMatch> : this line is also a comment, which contains the end of the section containing directives specific to PHP files on the virtual host. The # means that the line is disabled.

•  ErrorLog /home/computeruser/Documents/error_log :this line specifies the file in which the server logs error messages relating to the virtual host.

•  CustomLog /var/www/my-nutribe/logs/access.log combined : cthis line specifies the file in which the server logs information on requests received by the virtual host, using the combined format, which contains data such as the visitor's IP address, request date and time, HTTP method, requested URL, status code, response size, referrer, and user agent.

•  </VirtualHost> : this line indicates the end of the virtual host definition.
 

creating error.log and succes.log files  

error.log and succes.log are examples of log files, which record the history of events on a server, computer or application. These files are useful for understanding usage, resolving errors, compiling statistics or preventing fraud.


The error.log  file contains information on errors that have occurred on the system, such as exceptions, bugs or malfunctions. It enables developers to debug code and correct problems.
The succes.log file contains information on successful system operations, such as connections, transactions or validations. It enables users to check that the system is running smoothly and to trace activities.

to create them, simply open a terminal
-access the site folder according to its location in /var/www/opendata and then create the logs folder
-if you've imported the site's folders via a symbolic link, go to the original folder and create a logs folder at the root of your web site folder.

Finally, we'll create the error.log and succes.log files with the command

touch error.log succes.log
Profile picture for user landry Landry

Écrit le

Il y'a 1 year
Modifié
Il y'a 1 day
Loading ...
WhatsApp
Support Habeuk : +237 694 900 622
WhatsApp Send