Turn an Old PC Into a Do-It-All Home Server for IoT
Just with some extra storage, you can add a web server, MySQL database management system, NodeJS, and NAS folder.
Purpose
Oftentimes, I would want to have a web server running that could process commands from my local IoT devices, as well as host webpages and even have a MySQL server going for storage. I tried at first to use my primary PC, which although powerful, lacked the necessary storage and drew too much power to be on constantly. Then, I attempted to use a Raspberry Pi, but it turns out that even a Raspberry Pi 4 with 4GB of RAM still lacks the power and storage capacity to run all tasks simultaneously. That's when I remembered I had an old PC lying around, which already had an Intel i3-8100 and GTX 1050Ti, which would be perfect for running the necessary web servers and maybe even for training TensorFlow models in the future.
Components Used
Since I already had an i3-8100 and GTX 1050Ti, all that was left was the power supply and storage. I went with an EVGA 400W PSU, which was plenty for the system. In order to have fast boot times, my primary drive was a simple 120GB SSD from Sandisk. Finally, I added 3- 2TB HDDs for storing the majority of the files.
Installing an OS
I chose to use Ubuntu 19.10 due to its ease of use and out-of-the-box support for many Nvidia GPUs. I simply downloaded the iso image and then wrote it onto an 8GB USB flash drive using Balena Etcher. After the flashing was completed, I plugged it into the PC and installed the full OS to the SSD and rebooted after removing the USB drive.
RAID 5 Configuration
Accessing a single drive and reading/writing files to it is fine for simple tasks, but it has three primary drawbacks:
- Slow access time; each file must be read sequentially off of one drive at a time.
- No builtin recovery options; if the data were to get corrupted, then it's gone.
- Accessing as a single mounted drive; on Linux, drives show up in the form of /dev/sda, /dev/sdb/, etc. But what if we want to see them as one large drive?
This is where a RAID 5 configuration comes into play. By using the mdadm utility, you can create software RAID configs with ease. I went with RAID 5 because it gives a great combination of both speed and reliability, albeit at the expense of some storage space. You can read more about RAID configuration types here.
I began by running the command lsblk -o NAME,SIZE,FSTYPE,TYPE,MOUNTPOINT
to see what drives were present, and I took note of which ones were my 2 TB drives. Make sure you don't accidentally overwrite the boot drive with the RAID array.
You can install mdadm by running sudo apt install mdadm
.
Then I ran sudo mdadm --create --verbose /dev/md0 --level=5 --raid-devices=3 /dev/sdb /dev/sdc /dev/sdd
to actually create the RAID array. This will use the drives b through d and have the array show up under /dev/md0
. This command will take a while to complete (8 hours for me), and you can check its progress by running cat /proc/mdstat
.
In the meantime, run the following commands:
sudo mkfs.ext4 -F /dev/md0
sudo mkdir -p /mnt/md0
sudo mount /dev/md0 /mnt/md0
Once the cat /proc/mdstat
command shows up as completed, you can then run:
sudo mdadm --detail --scan | sudo tee -a /etc/mdadm/mdadm.conf
sudo update-initramfs -u
echo '/dev/md0 /mnt/md0 ext4 defaults,nofail,discard 0 0' | sudo tee -a /etc/fstab
Now the RAID 5 array is done and ready to have some files get put on it.
Apache Web Server
In order to host webpages, I went with the Apache web server, as it gives plenty of flexibility while also being fast. To install it on your machine, simply run sudo apt install apache2
. Now the server should be running. You also need to allow it through the firewall by running sudo ufw allow 'Apache'
. This will allow for Apache to communicate outside of your system. If all was successful, you should see the default Apache page when going to http://server_ip
.
Node JS Server
Node JS is a web server platform that allows for powerful, scalable applications to be created with javascript. I use it a lot for my webpage backends, especially when it comes to DIY IoT devices. Installing it is easy, just enter in the following commands:
sudo apt install curl
curl -sL https://deb.nodesource.com/setup_13.x | sudo -E bash -
sudo apt install nodejs
Then run node -v
and npm -v
to ensure they are installed and up to date.
MySQL Server
Almost every IoT project that uses storage does it through a database, as they are fast, allow for concurrent usage, and secure. Installation of MySQL server is quite simple, as all you have to do is run the command below:
sudo apt install mysql-server
After setting it up, run systemctl status
mysql.service
to see if it's running.
Samba Folder Sharing
Finally, I installed a utility called Samba, which allows for others computers to connect to a folder that is being shared over a network. It's great for cross-OS connectivity, and removes the need for using a program such as Filezilla or WinSCP that use SFTP to transfer files.
Run the following commands to set it up:
sudo apt install samba
sudo smbpasswd -a <user_name>
sudo chown <user_name> /mnt/<path to raid 5 share folder>/<folder_name>
sudo nano /etc/samba/smb.conf
Then add the following lines at the bottom of the configuration file:
[<folder_name>]
path = /mnt/<path to raid 5 share folder>/<folder_name>
valid users = <user_name>
read only = no
And finally run sudo service smbd restart
to restart the service. You can connect to the folder on Windows 10 by going to the Network folder, adding a mapped network drive, and then searching for the folder that you added to the samba config file. Use the same username and smb password you set up previously.