Skip to content
Petification Develop Blog

Chapter 5. Setting Develpment Environment

HaeramKim, BuildYourOwnIoTPlatform1 min read

NOTE: this notes are from “Build Your Own IoT Platform” by Anand Tamboli. And I can’t speak english very well, so some sentences or word might be inappropriate and might have some misunderstandings.

Settings that is best for us

Cloud instance

  • Register & make instance @ DigitalOcean
    • Ubuntu(18.04 x64)
    • Basic plan - 4GB RAM, 50GB Disk, Dual-core(Regular Intel)
    • NY Data-storage region
    • Authenticate with password
    • IPv6, Monitoring, User data option enable

Firewall settings

  • Initiate firewall via “Uncomplicated Firewall”
    • “Uncomplicated Firewall” is installed in Ubuntu by default
    • Here is some “Uncomplicated Firewall” command that helps us to manage firewall.
    • ufw app list: Show all available applications.
    • ufw allow <application_name>: Allow that application.
    • ufw enable: Run firewall.
    • ufw status: Show firewall status.
    • ufw app info "<application_name>": Show information of that application.
    • ufw allow in "<application_name>": It also allows that application. I don’t know the difference between using and not using “in” command.
  • Allow OpenSSH from firewall

Apache2 server

  • Install Apache2
1apt update
2apt install apache2
  • Allow “Apache Full” from firewall
1ufw allow in "Apache Full"
  • Apache uses port 80 for HTTP and port 433 for HTTPS
  • If everything’s gonna be fine, apache ubuntu default page must be shown when you enter your IP address on your browser.

MySQL

  • Install MySQL installer
1apt install mysql-server
  • Install MySQL with secure version
1mysql_secure_installation
1* This will prompt additional configuration console.
2* You have to select `0(LOW)` for `password validation policy`.
  • Setting administration account for MySQL
    • This is how to enter MySQL Shell:
1mysql
1* Query for showing all root account:
1SELECT user,authentication_string,plugin,host FROM mysql.user WHERE user="root";
1* Query for modifying password for root account:
1ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$YOUR_PASSWORD';
2FLUSH PRIVILEGES;
1* And this is expected result when you type show-root query:
1| user | authentication_string | plugin | host |
2| root | *A0AF1999141933B3B4C7
3AE72544AB01849669F98 | mysql_native_password| localhost |

PHP

  • Installing PHP
1apt install php libapache2-mod-php php-mysql
  • Configuring PHP
    1. Open configuring file that is exists in /etc/apache2/mods-enabled/dir.conf
    2. Edit that file to:
1<IfModule mod_dir.c>
2DirectoryIndex index.php index.html index.htm index.cgi index.pl index.xhtml
3</IfModule>
13. Restart apache2 server
1systemctl restart apache2
14. Test whether the server works correctly:
1vi /var/www/html/test.php
2# And add php script below to it
3<?php
4 echo "php ok";
5?>

PHPMyAdmin

  • This is a tool that allows developers to access DB.
  • Install PHPMyAdmin:
1apt update
2apt install phpmyadmin php-mbstring php-gettext
1* NOTE: php-gettext is not stable in Ubuntu 20.04. This is why you have to choose Ubuntu 18.04 when creating droplet.
2* While Installing PHPMyAdmin, several configuration console might be prompted. Here is recommened selection:
1# Configure database for phpmyadmin with dbconfig-common
2Yes
3
4# What kind of server do you plan to use
5Apache2 # You have to tap spacebar to select this opiton.
6
7# Communication
8Unix socket
9
10# Set database name, username, password on your own
11# Some errors might be occured when you select password policy to MEDIUM(1) or HIGH(2). That's why you have to choose LOW(0) when you select policies.
  • Configure plugin && restart server
1phpenmod mbstring
2systemctl restart apache2
  • Securing PHPMySQL
    • We’re gonna use htaccessfeature of apache to securing it.
