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



