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
 
(20 intermediate revisions by the same user not shown)
Line 43: Line 43:


Write the following:
Write the following:
<pre>
<html>
<head>
<title>Welcome to Example.com!</title>
</head> <body>
<h1>Success! The example.com virtual host is working!</h1> </body>
</html>
</pre>


[[File:SumCodeForLinux1.jpg|center left]]


Save and copy this file
Save and copy this file
Line 54: Line 61:
Change "example" to "test":
Change "example" to "test":


<pre>
[[File:SumCodeForLinux2.jpg|center left]]
<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 80: Line 94:
<pre>sudo nano /etc/apache2/sites-available/example.com.conf</pre>
<pre>sudo nano /etc/apache2/sites-available/example.com.conf</pre>


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


<pre>
[[File:SumCodeForLinux4.jpg|center left]]
<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
Activate the virtual hosts
Line 105: Line 137:


Since we don't have a DNS server installed, write the IP address and names of our virtual servers manually in file /etc/hosts
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
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
http://example.com , http://test.com , http://192.168.1.136
http://test.com


or by using curl in the Linux terminal or Windows browser
or by using curl in the Linux terminal
<pre>
<pre>
curl -v http://example.com
curl -v http://example.com
Line 125: Line 162:
</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
Stop the Apache server since since it's not possible to keep 2 servers on the same port
Line 171: Line 208:
<pre>sudo nano /etc/apache2/ports.conf</pre>
<pre>sudo nano /etc/apache2/ports.conf</pre>


<nowiki>#Listen 80</nowiki>
<nowiki>#Listen 80 </nowiki>

Listen 127.0.0.1:81
<nowiki>Listen 127.0.0.1:81 </nowiki>

Save changes


Make sure that all Apache's configuration files have
Make sure that all Apache's configuration files have
Line 220: Line 260:


[[File:SumCodeForLinux8.jpg|center left]]
[[File:SumCodeForLinux8.jpg|center left]]



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


Listen to port 80 on interface c
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
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
Now all the requests with an html file in the address will be handled by nginx



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