Tag Archives: php

Setting up a new Rackspace Cloud Server with Debian 6, Name.com, and PHP/MySQL

Another instructional blog post as I try to figure out something.

Configuring a website with Rackspace.com is much different than setting one up with a shared hosting service such as Dreamhost or GoDaddy.  I could write more about this, but it’s not really productive.

Anyways, here are the steps I went through:

1. Buy a domain.

I chose Name.com, for their cheap prices and no-bullshit/non-shady website (e.g. GoDaddy).  For $8.25/month (after coupon code), you get a .com domain.

2. Configure Google Apps

With Name.com, this is very simple, there’s a button for it.  After that, follow Google’s very detailed instructions.  I chose this option instead of configuring mail on my own server because it’s simpler, and a very pretty interface.  Plus, all the people working with me on the site already have gmail.

3. Set up a Rackspace.com Cloud Server Account

Pretty simple, not many options to choose.  At first, all you need will be a cloud server.

4. Set up your server on Rackspace.com

I went with a Debian 6.0 server with 512MB of RAM after about 10 minutes of internet research.  I have some experience with CentOS, Ubuntu, and Fedora, but the internet said “Debian” so I went with that.

5. Set up DNS on Name.com

First, delete the “A” DNS records on name.com.  These can be found under “Domain Management” » “DNS Record Management.”  Next, add a new “A” record with “*” in the Record Host field and your Rackspace IP in the Record Answer field.  Add another one with a blank Record Host field and the same IP.

6. Log in to your server as root, install everything

Using the provided root password from Rackspace, open up a terminal (on Windows use PuTTY).  Enter
#apt-get install apache2 php5 libapache2-mod-php5 mysql-server mysql-client php5-mysql
At this point, your DNS information will take a little while to propagate across the internet.  However, if you type your url into your address bar, you should get a page that says “It works!” (As of Debian 6).
Missing data…
Next add the following to your .htaccess for compression and www-removal.  Make sure to change “domain.com” to your domain.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

Making dynamic SQL queries from Javascript with PHP

Impossible? No!

Just add some AJAX to your <head> section:
<script type="text/javascript">
function request(query){
if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
if (xmlhttp.readyState==4 && xmlhttp.status==200){
//it worked.
alert(xmlhttp.responseText); //this is the output of your query.php file
}else{
//whatever happens when it doesn't work
alert('Query did not work');
}
xmlhttp.open("GET","query.php?q="+query,true);
xmlhttp.send();
}
</script>

and create the new page query.php with the following content, replacing the default values where necessary:

<?php
$dbhost = '#REPLACE THIS#';
$dbuser = '#REPLACE THIS#';
$dbpass = '#REPLACE THIS#';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
$dbname = '#REPLACE THIS#';
mysql_select_db($dbname);
$query=$_GET["query"];
$result = mysql_query($query);
//now use mysql_fetch_assoc($result) or something similar to get the returned result if applicable.
//if you echo a value, the javascript ajax will receive it
?>

Using this code, you can make whatever kind of SQL queries you want from other sections of your site.

Warning: Unless you sanitize your database inputs you leave yourself open to some huge vulnerabilities.