1. Code
  2. Yii

Building Your Startup With PHP: Choosing and Configuring Production Hosting

This post is part of a series called Building Your Startup With PHP.
Building Your Startup With PHP: Collecting and Managing User Reports and Feedback
Building Your Startup: Exporting iCal Files into Calendar Events
Final product image
What You'll Be Creating

This tutorial is part of the Building Your Startup With PHP series on Envato Tuts+. In this series, I'm guiding you through launching a startup from concept to reality using my Meeting Planner app as a real-life example. Every step along the way, I'll release the Meeting Planner code as open-source examples you can learn from. I'll also address startup-related business issues as they arise.

In this tutorial, I'll explore choosing an ideal hosting platform for Meeting Planner and the initial configuration of our server. I'll be guiding you through the process as I migrate the pre-launch website to a better production environment for our upcoming alpha launch.

If you haven't tried out Meeting Planner yet, go ahead and schedule your first meeting. I do participate in the comment threads below, so tell me what you think! I'm especially interested if you want to suggest features or topics for future tutorials.

As a reminder, all of the code for Meeting Planner is written in the Yii2 Framework for PHP. If you'd like to learn more about Yii2, check out our parallel series Programming With Yii2.

Selecting a Hosting Company

For many companies, Amazon's AWS is the emerging go-to solution. When AWS goes down, so do many of the web's most well-known services. But AWS is also expensive, often overly complex, and run by a company that has a history of hostility towards employees, especially its female and warehouse workers. Additionally, Amazon has single-handedly pressed the rapid over-development of my hometown Seattle in such a way that has fundamentally changed the city forever, but not necessarily for good.

Recently, I wrote a guide to alternatives to Amazon for cloud computing services. There are a lot of competitors, none as vast, but many better, more affordable, and easier to use. As Meeting Planner grows, I may consider AWS again, but for now I want to look elsewhere.

In the near term, at most, I'll need a server or two running web and database services. For this, managed hosting companies such as Rackspace, Linode, and my favorite Digital Ocean work just fine.

For now, Meeting Planner can run on one server, but as it grows, it's possible I'll need multiple web and database servers. Based on my experience, AWS makes it easier to implement this, but experienced system administrators can easily do this with the aforementioned cloud providers.

This tutorial will guide you through launching a web and database server up in the Digital Ocean clouds.

Working With Digital Ocean

I've written a handful of tutorials about Digital Ocean at Envato Tuts+, including how to use their API for server configuration. I also recently wrote about my positive experience with the Digital Ocean affiliate programFull disclosure: when asked, Digital Ocean provided a small account credit to cover the droplet costs for this startup series for up to six months. 

Over the past two years that I've been a user there, they've consistently incrementally improved their quality of service, user experience, support and feature set. For a while now, it's been extremely rare to experience a slowdown or outage, common in the early cloud hosting market.

Let's get started configuring the initial production server for Meeting Planner.

Configuring a New Droplet

Creating a New Droplet

Digital Ocean instances are called droplets. I'm sure they thought of calling them tadpoles or fish, but we developers are often destroying them, so the non-sentient droplet is a better term.

From the Droplets menu, we'll Create a Droplet. First, we choose Ubuntu 14.04 for our image and the $10 per month server option:

Meeting Planner Hosting - Create Droplet

Next, I chose the San Francisco region because it's closest to me:

Meeting Planner Hosting - Choose a Region

I also asked Digital Ocean to reuse my public key for establishing the login; key login is more secure than password login. Then, I chose the hostname for the server:

Meeting Planner Hosting - Choose a Hostname

When you click Create, you'll see the droplet being formed:

Meeting Planner Hosting - Status of Your Droplet Being Created

Once it's ready, you'll be given an IP address, to which I mapped domain records for the initial configuration: 

Meeting Planner Hosting - Updating Your DNS Records to Point to the Droplet

I'll wait until later to map the root domain to the new server.

Configuring the Server

Here's what you'll see when you log in to the server the first time:

Digital Ocean offers a wide variety of helpful guides. I created a new user for my primary non-root account and deployment accounts using this explainer

I also edited the SSH configuration to eliminate the root login and customize the default SSH port away from 22.

Then, I installed the typical UAMP server (Ubuntu, Apache, PHP and MySQL) on the server. It is possible that I just created a new child acronym of LAMP.

Then, I configured my default timezone. Meeting Planner runs internally on Pacific Standard Time (PST). I edited php.ini:

And, I added this:

Now it's time to get our codebase on the server. 

Migrating the Code Base and Assets

Installing Git

First, I installed git:

Then, as my code deployment user, I created a new SSH key to establish a secure connection with GitHub:

You also have to paste the key into your Git account settings.

Installing Composer

Next, I installed Composer: 

I absolutely love composer because it saves so much time, but there are days when I get annoyed with it, when it stops working out of nowhere—such as how the Yii Framework requires you install the fxp/composer-asset-plugin for proper use:

Installing the Meeting Planner Code

Admittedly, I used GitHub to sync the latest code until I remembered that actually Yii2 configures a lot of files that GitHub ignores, including index.php. There are also local key files and uploaded user profile images. So, I used tar to compress the active codebase:

Then, I used SCP to move the files to the new server, and then I extracted it:

Ultimately, there was also some work in reviewing the local database keys (which I updated on the new server) and reviewing my file permissions.

Now, let's move the database.

Moving the MySQL Database

First, I dumped the existing database to a file:

Then, I moved it to the new server with scp:

On the new server, I created a new database with new details and configured some permissions for Apache:

Finally, I imported the sql file into the new database:

Then it was time to configure Apache.

Configuring the Web Server

I created a .conf file on the new server:

I was able to derive much of it from the existing .conf file:

Then, I activated the site and reloaded Apache:

As I mentioned earlier, I had to make sure that the local files with keys and environment paths were correct on the new server. 

While there were definitely bumps, ultimately, everything came together.

Migrating the DNS

Finally, I re-mapped the DNS for the core Meeting Planner URL. Soon after, the new server was live:

Startup Series - Meeting Planner Home Page

You may notice that I used my recent tutorial about the amazingly awesome Let's Encrypt: I installed https for Meeting Planner before taking the above screenshot.

Updating cron Tasks

Meeting Planner and Let's Encrypt rely on cron for running key processes in the background. So, I moved my crontab settings over from the old server:

Read this if you're wondering why I refer to cron in all lowercase.

What's Next?

Meeting Planner's made tremendous progress the past few months. I've scheduled business meetings, and a couple of personal dates chose to schedule second dates with me using the app. Yet there are still a lot of shortcomings—and feedback from early users is vital. So, in the next tutorial, I'm going to be setting up a system for collecting feedback from users, helping them get answers to common questions and learning as much as I can to make the service better before declaring MVP and an alpha launch.

I hope you've enjoyed this episode. Watch for upcoming tutorials in our Building Your Startup With PHP series—there's also lots of polish work and a few more big features coming up.

Please feel free to add your questions and comments below; I generally participate in the discussions. You can also reach me on Twitter @reifman directly.

Related Links

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.