Monday, March 9, 2015

Creating a Minimalist CentOS Home Server

It's about time I wrote this, so here we go. I built a little NAS/home server over Christmas time a few months ago. It's based on a FM2 3.5Ghz dual core, a mATX Gigabyte board with eight Sata3 ports, and currently has three 4TB hard drives. I've been jumping between what OS to run on it for a while: first it had Arch, then some Debian, FreeBSD and now here we are at CentOS.

Why? Arch was stable but for fear of things going bad (Arch is not meant to be a server), I switched to Debian which had consistent issues with full network bandwidth causing the Ethernet driver to crash. I had high hopes for BSD then realized they dont support XFS anymore which means I couldn't mount my data drives. So here goes CentOS.

While this is meant for CentOS, most commands are similar and software names are the same, so it can be used as reference for other systems.


Requirements

Obviously, the first thing you'll need is a computer, but I guess the installer files are good too. They can be found here on the CentOS site. I used the minimal build found on the bottom left of the page. Unless you like living in the stone age, there is no point to using a CD or DVD, so grab a USB stick and use this Win32DiskImage utility to write the image to a USB stick.

Also, you'll need some time. The install only took a few minutes, but the majority of the time will be spent on configuring and installing software after the install of CentOS.


Installation

This is pretty straight forward. There are not many options, so no changes are really needed. Fill out all the forms and be sure to create a user account along with a root user.


Configuration

SSH

SSH may be blocked by the default firewall service firewalld. Simply run the folowing to open the ports:
firewall-cmd --permanent --zone=public --add-service=samba
firewall-cmd --reload

FSTAB

The data drives need to be mounted on system start-up. This is done with fstab which is located at /etc/fstab. Using the following command I found the UUIDs of my data drives:
blkid

I then created mount points in /mnt and added the following lines to the fstab:
UUID=fbea912f-7a10-4e86-b5ed-32db97a36778       /mnt/4TB.1              xfs     defaults        0 2
UUID=72430954-62c2-4b71-97e7-3aad660b30c5       /mnt/4TB.2              xfs     defaults        0 2
UUID=c6a421e9-a9df-4ccb-8f6a-3794ecc12c4b       /mnt/4TB.3              xfs     defaults        0 2

Samba Shares

Next we need to share these drives with the network. Only Windows machines will be accessing it, so Samba file sharing is used.
yum install samba samba-client

Next, open the config file:
nano /etc/samba/smb.conf

You'll probably want to change the workgroup and server strings. Then find the Share Definitions section near the bottom and add something like so:
[sharename]
comment = description
path = /path/to/directory
guest ok = no
browseable = yes
writable = yes

