Setup a Development Web Server for PHP, MySQL and Ruby on Rails

In this blog post you’ll learn how to turn that old computer you have lying around into a full-featured test bed for your websites. We’ll set up a typical LAMP server with Ubuntu Server 12.10 using Apache 2, PHP 5 and MySQL 5.5, and add in support for your Ruby on Rails 3 apps!

Get the latest version of Ubuntu Server (12.10 at time of writing) and burn it to a disk (or mount it in your VM software if you’re running a Virtual Machine).

Start up from disk, select your language then “Install Ubuntu Server“. Continue through the pages following the on-screen instructions. Enter a host name for your server when prompted. Something like web-server will do, and continue through partitioning your disk for install.

When asked, enter your full name, then a username. Make sure you enter a strong password. When asked to specify what software packages to install just press enter without selecting any packages.

We could have selected LAMP Server and OpenSSH Server which would install Apache, MySQL, PHP, OpenSSH and a few related dependencies automatically, but we’re going to do it manually to learn how it’s done.

When asked, ensure the installation disk has been ejected and restart to boot up Ubuntu.

You should be presented with a black screen and a login prompt. Enter your username and password when asked. When you type your password, you won’t see asterix’s or anything appear on screen to signify you typing, this is normal. Just enter your password and hit enter to log in.

You may have been expecting some sort of GUI interface, but as we installed the Server version of Ubuntu you get a command line instead :) A GUI just takes up unnecessary resources on a server –the shell is more than enough!

 

APT & the Shell

We can now start installing software packages to set up the web server. Installing software on Ubuntu is quite different compared to Windows or Mac systems. Ubuntu comes with a software package called APT, or Advanced Packaging Tool, which allows you to easily install software packages from online repositories.

Enter the following at the command prompt to first upgrade all software packages which are currently installed:
sudo apt-get upgrade

Enter y to any confirmation messages.

 

Sudo

By default on a UNIX-based system, you’re an ordinary user with no special powers. If you want to edit any settings or mess with system files you must become a Super User. This is done with the sudo command.

If you’re ever given a “Permission Denied” message when trying to run a command, prefix the command with sudo, enter your password when prompted and you’ll temporarily be given the Super User powers required to carry out the command.

 

LAMP

LAMP stands for “Linux, Apache, MySQL, PHP” –the most common web server configuration. This is the set up we’ll be going for.

Firstly, enter this command to install the Apache web server software:
sudo apt-get install apache2

Then, to install PHP simply run:
sudo apt-get install php5

And set up MySQL with the following:
sudo apt-get install mysql-server mysql-client

Here we’re installing two packages (mysql-server and mysql-client) at the same time to save time.

Next, you’ll be asked for a password for the root user in MySQL. Like your account password, make it something secure.

Finally, link MySQL with PHP and Apache by installing the following two libraries:
sudo apt-get install libapache2-mod-auth-mysql
sudo apt-get install php5-mysql

 

SSH

Secure Shell, or SSH, is a network protocol for accessing a UNIX shell remotely. For example, you could keep your web server hidden away without a monitor or keyboard attached and then use SSH to control the server remotely from your own computer.

To set up an SSH server, install the following package:
sudo apt-get install openssh-server

 

Ruby Essentials

Run the following to install the essential ruby packages:
sudo apt-get install build-essential zlib1g-dev ruby-full

Also, let’s install SQLite 3, Rails’ preferred database engine:
sudo apt-get install sqlite3 libsqlite3-dev

 

Ruby Version Manager

We’re going to use RVM to allow us to run multiple versions of Ruby on the same system. As Ruby 1.9.x was released recently, it’s common to also have 1.8.x installed to use with older code.

However before we can install RVM, we need to install the curl and git libraries:
sudo apt-get install curl git

Now install RVM with the following:
bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )

Finally, you need to add one line to the bottom of your .bashrc file. Open this file by running:
nano ~/.bashrc

Then add the following line to the bottom:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"

Save and quit out of the nano text-editor with Ctrl-O, hitting enter to confirm the save, then Ctrl-X to exit.

Now run this to avoid having to restart to use RVM:
source ~/.rvm/scripts/rvm

Now we can install Ruby 1.9.3 using RVM:
rvm install 1.9.3
Note that this will take a while to compile.

Once 1.9.2 is installed, you can set it as the default version of Ruby to use on your system with:
rvm use 1.9.2 --default

 

Ruby on Rails

To install Rails, you need the RubyGems package:
sudo apt-get install rubygems

Now you can use the gem command to install the latest version of Rails (3.0.3 at time of writing):
gem install rails

 

Let's See It In Action!

So now we have all the relevant software packages installed, let's see the server in action! Run the following command to get the local IP address of the server:
ifconfig
The IP is usually the first number resembling an IP displayed, after the inet label.

In a web browser on a computer on the same network as the server, enter the server's IP address and you should see Apache's "It Works!" page.

Apache stores your site files in the /var/www directory, which you can navigate to by running:
cd /var/www

By default, your user doesn't have permissions to edit this directory. Of course you could use nano when doing anything in this directory, but it'll be easier to just give ourselves permission:
sudo chown -R foksh /var/www
sudo chmod -R 755 /var/www

(change foksh with your username)

You can now edit files. Try this out by removing the default index file:
rm index.html

And creating your own:
nano index.html

Enter some content in here, save it and refresh your browser!

 

Working with Rails

So now we know LAMP is working. Let's try out Ruby and Rails.

You can store your Rails apps anywhere on your server, but it's best to keep them out of Apache's /var/www directory to keep your Rails sites separate. Instead, let's make a rails folder in our home directory:
cd ~
mkdir rails
cd rails

You can now create a new Rails app named "blog" by running:
rails new blog
cd blog

And the first thing you should do when setting up a new Rails 3 app is install the app's gem dependencies with:
bundle install

Finally, you can start the Rails server with:
rails s

Now if you go to http://serverip:3000 (port 3000) in a web browser, you should see Rails' welcome screen.

 

SFTP and SSH access

You should be able to access the server anywhere on your network through either SFTP or SSH to transfer files.

To SSH, simply run the following on any other UNIX-based system on your network:
ssh username@serverip

For example:
ssh foksh@11.215.50.10

You can also access the server through SFTP (note not FTP, but SFTP) by entering the server IP address and login details into your favourite client and ensuring you check "SFTP" mode.

 

Restart & Shutdown

You can restart your system with:
sudo shutdown -r now

Or shutdown with:
sudo shutdown -P now

 

 

 

Tags: , , , , , , , , , , , , , , , , , , ,

2 Responses to “Setup a Development Web Server for PHP, MySQL and Ruby on Rails”

  1. If you want to increase your knowledge just keep visiting this website and be updated with
    the most up-to-date gossip posted here.

  2. Rob says:

    Sweet, thanks! It would be good if you put up memory/processor requirements. I’m planning to use an AWS instance as a dev server and it would be good to know how big an instance I need.

Leave a Reply