Install Php Nginx Ubuntu 12.04


Install Php Nginx Ubuntu 12.04

Introduction

Welcome to our guide on How to install PHP, Nginx & MySQL on Ubuntu 12.04 LTS. This guide should work on other Linux VPS systems as well but was tested and written for an Ubuntu 12.04 VPS system. Here we will go through the steps required to install the respective software on Ubuntu 12.04.

Prerequisites

Before we install PHP, Nginx and MySQL on Ubuntu 12.04, we should update the repositories and packages to the latest version. This can be done by running the following command on the terminal:

sudo apt-get update
sudo apt-get upgrade

The above commands should update and install all the required packages for the installation of PHP, Nginx and MySQL on Ubuntu 12.04.

Installing PHP on Ubuntu 12.04

Once we have the prerequisites sorted out, we can move on to install PHP on Ubuntu 12.04. To do this, we first need to install Php5 using the following command:

sudo apt-get install php5

The above command should install and setup all required components of PHP on Ubuntu 12.04. Once it is done, we can verify it by running the following command:

php -v

This should display the PHP version that is installed on the system. We will also need to install Php5-fpm in this step. To do that, we need to run the following command.

sudo apt-get install php5-fpm

This should install php5-fpm on the system and all related components. Once the installation in done, we can start and enable the service using the following command.

sudo service php5-fpm start
sudo update-rc.d php5-fpm defaults

Installing Nginx on Ubuntu 12.04

Once we have the prerequisites sorted out, we can move on to install Nginx on Ubuntu 12.04. To do this, we first need to add the Nginx repository by running the following command on the terminal:

sudo add-apt-repository ppa:nginx/stable

Once the repository is added, we can update the packages and repositories by running the following command:

sudo apt-get update

Finally, we can install Nginx on Ubuntu 12.04 by running the following command:

sudo apt-get install nginx

Once the installation is done, we can test and verify it by running the following command:

sudo nginx -t

This should output the Nginx version, and if everything is setup correctly it should say that the configuration file has been tested successfully.

Installing MySQL on Ubuntu 12.04

Once we have all the prerequisites setup, we can install MySQL on Ubuntu 12.04. To do this, we first need to add the MySQL repository by running the following command on the terminal:

sudo apt-get install mysql-server

Once the installation is done, we can test and verify it by running the following command:

sudo mysql -V

This should output the MySQL version, and if everything is setup correctly it should say that the server is running properly.

Making Nginx and PHP Work Together

Now that we have both Nginx and PHP setup on the system, we need to configure both to work together. To do this, we first need to create a configuration file in the Nginx directory. To do this, we need to run the following command:

sudo nano /etc/nginx/sites-available/php-fpm

In this file, we need to paste the following configuration:

server {
listen 80;
server_name example.com;

root /var/www/example.com;
index index.php index.html;

location ~ .php$ {
try_files $uri = 404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Once we have saved and enabled the configuration file, we can restart Nginx and PHP for the changes to take effect by running the following command:

sudo service nginx restart
sudo service php5-fpm restart

Conclusion

We have now successfully installed PHP, Nginx and MySQL on Ubuntu 12.04. We have also configured Nginx and PHP to work together. This should provide us with enough power and flexibility to run any web application that requires PHP, Nginx and My SQL.

FAQs

Q. How do I start PHP on Ubuntu 12.04?

A. You can start and enable the php5-fpm service using the command sudo service php5-fpm start followed by sudo update-rc.d php5-fpm defaults.

Q. How do I configure Nginx and PHP to work together?

A. You need to create a configuration file in the Nginx directory and add the configuration given in the article. After that you need to restart Nginx and PHP for the changes to take effect.

Q. How do I restart Nginx and PHP?

A. You can restart Nginx and PHP using the command sudo service nginx restart and sudo service php5-fpm restart.

Thank you for reading this article. Please read other articles for more information and updates.

Leave a Reply

Your email address will not be published. Required fields are marked *