We have a few production servers at work, and we have a central bitbucket repository to store our core code. Once we make a change on our testing server (!), we used to have to commit, push changes to bitbucket, and then ssh into each server, then pull changes, and update each repository. A pain in the backside! Enter Capistrano. A ruby ssh automation tool. In a few simple steps you can create a recipe file that will let you do this all with one command.
SSH and public key authentication
Fed up with having to type your password in each time you log into a server over SSH? Me too. Down with passwords, and in with public key authentication!
Using o2 PAYG mobile broadband
Our internet connection via our landline has been dead since Tuesday afternoon, so I’ve needed an alternative connection in the meantime to give me my twitter fix!
I’d kept an eye on the o2 pay-as-you-go mobile broadband for a while, because it offered the cheapest, noncommittal setup. A one off £20 for the USB dongle, and then as little as £2 for 500MB for a 24 hour period.
So I plug in the dongle and we get a nice little installer to set everything up; installing drivers and the o2 Mobile Connect application. The annoying thing is that if you install going this route, you can only connect if you have this app open. You can’t just use system preferences. Also, turns out this ‘handy’ installer program then blocks you from getting to the installation files again, and the dongle will refuse to do anything unless Mobile Connect is open (you’ll get a red light on the dongle when it’s being evil).
So, what can you do?
Redirecting Apache traffic to a maintenance page
Here’s a simple solution to redirect users to a maintenance page in Apache. This rewrite rule can stay in your config (<VirtualHost> or .htaccess) all the time; all that you need to enable it is to create the file maintenance.html.
The first rewrite condition checks to see if the file exists, and only if it does, will it redirect all traffic to it. The second rewrite condition is there to prevent an infinite loop, by only redirecting traffic to files other than maintenance.html.
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/maintenance.html -f
RewriteCond %{REQUEST_FILENAME} !/maintenance.html
RewriteRule ^.*$ /maintenance.html [L]
PHP and nginx on Ubuntu: the easy way
I’ve now changed my slice from running lighttpd to nginx. Here’s the simplest way, in around 6 commands, to get PHP up and running via FastCGI.
Install and setup
Install PHP 5:
sudo aptitude install php5-cgi
Install nginx:
sudo aptitude install nginx
Create PHP 5 FastCGI start-up script:
sudo nano /etc/init.d/php-fastcgi
Inside, put:
#!/bin/bash
BIND=127.0.0.1:9000
USER=www-data
PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000
PHP_CGI=/usr/bin/php-cgi
PHP_CGI_NAME=`basename $PHP_CGI`
PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND"
RETVAL=0
start() {
echo -n "Starting PHP FastCGI: "
start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS
RETVAL=$?
echo "$PHP_CGI_NAME."
}
stop() {
echo -n "Stopping PHP FastCGI: "
killall -q -w -u $USER $PHP_CGI
RETVAL=$?
echo "$PHP_CGI_NAME."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
Make start-up script executable:
sudo chmod +x /etc/init.d/php-fastcgi
Launch PHP:
sudo /etc/init.d/php-fastcgi start
Launch at start-up:
sudo update-rc.d php-fastcgi defaults
That’s it. All installed and ready to go.
Test
In your server config, add the following:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_name;
include fastcgi_params;
}
Restart nginx:
sudo /etc/init.d/nginx restart
Create a file in your web root (in the example above, /var/www/nginx-default/test.php):
<?php
phpinfo();
Visit the page in your browser and you should see the standard PHP info page. And you’re done.
Source: Aberration
Authentication with CodeIgniter
Here’s how I do some basic authentication for a controller in CodeIgniter. It basically consists of creating a new class that extends the default Controller class. You then sub-class this on any controller that requires authentication.
Hide GIT or SVN files with lighttpd
Add this to the bottom of your lighttpd.conf file:
$HTTP["url"] =~ "/\.(git|svn)/" {
url.access-deny = ( "" )
}