Serving PHP with Nginx on Ubuntu

For PHP developers, the ideal stack is Linux, Apache, MySQL and PHP (a LAMP server). However, being a Ruby developer, I tend to prefer Nginx over Apache, as well as Postgres over MySQL, but that’s another post.

Nginx is gaining against IIS and Apache because it’s very good at being a web server and uses fewer resources to do its job. And serving PHP with Nginx is easy to do, too. While most guides use custom FastCGI spawners and scripts, serving PHP with Nginx can be done even quicker and easier with mostly apt-get installed software.

Note: This was written for Ubuntu 12.04 and PHP 5.

Server Foundation for Ubuntu LTS

This is a guide roughly outlining how I would manually setup and configure a server after the first boot. I tend to go with Ubuntu LTS (long-term support). Ubuntu is a popular distribution with a lot of library support, and the LTS version is supported for a longer period of time than the current releases.

Mobile Redirection with Nginx

Mobile visitors shouldn’t have to hit your desktop web application just to be redirected to your mobile web application. You can use Nginx to handle the redirect before it spins up all the resources of your web application.

Where I work, our desktop application automatically redirects visitors on mobile devices to its mobile counterpart. It was initially handling the checking and redirecting in the Ruby on Rails application using an implementation that was like the one shown in Railscast Episode 199. However, we have since made the mobile website a separate application and put it on its own subdomain, so I wanted to move the redirection logic out of the main Rails app so mobile users never hit the desktop application.

Previous
Archives