Linux praktikum 4
Installation of a name-based virtual web server with PHP support
Task: learn to set up and configure popular web servers Apache and Nginx
Conditions: this praktikum assumes that the Linux server IP address is 192.168.1.136
Make sure that nothing is launched on port 80
sudo lsof -i :80
Update package list from the repositories
sudo apt-get update
Install the Apache web server
apt-get install apache2
Check which catalogues Apache was installed to
dpkg -L apache2
Search for the PHP module for Apache
apt-cache search mod-php
Install the PHP module
sudo apt-get install libapache2-mod-php
Create catalogues for virtual hosts
sudo mkdir -p /var/www/example.com/public_html sudo mkdir -p /var/www/test.com/public_html
Recursively change the owner of directories
sudo chown -R www-data:www-data /var/www/example.com/public_html sudo chown -R www-data:www-data /var/www/test.com/public_html
Change allowances for catalogue /var/www rwx r-x r-x
sudo chmod -R 755 /var/www
Create file index.html
sudo nano /var/www/example.com/public_html/index.html
Write the following:
<html>
<head>
<title>Welcome to Example.com!</title>
</head> <body>
<h1>Success! The example.com virtual host is working!</h1> </body>
</html>
Save and copy this file
sudo cp /var/www/example.com/public_html/index.html /var/www/test.com/public_html/index.html
Change the contents of the copied file
sudo nano /var/www/test.com/public_html/index.html
Change "example" to "test":
<html>
<head>
<title>Welcome to Test.com!</title>
</head> <body>
<h1>Success! The test.com virtual host is working!</h1> </body>
</html>
Save
Create file /var/www/html/index.php
sudo nano /var/www/html/index.php
with the following contents
<?php phpinfo(); ?>
Save
Remove port 80 from the default host /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
Copy the configuration file for the first virtual host
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf
Change the content to the following
sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Copy this file to the virtual host directory
sudo cp /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-available/test.com.conf
And change its contents
sudo nano /etc/apache2/sites-available/test.com.conf
<VirtualHost *>
ServerAdmin admin@test.com
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/test.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Activate the virtual hosts
sudo a2ensite example.com.conf
cd /etc/apache2/sites-enabled sudo ln -s ../sites-available/test.com.conf
Check the configuration and restart
apachectl configtest sudo service apache2 restart
Since we don't have a DNS server installed, write the IP address and names of our virtual servers manually in file /etc/hosts sudo nano /etc/hosts
192.168.1.136 example.com 192.168.1.136 test.com
If we want to check this on a Windows machine, add these lines to the file C:\Windows\System32\drivers\etc\hosts
Check the health of virtual servers by name
http://example.com http://test.com
or by using curl in the Linux terminal or Windows browser
curl -v http://example.com curl -v http://test.com curl -v http://192.168.1.136
Installation of nginx
Stop the Apache server since since it's not possible to keep 2 servers on the same port
sudo service apache2 stop
Install nginx
sudo apt-get install nginx
Go to catalogue
cd /etc/nginx/sites-available
Copy the default configuration file
sudo cp default nginx
Edit the config file
sudo nano nginx
The contents should look like this
Create html file with any content, such as «hello Nginx»
sudo nano /var/www/html/nginx.html
Go to the site activation directory
cd /etc/nginx/sites-enabled/
Remove the link to default site
sudo rm default
Add the link to the new site
sudo ln -s /etc/nginx/sites-available/nginx
Check nginx syntax
nginx -t
Restart nginx
sudo service nginx restart
Check
curl -v http://192.168.1.136
Simultaneous use of Apache and Nginx
sudo nano /etc/apache2/ports.conf
#Listen 80 Listen 127.0.0.1:81
Make sure that all Apache's configuration files have <VirtualHost *>
Restart nginx
sudo systemctl restart nginx
Initiate apache
sudo systemctl start apache2
We can make sure that each port is listening
sudo netstat -nlpt | grep :8[01]
Check whether Apache works through 127.0.0.1 with curl
curl http://127.0.0.1:81 curl http://127.0.0.1:81/index.php curl -H 'Host: example.com' http://127.0.0.1:81 curl -H 'Host: test.com' http://127.0.0.1:81
Edit the nginx configuration file so that all requests on port 80 redirected to apache
sudo nano /etc/nginx/sites-available/nginx
Restart nginx
sudo systemctl restart nginx
Listen to port 81 on a local interface
sudo apt-get install ngrep sudo ngrep -W byline -d lo "" port 81
At the same time, on a different machine, try to open the sites test.com, example.com and 192.168.1.136
Now apache will respond to all requests
Server: Apache/2.4.18 (Ubuntu).
Add these lines to the config nginx file before location /
sudo nano /etc/nginx/sites-available/nginx
sudo systemctl restart nginx
Listen to port 80 on interface c
sudo ngrep -W byline -d enp0s8 "" port 80
At the same time, on a different machine, try opening the html file 192.168.1.136/nginx.html Now all the requests with an html file in the address will be handled by nginx
Server: nginx/1.10.0 (Ubuntu).



