Few months back I wrote an article about developing RESTful services using PHP, Slim and MySQL. But the services are not accessible to public as they are limited to localhost. In this article we are going to learn how to host the services on a realtime server, so that, they can be accessible from anywhere.
I tried hosting the services on free hosting spaces (like 000webhost.com), but because of limitations (like not supporting PUT, DELETE methods and mysqlnd) I couldn’t able to run the services. So I decided to move to a VPS hosting like Digitalocean to host the services. Digitalocean provides very high hardware configuration in cheaper prices (which is almost equal to shared hosting).
As this tutorial is continuation of below two articles, make sure you learn them well to know how to develop RESTful services for your android app.
Download the code from this link as we are going to see hosting this project into digitalocean.
1. Buying & Setting Up DigitalOcean (Get $10 free credit)
DigitalOcean offers various pricing plans ranging from $5/m to $640/m. For an entry level app, $5/m plan (which comes with 512MB Memory, 1 Core Processor, 20GB SSD & 1TB Transfer) is powerful enough to get started. If you think this configuration is not enough for your app, you can always upgrade to other plans.
Register your self with DigitalOcean by creating an account. Use this link to get $10 free credit in your account. In other words you don’t have to pay for the first two months (assuming you are choosing $5 plan).
⇒ Create a new account here. Give your email and password. You’ll receive the confirmation mail to your email account.
⇒ Activate the account by verifying the email sent to you.
⇒ Once the account is activated, you will be prompted to give your credit card details. Without giving the credit card details you can’t create the droplet. Don’t worry about giving the credit card information, there won’t be any money deducted from your card right away.
Check the below video to know the registration process.
2. Getting the Droplet Ready
Once you gave the credit card details, you will be allowed to create a droplet. Droplet is nothing but an instance of a server. Follow below steps to configure your droplet.
2.1 Click on Create Droplet on left sidebar or click this link to go to the page directly.
2.2 Give your droplet a name
2.3 Select the size. I selected $5/m plan.
2.4 Select the region. I selected New York.
2.5 Select the distribution image. I selected Ubuntu 14.04×64
2.6 Under applications tab, select LAMP on 14.04. This will install Apache, MySQL and PHP on creation of the droplet.
2.7 Click on Create Droplet to complete.
It will take few minutes to create the droplet. Once the droplet is created, your new server instance details (ip, username and password) will be mailed to your email id.
If you visit the ip address in browser, you can see apache welcome page. Now follow below steps to complete the droplet setup.
2.8 Logging into droplet via SSH
You can login into your server via ssh. Mac users can directly connect using terminal tool. But windows users needs download ssh client like Putty to login. Download Putty and execute below commands to login into your server.
In putty, give your server ip address, port number as 22 and click open. You will be asked to give your username and password. Use the credentials those sent to your email.
Mac users, run the below command directly in terminal.
2.9 Resetting Root Password
When you are logging in for the first time, you will be asked to change your password. Give your old password and new password to change.
3.0 Resetting MySQL Password
As MySQL is installed by selecting the LAMP app while configuring the droplet, we are not sure about the mysql password. You can see the current MySQL password in the console when you are logging in. This password is randomly generated and kept. In order to change the MySQL password, follow below steps.
sudo service mysql stop
sudo /usr/sbin/mysqld --skip-grant-tables --skip-networking &
mysql -u root
Replace ‘newpassword’ with your new mysql password.
SET PASSWORD FOR root@'localhost' = PASSWORD('newpassword');
You can find complete article about resetting MySQL password here.
3.1 Enabling mod rewrite
Apache rewrite module helps in writing the urls to another url. This is useful for us to shorting the Slim urls. So enable it by running the below command.
sudo a2enmod rewrite
3.2 Enabling mysqlnd
MySQL native driver is needed to run the mysqli functions. So install mysqlnd by executing the below commands.
sudo apt-get update sudo apt-get install php5-mysqlnd
Now restart the apache to take the changes effected.
sudo service apache2 restart
Checkout the below video that will take you through completing the droplet setup.
3. Installing phpmyadmin
You can create database using the mysql command line tool or using the phpmyadmin. I prefer using phpmyadmin as it is lot more easier than the command line tool. Follow the below steps to install phpmyadmin.
sudo apt-get install phpmyadmin apache2-utils
While installing you need do below steps.
⇒ Select Apache2 by pressing the SPACE key. When selected, you should see a start (*) indicating Apache2 is selected.
⇒ Choose YES whenever it prompts to configure the database for phpmyadmin
⇒ Enter MySQL password when prompted
⇒ Enter phpmyadmin password that you want to login with
Now you can test your phpmyadmin by visiting http://_YOUR_IP_ADRESS_/phpmyadmin (Replace _YOUR_IP_ADRESS_ with your actual server ip)
The below video will take you through installation of phpmyadmin.
4. Creating the Database
Goto your phpmyadmin and execute the below SQL to create the required database and tables. Once executed, you can see the newly created database on the left panel.
CREATE DATABASE task_manager; USE task_manager; CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(250) DEFAULT NULL, `email` varchar(255) NOT NULL, `password_hash` text NOT NULL, `api_key` varchar(32) NOT NULL, `status` int(1) NOT NULL DEFAULT '1', `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `email` (`email`) ); CREATE TABLE IF NOT EXISTS `tasks` ( `id` int(11) NOT NULL AUTO_INCREMENT, `task` text NOT NULL, `status` int(1) NOT NULL DEFAULT '0', `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ); CREATE TABLE IF NOT EXISTS `user_tasks` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `task_id` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `user_id` (`user_id`), KEY `task_id` (`task_id`) ); ALTER TABLE `user_tasks` ADD FOREIGN KEY ( `user_id` ) REFERENCES `task_manager`.`users` ( `id` ) ON DELETE CASCADE ON UPDATE CASCADE ; ALTER TABLE `user_tasks` ADD FOREIGN KEY ( `task_id` ) REFERENCES `task_manager`.`tasks` ( `id` ) ON DELETE CASCADE ON UPDATE CASCADE ;
The below video will show you creating database and tables.
5. Uploading your PHP Project
Now we’ll upload our project to the server. For this you need a FTP software like FileZilla. Download FileZilla and login to your server using the credentials as Host (your ip address), Username, Password and Port (give 22 as port number).
⇒ Once connected, goto the directory /var/www/html on the right panel and delete index.html and info.php. This is the root directory for all our php projects.
⇒ In your php task_manager project, goto include and change the credentials of MySQL in Config.php with your database username, password and database name.
⇒ Now upload the task_manager folder to html directory.
⇒ Goto /etc/apache2, open apache2.conf and change AllowOverride to All under <Directory /var/www/>. This step is very important to eliminate index.php from our api urls.
<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory>
The below video will show you how to upload your project using FileZilla.
6. Testing the API
Now we have everything in place, let’s test the API using Chrome Advanced REST client. Install the extension and test your url endpoints as shown in the below video.
|http://your_ip_address/task_manager/v1/register||POST||name, email, password||User registration|
|http://your_ip_address/task_manager/v1/login||POST||email, password||User login|
|http://your_ip_address/task_manager/v1/tasks||POST||task||To create new task|
|http://your_ip_address/task_manager/v1/tasks||GET||Fetching all tasks|
|http://your_ip_address/task_manager/v1/tasks/:id||GET||Fetching single task|
|http://your_ip_address/task_manager/v1/tasks/:id||PUT||Updating single task|
|http://your_ip_address/task_manager/v1/tasks/:id||DELETE||task, status||Deleting single task|
7. Mapping the Server with a Real Domain URL (Godaddy)
You can buy the domain from whichever service you like. In this tutorial I used Godaddy.com to buy my domain url.
⇒ Goto godaddy.com and search for the domain you want to buy. If it is available, complete the checkout process.
⇒ Launch the Zone File Editor for your domain.
⇒ Click Add New Record. A popup will be shown to edit the record.
⇒ From the Record type list, select A(Host)
⇒ Enter host value as @ and points to value is ip address of the droplet and click on finish.
It will take sometime to get the changes reflected depending upon your service provider. After sometime, if you visit the domain, it should points to our droplet. Once mapped, you can change the ip address to your domain url in the REST API urls.
Check the below video to get clear understanding of mapping the domain with droplet.
Hi there! I am Founder at androidhive and programming enthusiast. My skills includes Android, iOS, PHP, Ruby on Rails and lot more. If you have any idea that you would want me to develop? Let’s talk: firstname.lastname@example.org