Setting up Nginx with PHP took some serious effort few months ago. It’s much easier now. It shouldn’t take 10 minutes to set up fully functional web server with Nginx and PHP5. This tutorial shows you how to properly setup Nginx server with PHP, MySQL and
This tutorial was tested and worked on
Ubuntu 12.04 LTS Ubuntu 14.04. It may work on other Ubuntu versions as well. But I don’t expect to test it on other versions as Ubuntu 14.04 LTS is the latest version. This is a basic setup and it’ll need further configurations to support some CMS such as WordPress or tracking software like Prosper202.
This setup is meant to be deployed on an unmanaged VPS, not on a managed or cPanel enabled server. If you don’t have an unmanaged VPS ready to go, please read Best Unmanaged VPS Hosting Providers for suggestions.
Prepare the Server
We’ll start by removing some packages from server which we don’t use. Run following command on CLI logged in as root.
sudo apt-get purge apache2* bind9* samba*
Update and upgrade the server,
sudo apt-get update
Press Y and hit Enter when asked.
Install PHP-FPM and
We’ll now install PHP 5.5.9 on the server. It’s the latest version of PHP (4th October. 2014) and is the fastest version yet. Ubuntu 14.04 comes with PHP 5.5. So we can install it directly from apt. Run following command to install PHP-FPM,
APC Cache and other essential PHP modules.
sudo apt-get install php5-mysql php5-xmlrpc php5-cgi php5-curl php5-gd php5-cli php5-fpm php-pear php5-dev php5-imap php5-mcrypt
Confirm it when asked by hitting Y. After few seconds, PHP will be up and running on your server.
Now that we have PHP running on our server, next step is to install MySQL. All major CMS and web software use MySQL as database. It’s a must have for any server. Following command will install MySQL and mysqltuner on your server.
sudo apt-get install mysql-server sudo mysql_install_db
Again, you’ll be asked to confirm the installation. You’ll also ask to set up MySQL root password during setup. Do it as asked. MySQL will also be up and running in few seconds.
It’s a nice idea to secure the MySQL installation,
This will ask you few questions. Type N when it ask you to change MySQL root password as you just set it up. Then Just hit Enter to all other prompts to secure the MySQL installation.
Now We’ll install Nginx on the server. Rather than dealing with third-party repo’s, I find it easy to go with Ubuntu 14.04’s built in package. Which is nginx/1.4.6 (Ubuntu) at the moment of writing.
sudo apt-get install nginx
Nginx does not support longer domain names by default. If you have a bit longer domain, you’ll need to edit Nginx configurations to support it. I think it’s better to do it now than later. Open Nginx main configuration file,
sudo nano /etc/nginx/nginx.conf
Look for following line within the file and uncomment it (Delete # mark).
# server_names_hash_bucket_size 64;
It’s should look like this after uncommenting,
Save file and exit nano text editor (Ctrl+X). Then restart Nginx,
sudo service nginx restart
Nginx is now successfully installed on your server. Open your Server’s IP address in web browser to confirm it. You’ll see Nginx default web page saying “Welcome to nginx!”
Now you can install whichever CMS you like on your server. Please note that some CMS like WordPress have special requirements. If you’re planning to host multiple websites on this server, you must create server blocks (Virtual Hosts) for each site. I’ll cover it on CMS specific tutorials.