Personal Cloud for Self-organization

by blakeearth in Circuits > Raspberry Pi

1297 Views, 8 Favorites, 0 Comments

Personal Cloud for Self-organization

Untitled.png


If you enjoy this content, please follow me here on Instructables and subscribe on YouTube! I really love making things that anyone can try at home. I also have a website, blake.earth! I hope to see you around!

Organization has never been one of my strong suits, so when I learned that I could make my own private server and keep all my tasks, files and emails in one place, I was pretty excited. I'm a student, so adding my homework to my "cloud" (and syncing it across my devices) has helped me maintain my grades. It's actually way simpler than you'd think to set up. I highly recommend this for everyone who has a few hours to set one up- it's inexpensive and very helpful.

Materials

Installing Raspbian

Raspbian is a Debian-based operating system designed for use with the Raspberry Pi.

For this step, download (and, if applicable, install) the following materials to your computer. This assumes you have all physical materials readily accessible.

First, insert the microSD card into the adapter, and the adapter into your personal computer. Then, open SD Formatter. Ensure that you have selected the microSD card as the drive. Press "Options" and select "Erase (Full)" as the format type. For the format size adjustment, choose "ON". These settings will ensure that the drive is totally wiped and ready to be used. Press "OK" and "Format" to format the drive.

After the process is finished, close SD Formatter and open Win32DiskImager. Click the blue folder and navigate to your Raspbian disk image. Select it and press "Open". Ensure that the correct drive is selected and press "Write". Press "Yes" to confirm. Wait for the progress bar to complete, then close Win32DiskImager.

Raspbian is now installed to the microSD card. Eject the adapter, remove the microSD card, and insert it into the Raspberry Pi. Connect the Ethernet cable to your router and to the Pi. Plug the Pi into the MicroUSB charger to turn it on.

Connecting to the Pi

In this step, we'll prepare your Raspberry Pi to host a server.

For this step, download (and, if applicable, install) the following materials to your computer. This assumes you have all physical materials readily accessible.

Begin by launching Advanced IP Scanner. Once it starts, press the "Scan" button. This will look at every device connected to your WiFi network and display its name and private IP address. Wait for the progress bar to show completion of the scan. Look for a computer with the name "raspberrypi". Find its private IP address and write it down. Then, close Advanced IP Scanner.

Open PuTTY. Type the IP you wrote down into the "Host Name (or IP Address)" field. Keep the port set to 22. Select "SSH" and press "Open" to begin connecting.

You may receive an error message. Dismiss it by pressing "OK" or "Yes".

You will be taken to a login screen. The default username for a Raspberry Pi is "pi", so enter that into the "login as:" field. The default password is "raspberry", so enter that into the "pi@'s password:" field. You should be welcomed into the SSH connection.

Preparing the Pi and Router

Now that you've logged in to the Pi, it's time to set it up to host a server.

Use the following command to make your Raspberry Pi's IP address static. This is necessary to prevent your router from changing its IP and stopping incoming connections. Use the right-click button to paste.

sudo nano /etc/dhcpcd.conf

You may have to provide the password ("raspberry") to continue. It should open a sample configuration for a DHCPCD file, which controls what kinds of IP addresses can be assigned to the Pi. Using the arrow keys to navigate, paste the following code into the top of the file.

interface eth0
 static ip_address=XXX.XXX.X.X/24
 static routers=XXX.XXX.X.1
 static domain_name_servers=8.8.8.8

Press CTRL-X, then Y, then ENTER to save the file.

Type the following command to make your changes take action.

sudo reboot

You'll also need to open the ports on your router and direct them to your Pi. Open the ports "80" and "443" for the IP you assigned to your Pi. I won't cover how to open them because the process varies dramatically from router to router. You can use the site PortForward to get specific instructions for your device. I do not recommend downloading any of the site's utilities, however. They just don't look very trustworthy, to be honest.

Note: Do not include the "/24" when you forward ports. This specifies the subnet mask, and is not relevant to directing ports.

After you've opened and directed the ports, type "IP" into a Google search. The number Google outputs to you is your public IP address. Write this number down somewhere.

Setting Up the Server

In this step, we'll get the server going. This is the longest and most complicated step. You can pretty much just copy and paste the commands below, and that's what I'll be doing in the video, but it's more fun if you understand what's going on.

We'll begin by installing our server components (to the Pi), which include the following virtual materials:

  • Apache
  • PHP
  • MySQL

Before we begin this step, you'll likely need to update your package manager. This will allow your Pi to find the locations of the packages we'll install in this step.

Note: All commands in this step are run on the Raspberry Pi through PuTTY.
sudo apt-get update

