Installing and Configuring the Apache2 Web Server

Apache HTTP Server – named after the tribes of the North American Indians of Apache, is also an abbreviation of “a patchy server” – a free web server.

An example of an installation command in Ubuntu:

sudo apt-get install apache2

Example installation in CentOS:

sudo yum install httpd

We check to open in the browser the address http://HOSTNAME
Normally, a standard inscription should appear “It works!“.

Web Storage Directory – /var/www/
Directory of configuration files – /etc/apache2/

A separate virtual host is created for each site. The standard virtual host file is located at /etc/apache2/sites-available/000-default.conf

Suppose we have an ixnfo.com domain. Create a new file in the /etc/apache2/sites-available/example.conf and prescribe the settings in it:

<VirtualHost *:80>
     ServerAdmin test@ixnfo.com
     ServerName ixnfo.com
     ServerAlias www.ixnfo.com
     DocumentRoot /var/www/ixnfo.com/
  
     <Directory /var/www/ixnfo.com>
     Options -Indexes
     AllowOverride All
     Order allow,deny
     allow from all
     </Directory> 
  
     #You can restrict IP access to some directories, for example, the admin panel in this way:
     <Directory /var/www/ixnfo.com/admin>
     Order allow,deny
     allow from 127.0.0.1 192.168.0.47 172.16.30.5
     </Directory>
  
     ErrorLog /var/log/ixnfo.com.error.log
     CustomLog /var/log/ixnfo.com.access.log combined
</VirtualHost>

I’ll describe the options I mentioned above:
-Indexes – in the absence of index files index.php, index.html, etc. the contents of the directories will not be displayed.
FollowSymLinks – allows you to navigate through symbolic links.
-MultiViews – if you specify an example of a non-existent directory http://ixnfo.com/img/ the inclusion of this directive will allow you to redirect to the file http://ixnfo.com/img.php or http://ixnfo.com/img.jpg etc . if any one of them exists, such manipulations can badly affect the indexing of links by search engines, so it is better to disable this directive.
AllowOverride All – allows the use of additional configuration files. htaccess.

Next, make a link to this file in the directory /etc/apache2/sites-enabled/. You can do this with commands (the second one to delete, ending .conf is not specified):

sudo a2ensite ixnfo.com
sudo a2dissite ixnfo.com

Check if the configuration is correct:

sudo apachectl -t

If you need to activate any modules, you can also do this by eg commands (the first command to enable, the second to disable):

sudo a2enmod rewrite
sudo a2dismod rewrite

To activate additional configuration files:

sudo a2enconf TEXT
sudo a2disconf TEXT

Now create a directory for the site and copy the files into it:

sudo mkdir /var/www/ixnfo.com

In the /etc/apache2/conf.d/security file, we’ll change some of the web server security settings:

ServerTokens Prod
ServerSignature Off

Restart Apache2 for the changes to take effect:

sudo /etc/init.d/apache2 restart
sudo service apache2 restart

If the server uses many IP addresses, and apache2 should not work at all, then you can specify in which it works in /etc/apche2/ports.conf, you can also change the port, for example:

Listen 192.0.3.1:80
Listen 192.0.3.2:80
Listen 192.0.1.10:8080

To work at all:

Listen 80

An example of disabling/enabling Apache2 autorun at operating system startup (Ubuntu):

systemctl is-enabled apache2
systemctl disable apache2
systemctl enable apache2

View apache2 version:

apache2 -v

See also my articles:
Access Control Apache2
Installing Certbot in Ubuntu
Redirecting requests to SSL
Installing and Configuring Nginx

Leave a comment

Leave a Reply