1# Open /etc/apache2/conf-available/phpmyadmin.conf and modify to:
2Alias /phpmyadmin /usr/share/phpmyadmin
3<Directory /usr/share/phpmyadmin>
4 Options SymLinksIfOwnerMatch
5 DirectoryIndex index.php
6 AllowOverride All
7.....
8.....
1* Add following contents to the file
1# Open /usr/share/phpmyadmin/.htaccess
2AuthType Basic
3AuthName "Restricted Files"
4AuthUserFile /etc/phpmyadmin/.htpasswd
5Require valid-user
1* Set admin password
1htpasswd -c /etc/phpmyadmin/.htpasswd $USERNAME
2# And then entering password line will be prompted

DNS

  • Use noip.com to register custom free domain
  • Set destination IP as droplet instance uses.
  • Register domain to DigitalRecord and create new record with Type A.

Virtual Host

  • I don’t know what exactly virtual host means, but i think when one server hosts more than two hosts, like www.example1.com and www.example2.com…, It is called virtual host
1mkdir -p /var/www/<your-domain>/html
2chown -R $USER:$USER /var/www/<your-domain>/html
3chmod -R 755 /var/www/<your-domain>
  • Make main index file
1# Make index.php in /var/www/<your-domain>/html/
2# And then add following contents to the file:
3<?php
4 echo("Hi...this is our webpage with domain name !");
5?>
  • Make virtual host file
1# Make <your-domain>.conf file in /etc/apache2/sites-available/
2# And then add following contents:
3<VirtualHost *:80>
4 ServerAdmin admin@<your-domain>
5
6 ServerName <your-domain>
7 ServerAlias www.<your-domain>
8
9 DocumentRoot /var/www/<your-domain>/html
10
11 ErrorLog ${APACHE_LOG_DIR}/error.log
12 CustomLog ${APACHE_LOG_DIR}/access.log combined
13</VirtualHost>
1* And then enable virtual host:
1a2ensite <your-domain>.conf
2a2dissite 000-default.conf
3
4# Typing command below must prints "Syntax OK"
5apache2ctl configtest
6
7# Restart server
8systemctl restart apache2

SSL Certificates

  • Install SSL Certificates via Let’s Encrypt and Certbot.
1add-apt-repository ppa:certbot/certbot
2apt install python-certbot-apache
  • Certificate our site
1# When you registered www-prefixed domain
2# like "www.example.com", add "www.<your-domain>" too.
3certbot --apache -d <your-domain>
1* After this command runs, you have to enter email address for notification and agree with policy stuff.
2* And also, you have to select "redirect" option when selection console is prompted.
3* When messages saying "Congratulations!" is printed, it's done.
4* You can verify the Certificate with `http://www.ssllabs.com/ssltest/analyze.html?d=<your-domain>` page.

NodeJS & NodeRED

  • To install NodeJS to Ubuntu, you have to register NodeJS official repository to apt first.
1# Register repository
2curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
3
4# Install NodeJS & NPM
5apt install nodejs
6
7# Verify installation (Check version)
8nodejs -v
9npm -v
  • Install NodeRED
1npm install -g --unsafe-perm node-red
  • Allow NodeRED from firewall. NodeRED uses port 1880.
1ufw allow 1880/tcp
  • Configure NodeRED Setting
    • You can get settings.js file from Here
    • path for old settings.js file is /root/.node-red/settings.js. You have to overwrite this file.
  • Securing NodeRED
    1. Get a hashed-password
1# Install tool
2npm install -g node-red-admin
3
4# Get hashed password
5node-red-admin hash-pw
1* After typing the last command, password input console will be shown. Enter a password u like.
2* And then, hashed password will be printed to the console. Copy it to paste in `settings.js`.
32. Modify `settings.js`
1# Open settings.js and modify it to:
2adminAuth: {
3 type: "credentials",
4 users: [
5 {
6 username: "<your-username>",
7 password: "<hashed-password>",
8 permissions: "*"
9 },
10 ]
11},
12...
  • Run NodeRED on background with logging
1node-red > node-red.log &
© 2022 by Petification Develop Blog. All rights reserved.
Theme by LekoArts