Now, run the command to install the current Apache version (at the time of this Instructable's creation). The "-y" tag confirms that you want to install Apache.

sudo apt-get install apache2 -y

Now, if you've opened and directed the ports correctly, you should be able to view a sample page. Type the public IP address you wrote down in the last step into a new browser tab. You should get a page that reads "Apache2 Debian Default Page". If you don't reach this page, you should go back and check your DHCPCD file and make sure the IP address listed there matched the one you forwarded ports for.

Next, we'll install PHP. This installs both PHP and Apache mods which allows Apache and Nextcloud to recognize and use PHP.

sudo apt-get install php5 libapache2-mod-php5 php5-curl php5-gd -y

Lastly, we'll install MySQL. This is a little more involved than the other two required installations because you'll need to set up a database as well as install the software. First, we'll install the required components. The following command will install a database server as well as a mod for PHP which allows PHP to communicate with MySQL's database(s).

sudo apt-get install mysql-server php5-mysql -y

Upon running this command, you'll be asked for a password. Write down whatever you give it. Now, let's set up the database we want Nextcloud to use. We'll begin by logging in to our MySQL installation. The following command uses the tags "-u" and "-p" to specify a user and a password. If you'd like, you can type the password you wrote down after the "-p" tag. I've left it blank in the command below, so I'll be asked for it when I run the command.

mysql -uroot -p

You'll be welcomed into a MySQL terminal. From here, we can create databases. Use the following command to create a database called "cloud". Note that all MySQL commands are followed by semicolons.

create database cloud;

You should get a message back saying "Query OK". You can close out of MySQL using CTRL-D.

Lastly, reboot Apache to refresh it, allowing it to recognize PHP and MySQL.

sudo service apache2 restart

Running Nextcloud

In this step, we'll install Nextcloud and get it running with MySQL. This step includes a lot of commands, but they aren't very complex.

For this step, we'll download the following to your Pi. This will be done in the PuTTY shell.

First, direct your PuTTY shell to the web server directory using the following command:

cd /var/www/html/

Then, remove everything in the folder to ensure a clean installation of Nextcloud. The "*" acts as a wildcard, telling the Pi to remove everything, and the "-r" tag tells the Pi to include folders in its deletion.

sudo rm -r *

Now, let's install Nextcloud to this directory. The following command will download a .zip file of Nextcloud.

sudo wget https://download.nextcloud.com/server/releases/nextcloud-10.0.1.zip

Once that finishes installing, unzip the file with the following command.

sudo unzip *.zip

This may take a while to complete. Once it does, remove the .zip file. You don't need it anymore.

sudo rm *.zip

Now, we need to navigate to the "nextcloud" folder and move all the files in the folder we just created up one directory so that they are at the root of our /var/www/html folder. To do this, use the following compound command.

cd /var/www/html/nextcloud && sudo mv * .[^.]* ..

Just to clean up, we'll go back to our root directory and remove our old "nextcloud" folder:

cd /var/www/html && sudo rm -r nextcloud

Now, we need to make sure Nextcloud has write access to these folders. The following command will give it permission to modify our web server's root directory and restart Apache to allow the changes to take place.

sudo chown -R www-data: . && sudo service apache2 restart

Configuring Nextcloud

In this step, we'll set up Nextcloud. You won't need any additional programs or software for this step.

In a new web browser window, type your public IP address. This should open a "first-launch" setup page for Nextcloud. In the "Username" and "Password" fields, type the information you want to use when you login to your Nextcloud installation (you're creating your account).

Leave the "Data folder" as-is.

For the "Database user" field, enter "root". For the "Database password" field, enter the password you chose for MySQL. In the "Database name" field, enter "cloud". Leave the "Database host" field as-is.

Press ENTER to finish the installation.

You'll be logged in to Nextcloud and should see a list of your files. You can also switch to the calendar app, email app, and more awesome applications that you can use to make your life easier. This is really all you need to have a functional installation, but there are a few security recommendations which I'll cover in subsequent steps.

Securing Nextcloud (Optional, But Recommended)

In this step, we'll perform a few security procedures to ensure that our Nextcloud installation is safe from attack.

We'll encrypt Nextcloud and give it access to the web server's .htaccess file, which includes the following materials (which will be downloaded to our Pi):

To begin, navigate to your Pi's "var" folder.

cd /var/

Then, install the Let's Encrypt repository using the following command.

sudo git clone https://github.com/letsencrypt/letsencrypt

Next, navigate into the "letsencrypt" folder.

cd letsencrypt

Now, we're going to acquire our free HTTPS certificate(s), courtesy of Let's Encrypt. After the "-d" tag (which stands for domain), type your domain name which has been directed to your public IP address.

./letsencrypt-auto --verbose --apache -d example.com

This process will take a while. You'll eventually be asked for an email address, which will be used to contact you when your certificate is about to expire. When you're asked if you want to secure fully, I recommend choosing the "Secure" option. Then, restart Apache.

sudo service apache2 restart

Next, we're going to give enable HSTS to take full advantage of our Let's Encrypt certificate. To begin this process, run the following command.

sudo a2enmod headers

Next, run the following command to edit your default SSL configuration for Let's Encrypt.

sudo nano /etc/apache2/sites-available/000-default-le-ssl.conf

Paste the following code just beneath the line .

<IfModule mod_headers.c>

      Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains; preload"

</IfModule>

Lastly, we're going to allow Nextcloud to override the default .htaccess file. To begin, edit the default configuration file for Apache.

sudo nano /etc/apache2/apache2.conf

Scroll down to where you see the line . Make sure your copy of the file reads "All", as shown below.

AllowOverride All

Lastly, save the file (with CTRL-X, Y, and ENTER) and restart Apache.

sudo service apache2 restart

Improving Nextcloud's Performance (Optional)

In this step, we'll run a simple command to allow Nextcloud to build a cache in order to improve performance.

To begin, install the APCu module for PHP.

apt-get install php5-apcu

Then, edit the configuration file for Nextcloud.

sudo nano /var/www/html/config/config.php

Add this code between the bottom line:

'memcache.local' => '\OC\Memcache\APCu',

Then save (with CTRL-X, Y, and ENTER) and restart Apache.

sudo service apache2 restart

That's it! You've allowed Nextcloud to keep a memory cache.

Thanks so much for reading through this Instructable! If you're looking for more content like this, please consider following me here on Instructables and subscribing on YouTube. Thanks again!