Next, we need to set the selinux context so the data hard drives to be used remotely and set permissions(otherwise they just don't show up remotely). Also, SELinux can be disabled in /etc/selinux/config if you don't want to deal with it.
chcon -R -t samba_share_t /mnt/4TB.*
chmod -R 777 /mnt/4TB.*

Next the samba user needs to be created.
smbpasswd -a username

Then make the samba server start on bootup:
systemctl enable smb.service
systemctl enable nmb.service
systemctl restart smb.service
systemctl restart nmb.service

Finally, allow it through the firewall:
firewall-cmd --permanent --zone=public --add-service=samba
firewall-cmd --reload

While this samba configuration isn't very complex, it is simple and provides all the functionality I need.


Plex Media Server

Go to the Plex downloads page here and get the latest link for the CentOS rpm package. Then run:
yum install wget
wget {link to rpm from above}
yum localinstall {package from previous line}

Next we need to fix issues with selinux and the firewall again.
nano /usr/lib/firewalld/services/plexmediaserver.xml

and paste in the following:
<?xml version="1.0" encoding="utf-8"?>
<service version="1.0">
  <short>plexmediaserver</short>
  <description>Plex TV Media Server</description>
  <port port="1900" protocol="udp"/>
  <port port="5353" protocol="udp"/>
  <port port="32400" protocol="tcp"/>
  <port port="32410" protocol="udp"/>
  <port port="32412" protocol="udp"/>
  <port port="32413" protocol="udp"/>
  <port port="32414" protocol="udp"/>
  <port port="32469" protocol="tcp"/>
</service>

Then add the service and reload the firewall:
firewall-cmd --permanent --zone=public --add-service=plexmediaserver
firewall-cmd --reload

At this point I was tired of dealing with selinux, so I just disabled it. This makes the chcon command in the samba setup irrelevant. Do this by editing /etc/selinux/config and setting selinux to disabled.
nano /etc/selinux/config

Finally, enable and start the Plex service:
systemctl enable plexmediaserver
systemctl start plexmediaserver


FTP Server

In this case i'll be using VSFTPD, which is a nice lightweight server: perfect for my uses. Install it and then start the service:
yum install vsftpd

Next edit the config and set the options below:
nano /etc/vsftpd/vsftpd.conf

anonymous_enable=NO
local_enable=YES
ascii_upload_enable=YES
ascii_download_enable=YES
ftpd_banner=
use_localtime=YES

We also need to change SELinux and the firewall again. 
setsebool -P ftp_home_dir on
firewall-cmd --permanent --zone=public --add-service=ftp
firewall-cmd --reload

Finally, enable the service:
systemctl enable vsftpd
systemctl start vsftpd


FTP Client

Since this is a headless server, a regular gui ftp client wont do. rather than using the default ftp terminal client, i'll be using lftp. It's available in the yum repository:
yum install lftp

lftp downloads content to the directory it is executed from, so change to the downloads directory first. Start lftp like so
lftp ftp://username@Your.Server.ip

You can then navigate your server's file directory using the usual linux cd command. Using the following commands you can download single files and directories with n being the number of simultaneous connections. 
pget -n 5 FileName
mirror --use-pget-n=5 DirectoryName

It also has a queue feature. Simply add queue before the previous commands and it adds the files to the queue. Enter queue stop before adding any files to prevent immediate downloading. queue -d  to clear the queue. Use  job -v  to list the current download and queue to view the queue. To stop transfers enter kill or kill 1 where 1 is the queue number.

lftp can run in the background so downloads can be started via a ssh session without having to maintain the connection. To re-open the lftp session simply type pgrep lftp in your Linux shell and then start lftp and enter attach {PID from pgrep}


rTorrent with ruTorrent WebUI

This can get a bit messy doing it by hand, but fortunately, there is a auto install script here. Grab the latest version using wget and run it:
wget --no-check-certificate https://raw.github.com/etiennerached/rutorrent-auto-installer-centos/master/rutorrent-installer.sh

sh rutorrent-installer.sh

Apache is having some issues with CentOS 7, so use nginx when prompted. VSFTPD is not needed as we have already installed it, obviously. All other options are prompted for and self explanatory.

Of course, firewall exceptions need to be added. The first line is only needed if you installed webmin.
firewall-cmd --permanent --zone=public --add-port=10000/tcp
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload


Filebot

Perhaps the most vital part to this is Filebot: a program which automatically runs after a downlaod is complete, renames it, and finally puts it in a nice organized directory where the Plex Server can addit to the library. Automatically. It's awesome. Paired with a RSS feed with the latest media in rTorrent, this is amazing. 

Filebot requires Java 8 so get that from oracle and install it:
cd /opt/

wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u40-b25/jdk-8u40-linux-x64.tar.gz"

tar xzf jdk-8u40-linux-x64.tar.gz
rm -rf jdk-8u40-linux-x64.tar.gz
cd /opt/jdk1.8.0_40/

alternatives --install /usr/bin/java java /opt/jdk1.8.0_40/bin/java 2
alternatives --config java
alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_40/bin/jar 2
alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_40/bin/javac 2
alternatives --set jar /opt/jdk1.8.0_40/bin/jar
alternatives --set javac /opt/jdk1.8.0_40/bin/javac 

export JAVA_HOME=/opt/jdk1.8.0_40
export JRE_HOME=/opt/jdk1.8.0_40/jre
export PATH=$PATH:/opt/jdk1.8.0_40/bin:/opt/jdk1.8.0_40/jre/bin

Next grab the Filebot portable package
mkdir ~/scripts/filebot
cd ~/scripts/filebot

wget http://iweb.dl.sourceforge.net/project/filebot/filebot/FileBot_4.5.6/FileBot_4.5.6-portable.zip

unzip FileBot_4.5.6-portable.zip
rm -rf filebot.cmd FileBot.exe FileBot.l4j.ini

Add the following to your rtorrent config file at ~/.rtorrent.rc 
system.method.set_key=event.download.finished,filebot_amc,"execute={~/scripts/TorrentCompleteFB.sh,$d.get_base_path=,$d.get_name=,$d.get_custom1=}"

Create ~/scripts/TorrentCompleteFB.sh and add something like seen below. This changes how FileBot renames files and where it puts them.
#!/bin/bash
TORRENT_PATH=$1
TORRENT_NAME=$2
TORRENT_LABEL=$3

sh ~/scripts/filebot/filebot.sh -script fn:amc --output "/output/some/where" --log-file ~scripts/amc.log --action duplicate --conflict override -non-strict --def plex=127.0.0.1 gmail=user:password music=y "seriesFormat=/tv/save/location/{n}/Season {s}/{n} - {sxe} - {t}" "animeFormat=/anime/save/location/{n}/{n} - {e} - {t}" "movieFormat=/movie/save/location/{n} ({y})" "musicFormat=/music/save/location/{n}/{n} - {t}" "ut_dir=$TORRENT_PATH" "ut_kind=multi" "ut_title=$TORRENT_NAME" "ut_label=$TORRENT_LABEL" &

Conclusion

So, there we go. Everything I need in a basic home NAS.

3 comments:

  1. Really miss ya, Jordan. You were really doing some interesting projects and I wish I had your savvy! If this is your last post, I wish you well!!!

    ReplyDelete
  2. I guess it's difficult to get free product keys for your office, but you can get a much cheaper one from www.vanskeys.com, I got my key from this site and its low price surprised me a lot. You can go this site to have a try.

    ReplyDelete
  3. BlueHost is ultimately one of the best hosting company for any hosting services you require.

    ReplyDelete