Building Wordpress sites in the cloud

Door creator1988 op woensdag 11 juli 2012 15:20 - Reacties (3)
Categorie: Frontend, Views: 32.854

As part of the new features that we launched two weeks in Cloud9 IDE we've added support for Python, Ruby and PHP as server side languages besides node.js. That's not just it, because users have full freedom over the VM that we run your code on so they can install any platform they like (C++ development in the cloud f.e.). The cool thing is that it's now possible to bring all sorts of already existing applications into Cloud9, without relying on third parties to do the actual Apache hosting etc. because you'll just get an Openshift server. In this post I'll show a step by step demo of how to use Cloud9 to build a Wordpress application without leaving the browser.

If you want to see the quickest demo possible, sign up for Cloud9 at c9.io, log in via GitHub and create an index.php file:

PHP:
1
2
<?php
echo 'Hello world'


When you now click the 'Debug' button we spawn a shell version of PHP that echoes 'Hello world' back to you. To run it via Apache go to the run panel, select 'Apache+PHP' and re-click 'Debug'.

http://100procentjan.nl/tweakers/wp1.png

Installing wordpress
To install wordpress, we'll download the latest .zip file, unzip it. Then right click on the root folder in your tree and select 'Upload files' and choose the wordpress folder. Because the folder upload the files are in a seperate subfolder under root. Drag them to root and remove the 'wordpress' folder. Rename the 'wp-config-sample.php' to 'wp-config.php'.

Or do it via the terminal (CMD+T, CTRL+T)

bash:
1
2
3
4
5
$ wget http://wordpress.org/latest.tar.gz
$ tar xzfv latest.tar.gz
$ cp -r wordpress/* .
$ rm latest.tar.gz
$ rm -rf wordpress


Running wordpress
When you now click 'Debug' and open the site, an error is thrown that no database connection could be made. That's correct, because we don't have a database server running on this machine. To make this fun we'll run Wordpress with PostgreSQL instead of MySQL. To get that to work we'll need to install the pg4wp plugin first. Follow the steps in the installation page to add the driver. We still have no connection, because no database was installed on the virtual machine in Cloud9 but now we've got something to get started with. Let's install Postgres first!

Installing PostgreSQL
Cloud9 comes with a fully functional shell, that can be opened via 'View'->'Terminals'->'New Terminal'. This is just a SSH connection that you'd normally have to servers in your data center. That means that you have a limitless environment where you can install stuff (yes even vi!).

http://100procentjan.nl/tweakers/wp2.png

First we'll need to have the Postgres sources, then configure it, and finally make, install and start the server (and yes, this might take a while!).

bash:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ cd ..
$ mkdir pgsource
$ cd pgsource            # go into new directory
$ wget http://ftp.postgresql.org/pub/source/v9.1.4/postgresql-9.1.4.tar.gz        # grab source code
$ tar xzfv postgresql-9.1.4.tar.gz         # untar it
$ cd postgresql-9.1.4
$ ./configure --without-readline --prefix=$PWD/../postgres         # configure
$ make
$ make install
# now to start it
$ cd ~/pgsource/postgres/bin
$ ./initdb -D ../data
$ ./pg_ctl -D ../data -l logfile start
$ ./createdb wp


With a running postgres instance (you can add the bin/ folder to your path if you like) which can be verified by running ./psql, and a created database we can now configure Wordpress to use this database. In 'wp-config.php' change the database settings to (don't have to restart debugging, changes are immediate!):

PHP:

1
2
3
4
5
6
<?php
define('DB_NAME''wp');
define('DB_USER''');
define('DB_PASSWORD''');
define('DB_HOST''localhost:5432');
define('DB_CHARSET''utf8');
define('DB_COLLATE''');
?>

http://100procentjan.nl/tweakers/wp3.png

A running wordpress install, that you can use, modify and edit live from cloud9.

A hosted database solution (also for free users)
Because a local database won't hold when deploying this to a cloud service, we'll use the hosted postgres solution that Heroku offers. Select the dev plan (which is free) and create a database. Click on your new database to get the connection settings (hostname, username, password, etc.). To use this from within Cloud9 we can now load different configurations per environment, because on C9 the 'C9_PROJECT' environment variable is present.

PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
if (getenv("C9_PROJECT")) {
    define('DB_NAME''wp');
    define('DB_USER''');
    define('DB_PASSWORD''');
    define('DB_HOST''localhost:5432');
}
else {
    define('DB_NAME''HEROKUDBNAME');
    define('DB_USER''HEROKUUSER');
    define('DB_PASSWORD''HEROKUPWD');
    define('DB_HOST''HEROKUHOST.compute-1.amazonaws.com:5432');
}

define('DB_CHARSET''utf8');
define('DB_COLLATE''');
?>


If you want to migrate data from local -> hosted database, you can use the normal postgres tooling like pg_dump.

Deploying the application to Heroku
Now time to deploy this application to Heroku's hosted applications solution. Go to the 'Deploy' tab in Cloud9 (fourth tab with the balloon) and choose '+'.

http://100procentjan.nl/tweakers/wp4.png

Choose Heroku, log in with your fresh credentials and create a new app. Give it any name you like and click 'Add'. Because Cloud9 enforces a Procfile which is not required for PHP apps at the moment, we now need to manually push instead of via the UI. In the console (SHIFT+ESC) type:

bash:
1
2
3
4
$ git remote add heroku git@heroku.com:APPNAME.git
$ git add .
$ git commit -m "Initial commit"
$ git push heroku master

http://100procentjan.nl/tweakers/wp5.png

And now your application has been deployed to the URL mentioned in the console message. For example: here is my brand new blog!

Updating the site
In case you want to edit the PHP files, just edit them in Cloud9; test them via the built in Apache process, and when you're ready do a 'git add' for the files you edited, commit them and push to heroku. Easy as that!

Like Cloud9?
We're open source, so if you have cool improvements that you'd like to contribute, see the GitHub page. The VMs terminal stuff are only available on the hosted version, but you can roll your own by install c9 on your own cloud VM (f.e. on Amazon).

Happy coding!

Volgende: Node.js's null terminator of death 11-'12 Node.js's null terminator of death
Volgende: Rate limited website scraping with node.js and async 04-'12 Rate limited website scraping with node.js and async

Reacties


Door Tweakers user truegrit, woensdag 11 juli 2012 19:32

I fiddled with c9 in the past for a bit, but i was unaware it had so much possibilities. I shall have to try it out some day

Door Tweakers user Vold, woensdag 11 juli 2012 20:16

I didn't knew you could deploy php apps to Heroku with such ease! I used cloud9 a while ago, but I thought it was too buggy back then, I looks way more stable now!

How is the current support for mobile devices like the iPad? It would be perfect if I could edit on the road all my stuff. :)

Door Tweakers user creator1988, woensdag 11 juli 2012 23:28

truegrit schreef op woensdag 11 juli 2012 @ 19:32:
I fiddled with c9 in the past for a bit, but i was unaware it had so much possibilities. I shall have to try it out some day
Vold schreef op woensdag 11 juli 2012 @ 20:16:
I didn't knew you could deploy php apps to Heroku with such ease! I used cloud9 a while ago, but I thought it was too buggy back then, I looks way more stable now!
Great! We've made some incredible improvements in the past week, so I'd love to hear your feedback.
How is the current support for mobile devices like the iPad? It would be perfect if I could edit on the road all my stuff. :)
Tablet device support is not in there at the moment, can't give a timeline for it either a.t.m. But it's something that'll definitely land in the near future.

Reageren is niet meer mogelijk