Linux servers ICT4TN021 – H7

In this post I’m reporting Tero Karvinen’s “Linux Palvelimet” -courses assignment 7.[1] This is the last assignment of the course and I’m going to redo everything I’ve learnt so far. Assignment started 08.10.2018 @ 18:89.

What am I working with?

  • Operating system: Xubuntu-18.04.1 64b[2] (Bootable USB)
  • CPU: Intel I5-6600k @ 3.5GHz
  • GPU: Geforce GTX 1070, 8Gb VRAM
  • RAM: 2x 8Gb DDR4 @ 2667MHz
  • MoBo: Asus Maximus VIII Ranger (boot menu F8)

 

Assignment A)

Rehearsal started @ 18:28 08.10.2018

Solve any old lab rehearsal of your choosing from this course.[1]

I wanted to choose a rehearsal that covers a lot of ground that I’m not too comfortable with and I ended up choosing the latest rehearsal from Spring 2018.[3]

LAB-REHEARSAL[3]

I started by booting the computer using a bootable USB stick. Once my computer had opened I switched my keyboard layout to the Finnish one in terminal by typing:

setxkbmap fi

I also updated the packet repository with:

sudo apt-get update

I opened SSH(22) and HTTP(80) ports and enabled the firewall:

sudo ufw allow 80/tcp
sudo ufw allow 22/tcp
sudo ufw enable

Install a web stack of your choosing. Make an example program using it

I decided to install LAMP (Linux, Apache, MariaDB(or MySQL) and PHP).

I started by installing Apache:

sudo apt-get install -y apache2

Then I rewrote localhost with:

echo hello | sudo tee /var/www/html/index.html 

I enabled user directories and restarted apache2 with:

sudo a2enmod userdir
sudo systemctl restart apache2

I created a directory called public_html under /home/xubuntu and a file called index.html in it with:

mkdir public_html
cd public_html/
nano index.html

I wrote “Hellooo” inside the index.html file and refreshed my localhost page with shift + refresh(top left corner):

Screenshot_2018-10-08_15-51-18screenshot_2018-10-08_15-54-03.png

Time to install PHP:

sudo apt-get install -y libapache2-mod-php7.2

I renamed my index.html into index.php:

mv index.html index.php

and edited it:

Helloo

<?php

print(2*2);

?>

It didn’t post anything so I need to look what’s wrong:

cd /etc/apache2
grep -ir php

Grep gave me the following information and like the 3rd last line says, I need to re-enable PHP by commenting out some lines.

sites-available/default-ssl.conf:		<FilesMatch "\.(cgi|shtml|phtml|php)$">
mods-available/php7.2.load:# Conflicts: php5
mods-available/php7.2.load:LoadModule php7_module /usr/lib/apache2/modules/libphp7.2.so
mods-available/php7.2.conf:    SetHandler application/x-httpd-php
mods-available/php7.2.conf:<FilesMatch ".+\.phps$">
mods-available/php7.2.conf:    SetHandler application/x-httpd-php-source
mods-available/php7.2.conf:    # Deny access to raw php sources by default
mods-available/php7.2.conf:# Deny access to files without filename (e.g. '.php')
mods-available/php7.2.conf:# Running PHP scripts in user directories is disabled by default
mods-available/php7.2.conf:# To re-enable PHP in user directories comment the following lines
mods-available/php7.2.conf:        php_admin_flag engine Off
mods-available/dir.conf:	DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.html

So I opened mods-available/php7.2.conf with sudoedit:

sudoedit mods-available/php7.2.conf

And I commented them out by putting # infront of every line starting from <Ifmoduele to </Ifmodule>.

screenshot_2018-10-08_16-04-30.png

Now I only need to restart Apache to see if PHP is working or not:

sudo systemctl restart apache2

Screenshot_2018-10-08_16-07-17

Last but not least I have to download MariaDB since MySQL doesn’t work well on Xubuntu 18.04.1, but they are basicly the same programs. Used Tero’s Guide here.[4]

I installed mariadb-client and mariadb-server:

sudo apt-get -y install mariadb-client mariadb-server

Logged in as root in to MariaDB:

sudo mariadb

Created a database:

CREATE DATABASE cars CHARACTER SET utf8;

Created an account that uses the specific database:

GRANT ALL ON cars.* TO cars@localhost IDENTIFIED BY 'XNg`$KdU`JT0-1I';

