Blog/ How to install Drupal 7 with Nginx, PHP-FPM and MySQL on Ubuntu 14.04

Ubuntu 14.04's package manager (apt) installs PHP 5.5 by default. Although Drupal 7 core works on PHP 5.5, some contributed modules may not. In that case, you may either need to fix the module or downgrade to a lower version of PHP. In this article, we will show you how to install PHP 5.3.29 with PHP-FPM from source on Ubuntu 14.04. On top of this, we will install Nginx and MySQL to have a working Drupal 7 website.

1) Get the latest package information

sudo apt-get update

2) Install and configure Nginx

sudo apt-get install nginx

The above command will install nginx. Now you need to configure it. Assuming that your Drupal root directory is going to be /usr/share/nginx/html, open the file /etc/nginx/sites-available/default, delete everything in it and paste the following configuration in it.

Above command will run automated tests. In all the installations that I have done, I have seen that 0.5% of tests fail. That is fine. I have not seen any error while running Drupal because of these failures. Now install PHP.

sudo make install

The above command will install PHP in /opt/PHP-5.3 directory. PHP-FPM is installed in /opt/PHP-5.3/sbin folder. First let's enable the default configuration file.

You can start PHP-FPM by executing the command sudo /opt/PHP-5.3/sbin/php-fpm but this is not recommended as if the server reboots because of any reason, PHP-FPM won't start automatically. Let's convert PHP-FPM to a service so that reboot of the server will automatically start the PHP-FPM process.

Now execute the following command so that PHP-FPM will be started automatically after the service reboots.

sudo update-rc.d php-fpm defaults

Reboot the server. Once the server is back up, execute the following command:

ps aux | grep php-fpm

You should be able to see php-fpm processes that were started automatically.

Now go to the server using the browser and you show see the following error:

This means that Nginx and PHP-FPM are working well together. You are seeing this error because there is no index.php file in /usr/share/nginx/html folder. Let's create an index.php file in /usr/share/nginx/html folder and put the following content in it:

<?php
phpinfo();
?>

Now go to the browser again and reload the webpage. You must be able to see the PHP configuration similar to the image below:

There is one more thing that we need to do that will come in handy later. We need to put PHP in executable path. Execute the following command:

sudo echo "PATH=/opt/PHP-5.3/bin:\$PATH" >> ~/.bashrc

Execute the following command to make sure that PHP is not in your executable path:

Congratulations! Most difficult part of installation is now over. You have installed Nginx with PHP-FPM on your server.

4) Install MySQL

sudo apt-get install mysql-server mysql-client

You will be asked to provide root password for MySQL. Please provide a secure password. Then execute mysql_secure_installation command to make your MySQL installation even more secure.

[email protected]:/usr/share/nginx/html# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] n
... skipping.
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] Y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] Y
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] Y
- Dropping test database...
ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist
... Failed! Not critical, keep moving...
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!

Now we need to create a database in the MySQL server that Drupal can use. Log into the MySQL admin console using the command:

mysql -u root -p

You will be asked for a password. Once you are logged in, execute the following queries to create a database named "drupal" that is accessible by user "drupaluser" with password "drupalpassword".

Now go to the server using the browser. You will see the screen to install Drupal. In the first step, install the profile that you want. We selected the Standard profile.

In the second step, choose the language in which you want to build the site. By default, you will get only one choice: English. Select that and proceed to the next page.

In the next step, you need to provide Drupal with the database access information. This is the name of the database that you created for storing Drupal's data. In this example, the database we created was named "drupal". We also need to provide Drupal with the username and password for the MySQL user who has access to this database. In this example, we provide "drupaluser" and "drupalpassword". Note that you could have provided "root" and the root password but it is not recommended since it's a security risk.

Now Drupal will install the installation profile you selected and then will ask you to fill in the details about the new site that you are creating. On top of that page, it will ask you to secure the access to settings.php file.

So let's go back to the terminal in the server and change its permissions.

cd /usr/share/nginx/html/sites/default
sudo chmod 644 settings.php

Now go back to the browser and refresh the page. The above warning should go away. In the form, enter details about the site and the admin and go to the next page.

Voila! Your Drupal installation is complete and you should see a screen informing you of the same.

Click on the link "Visit your new site" to go to the site's homepage.

That was easy, wasn't it? Please feel free to add comments below if you have any suggestions or questions about this blog post.

Comments

FYI, Drupal 7 core works just fine with PHP 5.5, and most of my D7 infrastructure is running on 5.5 on either Ubuntu or CentOS without issue. There are a few bad contrib modules that cause issues, but all the major (top 100 or so) ones seem to work just fine.