Technical Adventure du Jour: Setting Up LAMP on Ubuntu From Scratch
So yesterday I decided that I was going to distract myself a bit by moving all my websites to a Digital Ocean “droplet” (virtual machine – why does everyone need to find a cute name for the same idea? Get off my lawn, you stupid kids!)
I’ve done a Ubuntu setup of the LAMP stack a few times now, but everytime I forget the little gotchas, so in case someone else out there is doing this, here are a few tips:
To start with, let’s assume you’re setting yourself up to run an Apache/MySQL/PHP website – say, Drupal or WordPress. Also, I’ll assume you have Ubuntu 12.04. A later version might have some differences in the commands listed below but I doubt it.
I’ll also assume that you know how to setup a droplet (VM) on Digital Ocean (DO), and have figured out how to login to the console/shell as root.
Before you do anything on your machine, create a user other than root, that you will be operating as. You should never login as root to do most tasks – you should instead give your user “sudoer” privileges:
adduser –home /home/adminuser –shell /bin/bash –ingroup sudo adminuser
Log out as root and log back in as the user you just created (‘adminuser’, in the above example.) All future users who need to have sudo privileges should be similarly added to the sudo group.
You are now ready to set up your AMP stack (run all these with sudo):
apt-get install apache2
apt-get install php5
# Remember to set a password for root when installing MySQL
apt-get install mysql-server
apt-get install php5-mysql
apt-get install sendmail
service apache2 restart
Once you have MySQL installed, you’ll need to configure it with at least one user that will be accessing your databases. Let’s call this user wp_user:
shell$> mysql -u root -p # You should have set a password for root when installing MySQL mysql> create user 'wp_user'@'localhost' identified by 'wp_password'; mysql> create database your_db; mysql> grant all on your_db.* to 'wp_user'@'localhost';
Make sure you use a better password than that! Now log back in as wp_user and you can check that the database is accessible to you.
shell$> mysql -u wp_user -p your_db
If all’s well, you shouldn’t see any errors when you run the mysql client as wp_user.
Bonus 1 – Migrating a WordPress installation!
This takes multiple steps.
Step 1: Prepare Apache
Before you bring over your WordPress install, make sure your fresh, target, server has Apache configured right – you’ll need to do three things:
- Enable the rewrite module: a2enmod rewrite
- Enable the expires module: a2enmod expires
- Allow your .htaccess to be read, by editing your Apache conf file (usually /etc/apache2/sites-available/default) to contain the following lines:
</span> <Directory /var/www/> Options FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory>
Note that some of the lines above might be in your configuration file already so edit it accordingly.
Step 2: Migrate your database
If you already have a WordPress site somewhere, you can easily migrate it using command-line tools. Note though that this bonus section doesn’t handle dealing with a change in the domain name – just a change in the location of your domain.
Use mysqldump on the source site to generate a dump of the database:
shell$> mysqldump –user=user_source –host mysql.source_db_server.org –password=’my source pass’ source_db_name > mysqldump.sql
Transfer this dump file to your target server, and run the mysql client:
shell$> mysql -u wp_user -p your_db < mysqldump.sql
Et voila! Your DB has been migrated.
Step 3: Transfer your files
Now migrate the files, remembering to tar/zip your dot-files as well, because you need to migrate the .htaccess files.
Bonus 2 – Setting up SSL on your Apache Server
This is complicated enough that it’s worth writing a new blog post for it.
Bonus 3 – Making Swap Files, Postgres and more!
If you have a droplet with less than a gig of memory, you should think about setting up a swap file, of at least 512 MB (this works for Ubuntu 12).
If you want to install Postgres, here’s a good blog post (outside this blog) that tells you how to configure and install Postgres in a variety of environments.