And configured mariadb to run from the account i had just created:

nano .my.cnf

I wrote this inside the .my.cnf:

[CLIENT]
user=cars
database=cars
password='XNg`$KdU`JT0-1I'

Now i should be able to open MariaDB by typing mariadb in terminal.

I created a table called cars and set 3 fields in it:

CREATE TABLE cars (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), time TIMESTAMP);
DESCRIBE cars;
+-------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type         | Null | Key | Default           | Extra                       |
+-------+--------------+------+-----+-------------------+-----------------------------+
| id    | int(11)      | NO   | PRI | NULL              | auto_increment              |
| name  | varchar(255) | YES  |     | NULL              |                             |
| time  | timestamp    | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------+--------------+------+-----+-------------------+-----------------------------+

Inserted some information into my database:

MariaDB [cars]> INSERT INTO cars(name) VALUES ('Mustang');
MariaDB [cars]> INSERT INTO cars(name) VALUES ('Nissan Skyline GTR');
MariaDB [cars]> INSERT INTO cars(name) VALUES ('Mazda RX7');
MariaDB [cars]> SELECT * FROM cars;
+----+--------------------+---------------------+
| id | name               | time                |
+----+--------------------+---------------------+
|  1 | Mustang            | 2018-10-08 16:40:48 |
|  2 | Nissan Skyline GTR | 2018-10-08 16:41:13 |
|  3 | Mazda RX7          | 2018-10-08 16:41:31 |
+----+--------------------+---------------------+

Time to make it work through localhost:

First I edited index.php by giving it a working script that uses mariadb:

screenshot_2018-10-08_16-47-12.png

(script by Tero Karvinen).[4]

Didn’t work, since I hadn’t installed the module that let’s mariadb and php work together:

sudo apt-get -y install php-mysql 
sudo systemctl restart apache2

Screenshot_2018-10-08_16-49-23

Now to add some users to server.

I started by creating a directory and file under /etc/skel which are going to be copied to every user that I’m going to create.

sudo mkdir /etc/skel/public_html
sudo nano /etc/skel/public_html/index.html

I created users called

Jorma Mähkylä, Pekka Hurme, Ronaldo Smith, Håkan
Petersson, Einari Mikkonen, Einari Vähäkäähkä, Eija Vähäkäähkä. 

by:

sudo adduser jmahkyla
sudo adduser phurme
sudo adduser rsmith
sudo adduser hpeterss
sudo adduser emikkone
sudo adduser evahakaa
sudo adduser eijavaha

I generated passwords for the users using pwgen:

sudo apt-get -y install pwgen

I tested the users directories with http://192.168.10.50/~”user”/ and tried that every single one worked.

Creating a command called ipos that shows the computers ip address. Command needs to work on all users.

I started by creating a BASH file:

nano ipos.sh 

inside ipos.sh I wrote

hostname -I

and ran the file by typing:

bash ipos.sh

which printed out my ip 192.168.10.50.

Now to make the ipos.sh script public for every user:

I started by putting a sha-bang in the script:

#!/bin/bash

hostname -I

Gave everyone rights to execute the script:

chmod a+x ipos.sh

Then i renamed the file to ipos:

mv ipos.sh ipos

copied it to /usr/local/bin with:

sudo cp ipos /usr/local/bin/

Tested the file using another user:

sudo apt-get -y install ssh
ssh phurme@localhost
phurme@xubuntu:~$ ipos
192.168.10.50 

 

Next step is to create Hello world for Pekka using Python, BASH and Nodejs.

I’m not sure which is the right way to do this, to create it using my sudo user credentials, in my own home folder and moving it to Pekka’s folder; or to create it inside Pekka’s folder from the get go.

I used SSH to get on Pekka’s user and created a directory called Scripts under /home/phurme:

mkdir scripts
cd scripts/
nano hello.py

I wrote:

print ("Hello world!")

inside the Hello.py script.

phurme@xubuntu:~/scripts$ python3 hello.py 
Hello world!

Next up was to create a BASH script.

I created a bash file:

nano hello.sh

wrote the following inside the hello.sh file:

echo Hello world!

And it printed out the following in console, once I gave the command:

phurme@xubuntu:~/scripts$ bash hello.sh
Hello world!

Last Hello world is going to be java. I used Tero’s guide[5] for this part.

I needed to install Java:

sudo apt-get -y install default-jre default-jdk

