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
Line 77: Line 77:


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

<br>


Copy this file to the virtual host directory<br>
Copy this file to the virtual host directory<br>
sudo cp /etc/apache2/sites-available/example.com.conf /etc/apache2/sites-available/test.com.conf
<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<br>
sudo nano /etc/apache2/sites-available/test.com.conf<br>
<pre>sudo nano /etc/apache2/sites-available/test.com.conf</pre>
[[File:SumCodeForLinux4.jpg|center left]]
[[File:SumCodeForLinux4.jpg|center left]]

<br>


Activate the virtual hosts<br>
Activate the virtual hosts<br>
sudo a2ensite example.com.conf
<pre>sudo a2ensite example.com.conf</pre>


<pre>
cd /etc/apache2/sites-enabled<br>
cd /etc/apache2/sites-enabled<br>
sudo ln -s ../sites-available/test.com.conf
sudo ln -s ../sites-available/test.com.conf
</pre>


Check the configuration and restart<br>
Check the configuration and restart
<pre>
apachectl configtest<br>
apachectl configtest<br>
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<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<br>
sudo nano /etc/hosts
sudo nano /etc/hosts



192.168.1.136 example.com<br>
192.168.1.136 example.com<br>
192.168.1.136 test.com
192.168.1.136 test.com



If we want to check this on a Windows machine, add these lines to the file<br>
If we want to check this on a Windows machine, add these lines to the file<br>
Line 109: Line 115:


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

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


or by using curl in the Linux terminal or Windows browser<br>
or by using curl in the Linux terminal or Windows browser<br>
<pre>
curl -v http://example.com<br>
curl -v http://example.com<br>
curl -v http://test.com<br>
curl -v http://test.com<br>
curl -v http://192.168.1.136
curl -v http://192.168.1.136
</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<br>
sudo service apache2 stop
<pre>sudo service apache2 stop</pre>


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


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


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


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


The contents should look like this<br>
The contents should look like this<br>
Line 139: Line 148:


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


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


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


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


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


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


Check<br>
Check<br>
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><br>
<nowiki>#Listen 80</nowiki><br>
Line 170: Line 179:


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


Initiate apache<br>
Initiate apache<br>
sudo systemctl start apache2
<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<br>
sudo netstat -nlpt | grep :8[01]
<pre>sudo netstat -nlpt | grep :8[01]</pre>


[[File:SumCodeForLinux6.jpg|center left]]
[[File:SumCodeForLinux6.jpg|center left]]
Line 182: Line 191:


Check whether Apache works through 127.0.0.1 with curl<br>
Check whether Apache works through 127.0.0.1 with curl<br>
<pre>
curl http://127.0.0.1:81<br>
curl http://127.0.0.1:81<br>
curl http://127.0.0.1:81/index.php<br>
curl http://127.0.0.1:81/index.php<br>
curl -H 'Host: example.com' http://127.0.0.1:81<br>
curl -H 'Host: example.com' http://127.0.0.1:81<br>
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]]
Line 195: Line 206:


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


Listen to port 81 on a local interface<br>
Listen to port 81 on a local interface<br>
<pre>
sudo apt-get install ngrep<br>
sudo apt-get install ngrep<br>
sudo ngrep -W byline -d lo "" port 81<br>
sudo ngrep -W byline -d lo "" port 81<br>
</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


Line 206: Line 219:


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


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


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


Listen to port 80 on interface c<br>
Listen to port 80 on interface c<br>
sudo ngrep -W byline -d enp0s8 "" port 80
<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 192.168.1.136/nginx.html<br>

Revision as of 21:14, 30 October 2022

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<br>
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<br>
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:
center left


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":
center left

Save

Create file /var/www/html/index.php

sudo nano /var/www/html/index.php

with the following contents

<?php<br>
phpinfo();<br>
?>

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

center left


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

center left


Activate the virtual hosts

sudo a2ensite example.com.conf
cd /etc/apache2/sites-enabled<br>
sudo ln -s ../sites-available/test.com.conf

Check the configuration and restart

apachectl configtest<br>
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<br>
curl -v http://test.com<br>
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

Make sure that all the configuration files have Apache
<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<br>
curl http://127.0.0.1:81/index.php<br>
curl -H 'Host: example.com' http://127.0.0.1:81<br>
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<br>
sudo ngrep -W byline -d lo "" port 81<br>

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