Installing PHP on Debian without Apache

2 Aug 2013 • 1 minute read

When you apt-get install php5 on a Debian/Ubuntu server, you’ll notice that APT will automatically install a bunch of apache2 packages as well. This can be pretty annoying if you’re planning on using another web server (or no web server at all).

If you take a look at the package dependencies (Debian/Ubuntu) you’ll see why this happens - php5 needs one of either libapache2-mod-php5, libapache2-mod-php5filter, php5-cgi, or php5-fpm. APT doesn’t care which package it installs; it just picks the first package that satisfies the dependency, which is why you get the apache2 packages.

You can get around this by installing one of the other dependencies before php5. For example, apt-get install php5-fpm php5 or apt-get install php5-cgi php5.

(Read more)

Using SSH agent forwarding with Vagrant

1 Aug 2013 • 1 minute read

Sometimes you’ll want to use your local SSH keys on your Vagrant boxes, so that you don’t have to manage password-less keys for each box. This can be done with SSH agent forwarding, which is explained in great detail on Unixwiz.net.

Setting this up is fairly straightforward. On the host machine, you need to add the following to ~/.ssh/config (which you should create if it doesn’t exist):

host your.domain.com
    ForwardAgent yes

You need to replace your.domain.com with either the domain or the IP address of your Vagrant box. You can wildcard this with host *, but this is a really bad idea because it lets every server you SSH to access your keys.

Once you’ve done that, just run ssh-add to ensure you ensure your identities are added to the SSH agent.

Now, add the following to the config block in your Vagrantfile:

config.ssh.forward_agent = true

That’s all it takes. You can make sure it worked by comparing the output of ssh-add -L on both the host machine and the guest box.

(Read more)

Blazing fast WordPress with Nginx and Memcached

21 May 2013 • 7 minute read

Inspired by Eric Mann’s post on caching WordPress with Redis, I thought I’d experiment with a similar setup using Memcached. Any in-memory caching system should work just as well, but I’ve chosen Memcached because it’s already running on my server and because PHP already has a built-in libmemcached API.

My current setup is Nginx and PHP-FPM, with WP Super Cache. The cache is saved to the filesystem, allowing Nginx to serve static files (which it is very good at) without needing to pass any requests to PHP. This setup has worked very well, so I’ll be using it as a baseline.

To use Memcached, every request needs to be passed to PHP. My gut feeling was that this would be slower than serving static files with Nginx due to the overhead of spinning up a PHP process for each request.

Benchmarks

To find out which of the two setups was faster, I measured the following metrics using WebPagetest and Blitz (referral link):

(Read more)

Increasing the size of the LISH console

20 May 2013 • 1 minute read

If you’ve used Linode’s LISH console to get remote access to your server, you’re probably familiar with the way the console wraps everything to 60x20 (columns x rows) - even when you’re connected via ssh in a much larger terminal.

(Read more)

Solving "502 Bad Gateway" with nginx & php-fpm

23 Sep 2012 • 1 minute read

After upgrading php-fpm, my PHP-based sites were returning “502 Bad Gateway” errors. This can happen when the php5-fpm package reconfigures itself to listen on a different socket. Here’s how you can solve it.

Check to make sure that php-fpm is running with ps aux | grep php - if you can’t see any php-fpm processes in the output, then you may need to re-install php-fpm. If php-fpm is running okay, then skip this first step.

sudo apt-get remove php5 php5-cgi php5-fpm
sudo apt-get install php5 php5-cgi php5-fpm

The thing to notice here is that the order in which you install the packages is important. In the past I have found that installing them in the wrong order causes the packages to be configured incorrectly.

Next, get php-fpm to listen on the correct host/port. In /etc/php5/fpm/pool.d/www.conf change the listen value to match the fastcgi_pass location in your Nginx configuration. For example, I changed mine from:

listen = /var/run/php5-fpm.sock

To:

listen = 127.0.0.1:9000

If you are configuring php-fpm to listen on a Unix socket, you should also check that the socket file has the correct owner and permissions. While I wouldn’t recommend it, you can simply give read-write permissions to all with sudo chmod go+rw /var/run/php5-fpm.sock.

Restart php-fpm with sudo service php5-fpm restart and everything should work normally again.

(Read more)

Automatic MySQL Backups

4 May 2012 • 1 minute read

It’s really easy to set up automatic MySQL backups using mysqldump. First, you need to set up a user with SELECT and LOCK TABLES privileges. In this example the user doesn’t have a password.

CREATE USER 'autobackup'@'localhost';
GRANT SELECT, LOCK TABLES ON *.* TO 'autobackup'@'localhost';

Next create the cron job with crontab -e. This job is set to run every day at 5:20am.

20 5 * * * mysqldump --user=autobackup dbname | gzip -c > /var/backups/dbname-$(date +\%Y\%m\%d).sql.gz

Don’t forget to change dbname to the name of the database that you want to backup. And that’s it - you’re done! This cron job will create a backup of your database and save it to /var/backups with a filename based on the current date, e.g. /var/backups/dbname-20120503.sql.gz

(Read more)

Deploying a Git repository to a remote server

21 Aug 2011 • 1 minute read

Git’s archive command is basically the equivalent of SVN’s export – it dumps a copy of the entire repository without any of the version control files, making it perfect for deploying to a testing or production server.

(Read more)

Setting up a Web Server on Ubuntu/Debian

23 Jul 2011 • 2 minute read

Setting up a web server with Apache, PHP, and MySQL on any Debian-based system is really easy thanks to APT (Advanced Packaging Tool). Follow along and you’ll have a web server set up within fifteen minutes.

(Read more)

Recursively chmod Directories Only

23 May 2011 • 1 minute read

The find utility’s -exec flag makes it very easy to recursively perform operations on specific files or directories.

find . -type d -exec chmod 755 {} \;

This command finds all directories (starting at ‘dot’ - the current directory) and sets their permissions to 755 (rwxr-xr-x).

find . -type f -exec chmod 644 {} \;

Similarly, this command finds all files and sets their permissions to 644 (rw-r–r–).

Thanks to moveabletripe for the info.

(Read more)

Dynamic Virtual Hosts Using .htaccess

21 May 2011 • 1 minute read

There are several ways to set up virtual hosts on your web server. One of the more common methods is to manually create a [<VirtualHost>](http://httpd.apache.org/docs/2.0/mod/core.html#virtualhost) record for each virtual host. While using this method is fine, it can end you up with a huge configuration file that is difficult to manage.

Because all of my virtual hosts are sub-directories of my web server’s base directory, I prefer to dynamically allocate the virtual host directory based on the host name. For example, I want wildlyinaccurate.localhost to point to /var/www/wildlyinaccurate. This can be achieved by modifying the .htaccess file of your web server’s base directory:

(Read more)