Then I created a script called hello.java:

nano hello.java

and wrote the following inside:

public class hello
{
 public static void main(String[] args)
 {
 System.out.println("Hello world!");
 }
}

(Teros script).[5]

I compiled the script:

javac hello.java

and executed it:

phurme@xubuntu:~/scripts$ java hello 
Hello world!

 

Put the example program(LAMP) to show from your apache frontpage.

I followed Tero’s guide[6] here since I couldn’t quite remember what to do:

I started by creating a new .conf file by:

sudoedit /etc/apache2/sites-available/thomas.conf

Editing the following in it:

## /etc/apache2/sites-enabled/thomas.conf
<VirtualHost *:80>
 DocumentRoot /home/xubuntu/public_html/

 <Directory /home/xubuntu/public_html/>
   Require all granted
 </Directory>
</VirtualHost>

Enabling my newly created default page:

sudo a2ensite thomas.conf

and disabling the original default page:

sudo a2dissite 000-default.conf

And by reloading my apache:

sudo systemctl reload apache2.service

screenshot_2018-10-08_18-17-38.png

Two(2) sites under One(1) IP.

I had never done this part before, so it was super nice to try it out since it seems that it has been in multiple lab rehearsals before. I followed Tero’s guide here.[7]

I started by creating a new conf:

sudoedit /etc/apache2/sites-available/thomas.fun.conf

And pasting the following inside it:

<VirtualHost *:80>
 ServerName thomas.fun.com
 ServerAlias www.thomas.fun.com

 DocumentRoot /home/xubuntu/publicsites/thomas.fun.com

 <Directory /home/xubuntu/publicsites/thomas.fun.com>
   Require all granted
 </Directory>
</VirtualHost>

I enabled the site:

sudo a2ensite thomas.fun.conf

And reloaded apache2:

sudo systemctl reload apache2.service

Created a web page using a normal user:

mkdir -p /home/xubuntu/publicsites/thomas.fun.com/

Created a new index.html site that said thomas:

echo thomas > /home/xubuntu/publicsites/thomas.fun.com/index.html

Installed curl:

sudo apt-get install curl

Curled the site:

$ curl -H 'Host: thomas.fun.com' localhost
thomas

And curled my localhost site:

$ curl localhost
Helloo

<p>1 Mustang</p>
<p>2 Nissan Skyline GTR</p>
<p>3 Mazda RX7</p>

Edited /etc/hosts file:

$ sudoedit /etc/hosts

Wrote the last line inside (127.0.0.1 thomas.fun.conf):

127.0.0.1 localhost
127.0.1.1 xubuntu
127.0.0.1 thomas.fun.com

# The following lines are desirable for IPv6 capable hosts

and saved.

Now my ip had 2 different addresses under the same ip.

Screenshot_2018-10-08_18-41-25

Lab-rehearsal finished, since I couldn’t do the parts that required me to check logs etc.

Rehearsal finished @ 21:56. This is the last given assignment for the course and all I have left is the real lab-rehearsal on Thursday 11.08.2018. I am definitely going to use Linux outside of this course and in the next course that is going to start right after this one.

SOURCES:

[1] Course and it’s assignments: http://terokarvinen.com/2018/aikataulu-%E2%80%93-linux-palvelimet-ict4tn021-3003-to-8-14-alkusyksy-2018p1-%E2%80%93-5-op

[2] Operating system (Xubuntu 18.04.1 64b): http://ftp.lysator.liu.se/ubuntu-dvd/xubuntu/releases/18.04/release/

[3] Old lab-rehearsal: http://terokarvinen.com/2018/arvioitava-laboratorioharjoitus-linux-palvelimet-ict4tn021-8-maanantai-%e2%80%93-alkukevat-2018-%e2%80%93-5-op

[4] MySQL database creation with php: http://terokarvinen.com/2016/read-mysql-database-with-php-php-pdo

[5] Javascript: http://terokarvinen.com/2018/hello-python3-bash-c-c-go-lua-ruby-java-programming-languages-on-ubuntu-18-04

[6] Userdir page as default page: http://terokarvinen.com/2016/new-default-website-with-apache2-show-your-homepage-at-top-of-example-com-no-tilde

[7] Multiple sites in one IP: http://terokarvinen.com/2018/name-based-virtual-hosts-on-apache-multiple-websites-to-single-ip-address

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s