Linux praktikum 4: Difference between revisions

From TÜ Narva Kolledži Wiki
Jump to navigation Jump to search
Content deleted Content added
No edit summary
No edit summary
 
(28 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<br>
Make sure that nothing is launched on port 80


<pre>sudo lsof -i :80</pre>
<pre>sudo lsof -i :80</pre>


Update package list from the repositories<br>
Update package list from the repositories
<pre>sudo apt-get update</pre>
<pre>sudo apt-get update</pre>


Install the Apache web server<br>
Install the Apache web server
<pre>apt-get install apache2</pre>
<pre>apt-get install apache2</pre>


Check which catalogues Apache was installed to<br>
Check which catalogues Apache was installed to
<pre>dpkg -L apache2</pre>
<pre>dpkg -L apache2</pre>


Search for the PHP module for Apache<br>
Search for the PHP module for Apache
<pre>apt-cache search mod-php</pre>
<pre>apt-cache search mod-php</pre>


Install the PHP module<br>
Install the PHP module
<pre>sudo apt-get install libapache2-mod-php</pre>
<pre>sudo apt-get install libapache2-mod-php</pre>


Create catalogues for virtual hosts<br>
Create catalogues for virtual hosts
<pre>
<pre>
sudo mkdir -p /var/www/example.com/public_html<br>
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>
</pre>


Recursively change the owner of directories<br>
Recursively change the owner of directories
<pre>
<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>
</pre>


Change allowances for catalogue /var/www rwx r-x r-x<br>
Change allowances for catalogue /var/www rwx r-x r-x
<pre>sudo chmod -R 755 /var/www</pre>
<pre>sudo chmod -R 755 /var/www</pre>


Create file index.html<br>
Create file index.html
<pre>sudo nano /var/www/example.com/public_html/index.html</pre>
<pre>sudo nano /var/www/example.com/public_html/index.html</pre>


Write the following:<br>
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>
Save and copy this file
<pre>sudo cp /var/www/example.com/public_html/index.html /var/www/test.com/public_html/index.html</pre>
<pre>sudo cp /var/www/example.com/public_html/index.html /var/www/test.com/public_html/index.html</pre>


Change the contents of the copied file<br>
Change the contents of the copied file
<pre>sudo nano /var/www/test.com/public_html/index.html</pre>
<pre>sudo nano /var/www/test.com/public_html/index.html</pre>


Change "example" to "test":<br>
Change "example" to "test":

[[File:SumCodeForLinux2.jpg|center left]]
<pre>
<html>
<head>
<title>Welcome to Test.com!</title>
</head> <body>
<h1>Success! The test.com virtual host is working!</h1> </body>
</html>
</pre>


Save
Save
Line 61: Line 76:
<pre>sudo nano /var/www/html/index.php
<pre>sudo nano /var/www/html/index.php
</pre>
</pre>
with the following contents<br>
with the following contents
<pre>
<pre>
<?php<br>
<?php
phpinfo();<br>
phpinfo();
?>
?>
</pre>
</pre>
Line 70: Line 85:
Save
Save


Remove port 80 from the default host /etc/apache2/sites-available/000-default.conf<br>
Remove port 80 from the default host /etc/apache2/sites-available/000-default.conf
<pre><VirtualHost *:80></pre>
<pre><VirtualHost *:80></pre>


Copy the configuration file for the first virtual host<br>
Copy the configuration file for the first virtual host
<pre>sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf</pre>
<pre>sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf</pre>


Change the content to the following<br>
Change the content to the following
<pre>sudo nano /etc/apache2/sites-available/example.com.conf</pre>
<pre>sudo nano /etc/apache2/sites-available/example.com.conf</pre>
[[File:SumCodeForLinux3.jpg|center left]]


<pre>
<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>
</pre>


Copy this file to the virtual host directory<br>
Copy this file to the virtual host directory
<pre>sudo cp /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-available/test.com.conf</pre>
<pre>sudo cp /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-available/test.com.conf</pre>


And change its contents<br>
And change its contents
<pre>sudo nano /etc/apache2/sites-available/test.com.conf</pre>
<pre>sudo nano /etc/apache2/sites-available/test.com.conf</pre>
[[File:SumCodeForLinux4.jpg|center left]]


<pre>
<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<br>
Activate the virtual hosts
<pre>sudo a2ensite example.com.conf</pre>
<pre>sudo a2ensite example.com.conf</pre>


<pre>
<pre>
cd /etc/apache2/sites-enabled<br>
cd /etc/apache2/sites-enabled
sudo ln -s ../sites-available/test.com.conf
sudo ln -s ../sites-available/test.com.conf
</pre>
</pre>
Line 99: Line 132:
Check the configuration and restart
Check the configuration and restart
<pre>
<pre>
apachectl configtest<br>
apachectl configtest
sudo service apache2 restart
sudo service apache2 restart
</pre>
</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>
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>
sudo nano /etc/hosts
sudo nano /etc/hosts
</pre>


Add your IP number

<pre>
⚫
192.168.1.136 example.com<br>
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<br>
C:\Windows\System32\drivers\etc\hosts
C:\Windows\System32\drivers\etc\hosts


Check the health of virtual servers by name<br>
Check the health of virtual servers by name on Windows


http://example.com<br>
http://example.com , http://test.com , http://192.168.1.136
http://test.com<br>


or by using curl in the Linux terminal or Windows browser<br>
or by using curl in the Linux terminal
<pre>
<pre>
curl -v http://example.com<br>
curl -v http://example.com
curl -v http://test.com<br>
curl -v http://test.com
curl -v http://192.168.1.136
curl -v http://192.168.1.136
</pre>
</pre>


Installation of nginx
=====Installation of Nginx=====


Stop the Apache server since since it's not possible to keep 2 servers on the same port<br>
Stop the Apache server since since it's not possible to keep 2 servers on the same port
<pre>sudo service apache2 stop</pre>
<pre>sudo service apache2 stop</pre>


Install nginx<br>
Install nginx
<pre>sudo apt-get install nginx</pre>
<pre>sudo apt-get install nginx</pre>


Go to catalogue<br>
Go to catalogue
<pre>cd /etc/nginx/sites-available</pre>
<pre>cd /etc/nginx/sites-available</pre>


Copy the default configuration file<br>
Copy the default configuration file
<pre>sudo cp default nginx</pre>
<pre>sudo cp default nginx</pre>


Edit the config file<br>
Edit the config file
<pre>sudo nano nginx</pre>
<pre>sudo nano nginx</pre>


The contents should look like this<br>
The contents should look like this

[[File:SumCodeForLinux5.jpg|center left]]
[[File:SumCodeForLinux5.jpg|center left]]
<br>


Create html file with any content, such as «hello Nginx»<br>
Create html file with any content, such as «hello Nginx»
<pre>sudo nano /var/www/html/nginx.html</pre>
<pre>sudo nano /var/www/html/nginx.html</pre>


Go to the site activation directory<br>
Go to the site activation directory
<pre>cd /etc/nginx/sites-enabled/</pre>
<pre>cd /etc/nginx/sites-enabled/</pre>


Remove the link to default site<br>
Remove the link to default site
<pre>sudo rm default</pre>
<pre>sudo rm default</pre>


Add the link to the new site<br>
Add the link to the new site
<pre>sudo ln -s /etc/nginx/sites-available/nginx</pre>
<pre>sudo ln -s /etc/nginx/sites-available/nginx</pre>


Check nginx syntax<br>
Check nginx syntax
<pre>nginx -t</pre>
<pre>nginx -t</pre>


Restart nginx<br>
Restart nginx
<pre>sudo service nginx restart</pre>
<pre>sudo service nginx restart</pre>


Check<br>
Check
<pre>curl -v http://192.168.1.136</pre>
<pre>curl -v http://192.168.1.136</pre>


Line 172: Line 208:
<pre>sudo nano /etc/apache2/ports.conf</pre>
<pre>sudo nano /etc/apache2/ports.conf</pre>


<nowiki>#Listen 80</nowiki><br>
<nowiki>#Listen 80 </nowiki>
⚫
Listen 127.0.0.1:81


⚫
<nowiki>Listen 127.0.0.1:81 </nowiki>
⚫
Make sure that all the configuration files have Apache<br>

Save changes

⚫
Make sure that all Apache's configuration files have
<VirtualHost *>
<VirtualHost *>


Restart nginx<br>
Restart nginx
<pre>sudo systemctl restart nginx</pre>
<pre>sudo systemctl restart nginx</pre>


Initiate apache<br>
Initiate apache
<pre>sudo systemctl start apache2</pre>
<pre>sudo systemctl start apache2</pre>


We can make sure that each port is listening<br>
We can make sure that each port is listening
<pre>sudo netstat -nlpt | grep :8[01]</pre>
<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<br>
Check whether Apache works through 127.0.0.1 with curl
<pre>
<pre>
curl http://127.0.0.1:81<br>
curl http://127.0.0.1:81
curl http://127.0.0.1:81/index.php<br>
curl http://127.0.0.1:81/index.php
curl -H 'Host: example.com' http://127.0.0.1:81<br>
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>
</pre>
Line 203: Line 241:


[[File:SumCodeForLinux7.jpg|center left]]
[[File:SumCodeForLinux7.jpg|center left]]
<br>


Restart nginx<br>
Restart nginx
<pre>sudo systemctl restart nginx</pre>
<pre>sudo systemctl restart nginx</pre>


Listen to port 81 on a local interface<br>
Listen to port 81 on a local interface
<pre>
<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>
</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<br>
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>

⚫
Add these lines to the config nginx file before location /
<pre>sudo nano /etc/nginx/sites-available/nginx</pre>
<pre>sudo nano /etc/nginx/sites-available/nginx</pre>


[[File:SumCodeForLinux8.jpg|center left]]
[[File:SumCodeForLinux8.jpg|center left]]
<br>


<pre>sudo systemctl restart nginx</pre>
<pre>sudo systemctl restart nginx</pre>


Listen to port 80 on interface c<br>
Listen to port 80 on interface
<pre>sudo ngrep -W byline -d enp0s8 "" port 80</pre>
<pre>sudo ngrep -W byline -d enp0s8 "" port 80</pre>


At the same time, on a different machine, try opening the html file 192.168.1.136/nginx.html<br>
At the same time, on a different machine, try opening the html file

⚫
Now all the requests with an html file in the address will be handled by nginx<br>
http://192.168.1.136/nginx.html
⚫
Server: nginx/1.10.0 (Ubuntu).

⚫
Now all the requests with an html file in the address will be handled by nginx

⚫
''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

center left

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]

center left

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

center left

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

center left

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).