101对双生儿's Blog

Not pleased by external gains. Not saddened by personal losses.

0%

Deploying OneIndex on Heroku

Students from those schools which offer Office 365 Education are eligible to enjoy up to 5TB of OneDrive for Business storage. With the huge space provided, we can turn it into a file hosting platform, which is similar to Amazon S3, to store and share static files like documents, images, videos, etc. OneDrive for Business supports sharing files with external users, but your school has to opt to enable external sharing, and the links generated are encoded. We can use a program called OneIndex which helps generate direct download links without enabling external sharing. We can link to our files directly later.

Introduction

OneIndex is a PHP program developed by @donwa which indices directories and files in OneDrive or OneDrive for Business. We can deploy it on a PHP server. If you have your own server, you can simply install it on a LAMP (Linux, Apache, MySQL, PHP) stack. In this tutorial, I will demonstrate how to install it on Heroku, where we can deploy cloud applications for free. However, due to Heroku’s ephemeral file system, we still need a temporary server, and we have to install and configure the program twice.

Installing OneIndex on a Server

You may check my previous post to prepare a server if you do not have one. I will deploy a Vultr Cloud Compute (VC2) server with Ubuntu 18.04 in this tutorial.

Installing Apache2 Web Server and PHP

First, follow this tutorial to set up a LAMP stack. Here are some points that you should take care of:

  • Setting up firewall rules is important to keep your server secure. However, you can skip enabling the UFW firewall in Step 1 if your server supports setting up firewall from graphical interface or your server is for temporary use.
  • You can skip Step 2 since we do not need a database. Similarly, you do not to install php-mysql in Step 3.

In addition, we need to enable PHP’s cULR module. Run the following commands to install the module and to restart Apache web server:

1
2
sudo apt install php-curl
sudo systemctl restart apache2

We also need to allow .htaccess overrides. Suppose we will use/var/www/oneindex as the root directory of our OneIndex program. Use your favorite editor to create an Apache configuration file for OneIndex:

1
sudo vim /etc/apache2/sites-available/oneindex.conf

Add the following text to the file:

1
2
3
4
5
6
7
8
<VirtualHost *:80>
DocumentRoot /var/www/oneindex
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/oneindex/>
AllowOverride All
</Directory>
</VirtualHost>

Then run the following commands to enable our site:

1
2
sudo a2ensite oneindex.conf
sudo a2dissite 000-default.conf

Run the following commands to enable Apache mod_rewrite:

1
2
sudo a2enmod rewrite
sudo systemctl restart apache2

Installing OneIndex

Now, let’s start to deploy OneIndex on our server. Download the GitHub repository, move it to the root directory and change the ownership:

1
2
3
4
sudo apt install git
git clone https://github.com/donwa/oneindex.git
sudo mv oneindex/ /var/www/
sudo chown -R www-data:www-data /var/www/oneindex

Now, open the browser and visit your site. Follow this animation to install and configure OneIndex. Some additional configurations can be found in this tutorial. Make sure that your settings are final, since it is not trivial to change them after we move it onto Heroku.

Installing OneIndex on Heroku

On the local machine, follow the first two steps of this tutorial to set up the development environment.

Then prepare the template of the PHP cloud application:

1
2
3
git clone https://github.com/heroku/php-getting-started.git
mv php-getting-started/ onedrive
cd onedrive

Pick your favorite editor, edit composer.json in the root directory to adjust dependencies:

1
2
3
4
5
6
7
8
{
"require" : {
"php": "^7.2.0"
},
"require-dev": {
"heroku/heroku-buildpack-php": "*"
}
}

Run the following command to update the dependencies:

1
composer update

In the root directory, replace the web directory by OneIndex program:

1
2
3
rm -rf web/
git clone https://github.com/donwa/oneindex.git
mv oneindex/ web

In case you want to put OneIndex in a subfolder, make sure to set up .htaccess correctly under the web directory:

1
2
3
4
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L,QSA]

Now, we need to “borrow” the configuration files of OneIndex on the server.

As stated before, Heroku has an ephemeral file system, which means that any files written will be discarded eventually. Generated by the program when we install OneIndex, caches and configuration are not parts of the Heroku application. Therefore, they will go away as well. It does not matter whether caches are persistent, but we have to make sure that configuration is persistent. Therefore, we have to use a temporary server to generate the configuration first and put the files into the Heroku application’s repository before publishing it.

Pick your favorite SFTP or SCP client, for example, graphical interface tools such as FileZilla and WinSCP (for Windows only) or command-line tools such as sftp or scp. Download base.php and token.php from the server (inside /var/www/onedrive/config/) to your local repository (web/config).

Last, create an application on Heroku, and use Heroku CLI to push the local repository to the cloud (supposing your application is oneindex):

1
2
3
heroku login
heroku create oneindex
git push heroku master

Open the browser and verify whether OneIndex is working on https://oneindex.herokuapp.com/.

Reference

  1. M. Drake, “How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 18.04,” DigitalOcean. [Online]. Available: https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-ubuntu-18-04.
  2. B. Boucheron, “How To Install WordPress with LAMP on Ubuntu 18.04,” DigitalOcean. [Online]. Available: https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-with-lamp-on-ubuntu-18-04.
  3. B., “How To Set Up Apache Virtual Hosts on Ubuntu 16.04,” DigitalOcean. [Online]. Available: https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-16-04.
  4. “Getting Started on Heroku with PHP,” Heroku. [Online]. Available: https://devcenter.heroku.com/articles/getting-started-with-php.

扯些别的

专题《FUN@HK 香港玩啲乜》暂时跳票了。在这个专题里,我将会分享许多香港鲜为人知的好去处。然而由于资料整理起来比较耗时间,这个专题何时上线还不确定。

回国以后补、追了这些番: