Instructions to set up a basic LAMP+SSH server environment
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
6.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. Linux servers - Exercice 6
  2. ==============
  3. *Disclaimer:*
  4. --------------
  5. This exercise is a part of [Linux servers (ICT4TN021, spring 2018) // Linux-palvelimet (ICT4TN021, kevät 2018)](http://www.haaga-helia.fi/fi/opinto-opas/opintojaksokuvaukset/ICT4TN021) school course organized as a part of Information Technology studies in Haaga-Helia university of Applied Sciences, Helsinki, Finland. Course lecturer [Tero Karvinen](http://terokarvinen.com/) has defined the original assignment descriptions in Finnish presented in this document in English. Answers and translations have been written by Pekka Helenius (me, ~ Fincer).
  6. *Table of contents:*
  7. --------------
  8. [a) Write and execute "Hello world" in three code languages of your choice. Set up a necessary development environments.](https://github.com/Fincer/linux_server_setup/blob/master/exercises/h6.md#a-write-and-execute-hello-world-in-three-code-languages-of-your-choice-set-up-a-necessary-development-environments)
  9. - [Perl (hello.pl)](https://github.com/Fincer/linux_server_setup/blob/master/exercises/h6.md#perl-hellopl)
  10. - [Python 3 (hello.py)](https://github.com/Fincer/linux_server_setup/blob/master/exercises/h6.md#python-3-hellopy)
  11. - [C (hello.c)](https://github.com/Fincer/linux_server_setup/blob/master/exercises/h6.md#c-helloc)
  12. - [b) (optional) Write a simple program for some practical purpose with each language. Ideas: utilize the key benefits of your language of choice. Take an input value from a user, generate a calculation and print the output (input-processing-layout).](https://github.com/Fincer/linux_server_setup/blob/master/exercises/h6.md#b-optional-write-a-simple-program-for-some-practical-purpose-with-each-language-ideas-utilize-the-key-benefits-of-your-language-of-choice-take-an-input-value-from-a-user-generate-a-calculation-and-print-the-output-input-processing-layout)
  13. --------------
  14. **a)** Write and execute "Hello world" in three code languages of your choice. Set up a necessary development environments.
  15. --------------
  16. **Answer:**
  17. Let's write "Hello World" in the following three languages: `perl`, `python 3` and `C`. We need to install required development tools:
  18. ```
  19. sudo apt-get -y install perl python gcc
  20. ```
  21. (GCC = [Gnu Compiler Collection](https://gcc.gnu.org/) + [Wikipedia](https://en.wikipedia.org/wiki/GNU_Compiler_Collection))
  22. Let's write the codes into a subfolder `hello-world` in user's home dir:
  23. ```
  24. [newuser@goauldhost: ~ ]$ mkdir ~/hello-world
  25. [newuser@goauldhost: ~ ]$ cd hello-world/
  26. [newuser@goauldhost: hello-world ]$ touch hello{.c,.py,.pl}
  27. ```
  28. - `hello.py` = "Hello World" written in Python 3
  29. - `hello.pl` = "Hello World" written in Perl
  30. - `hello.c` = "Hello World" written in C
  31. Hello World program written in three languages (nano editor used):
  32. ### Perl (hello.pl)
  33. ```
  34. #!/usr/bin/env perl
  35. # Declare runtime environment above
  36. # Include these to find common warnings, syntax errors etc in the code
  37. import strict;
  38. import warnings;
  39. # Print Hello World! with a newline
  40. print "Hello World!\n";
  41. ```
  42. ### Python 3 (hello.py)
  43. ```
  44. #!/usr/bin/env python3
  45. # Declare runtime environment above
  46. # Print Hello World!
  47. print("Hello World!");
  48. ```
  49. **NOTE!** Pay attention when referring to python executable. On some Linux distributions, python still refers to `python2`, and on some, python refers to `python3`. Practices differ. It can be safer to use 'python3' or 'python2' instead of just 'python' if not sure.
  50. In a simple program like "Hello world" this python issue doesn't really matter. However, if any python libraries are imported into the code, you must know which python environment to use, Python 2 or Python 3.
  51. ### C (hello.c)
  52. ```
  53. // Include Standard Input Output Library
  54. #include <stdio.h>
  55. // Declare main function for the program. int type for returning a integer
  56. int main()
  57. {
  58. // Print Hello World! (stdout in CLI)
  59. printf("Hello World!");
  60. // Return boolean value "true" to the execution environment
  61. return 0;
  62. }
  63. ```
  64. `hello.c` requires compiling from source code to executable binary file. Therefore, we compile the source code with the following command in `hello-world` folder:
  65. ```
  66. gcc -o hello hello.c
  67. ```
  68. after which our `hello` binary can be executed with
  69. ```
  70. ~/hello-world/hello
  71. ```
  72. **NOTE:** You can check file details by issuing the following command:
  73. ```
  74. file ~/hello-world/hello
  75. hello: ELF 64-bit LSB pie executable x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=8e76492bc5ce6c65df8ec5ce7be42645fae2ab70, not stripped
  76. ```
  77. All programs give output string "Hello World!" (stdout data stream) in our shell environment:
  78. ```
  79. [newuser@goauldhost: hello-world ]$ pwd
  80. /home/newuser/hello-world
  81. [newuser@goauldhost: ~ ]$ python hello.py
  82. Hello World!
  83. [newuser@goauldhost: ~ ]$ perl hello.pl
  84. Hello World!
  85. [newuser@goauldhost: ~ ]$ ./hello
  86. Hello World!
  87. ```
  88. **b)** (optional) Write a simple program for some practical purpose with each language. Ideas: utilize the key benefits of your language of choice. Take an input value from a user, generate a calculation and print the output (input-processing-layout).
  89. --------------
  90. **Answer:**
  91. This could have been very interesting assignment to work out. Unfortunately, my working laptop was broken so that I was not effectively able to write new code. I have worked with a perl program which generated random port and TCP/UDP sequences for knockd daemon. In addition, I have worked with some updates to Wine Configuration window (winecfg, part of Wine program) in C language:
  92. ![winecfg-update](https://i.imgur.com/SihmPUA.jpg)
  93. See: [Fincer/winecfg_patch](https://github.com/Fincer/winecfg_patch)
  94. Some Python and bash based program-related scripts and code updates are available here:
  95. - [PlayOnLinux patches](https://github.com/Fincer/linux-patches-and-scripts/tree/master/playonlinux).
  96. - Little CMake script targeted for compiling C++ code can be found [here](https://github.com/Fincer/linux-patches-and-scripts/tree/master/xclipshow)
  97. - Some basic scripting to convert videos with ffmpeg in KDE DE: [ffmpeg-fileconversion-video](https://github.com/Fincer/linux-patches-and-scripts/blob/master/kde-servicemenus-multimediatools/ffmpeg-fileconversion-video.sh)
  98. etc.