Tutorial

How To Install Linux, nginx, MySQL, PHP (LEMP) stack on Ubuntu 12.04

Published on June 13, 2012
How To Install Linux, nginx, MySQL, PHP (LEMP) stack on Ubuntu 12.04
Not using Ubuntu 12.04?Choose a different version or distribution.
Ubuntu 12.04

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

About Lemp

LEMP stack is a group of open source software to get web servers up and running. The acronym stands for Linux, nginx (pronounced Engine x), MySQL, and PHP. Since the server is already running Ubuntu, the linux part is taken care of. Here is how to install the rest.

Setup

The steps in this tutorial require the user to have root privileges. You can see how to set that up in the Initial Server Setup Tutorial in steps 3 and 4.

Step One—Update Apt-Get

Throughout this tutorial we will be using apt-get as an installer for all the server programs. On May 8th, 2012, a serious php vulnerability was discovered, and it is important that we download all of the latest patched software to protect the virtual private server.

Let's do a thorough update.

sudo apt-get update

Step Two—Install MySQL

MySQL is a powerful database management system used for organizing and retrieving data

To install MySQL, open terminal and type in these commands:

sudo apt-get install mysql-server php5-mysql

During the installation, MySQL will ask you to set a root password. If you miss the chance to set the password while the program is installing, it is very easy to set the password later from within the MySQL shell.

Once you have installed MySQL, we should activate it with this command:

sudo mysql_install_db

Finish up by running the MySQL set up script:

sudo /usr/bin/mysql_secure_installation

The prompt will ask you for your current root password.

Type it in.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Then the prompt will ask you if you want to change the root password. Go ahead and choose N and move on to the next steps.

It’s easiest just to say Yes to all the options. At the end, MySQL will reload and implement the new changes.

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...
 ... Success!
 - 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...

Once you're done with that you can finish up by installing PHP.

Step Three—Install nginx

Once MySQL is all set up, we can move on to installing nginx on the VPS.

echo "deb http://ppa.launchpad.net/nginx/stable/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/nginx-stable.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys C300EE8C
sudo apt-get update
sudo apt-get install nginx

nginx does not start on its own. To get nginx running, type:

sudo service nginx start

You can confirm that nginx has installed an your web server by directing your browser to your IP address.

You can run the following command to reveal your VPS's IP address.

ifconfig eth0 | grep inet | awk '{ print $2 }'

Step Four—Install PHP

To install PHP-FPM, open terminal and type in these commands. We will configure the details of nginx and php details in the next step:

sudo apt-get install php5-fpm

Step Five—Configure php

We need to make one small change in the php configuration.Open up php.ini:
 sudo nano /etc/php5/fpm/php.ini

Find the line, cgi.fix_pathinfo=1, and change the 1 to 0.

cgi.fix_pathinfo=0

If this number is kept as 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative. Save and Exit.

We need to make another small change in the php5-fpm configuration.Open up www.conf:

 sudo nano /etc/php5/fpm/pool.d/www.conf

Find the line, listen = 127.0.0.1:9000, and change the 127.0.0.1:9000 to /var/run/php5-fpm.sock.

listen = /var/run/php5-fpm.sock

Save and Exit.

Restart php-fpm:

sudo service php5-fpm restart

Step Six—Configure nginx

Open up the default virtual host file.

sudo nano /etc/nginx/sites-available/default

The configuration should include the changes below (the details of the changes are under the config information):

UPDATE: Newer Ubuntu versions create a directory called 'html' instead of 'www' by default. If /usr/share/nginx/www does not exist, it's probably called html. Make sure you update your configuration appropriately.

 [...]
server {
        listen   80;
     

        root /usr/share/nginx/www;
        index index.php index.html index.htm;

        server_name example.com;

        location / {
                try_files $uri $uri/ /index.html;
        }

        error_page 404 /404.html;

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
              root /usr/share/nginx/www;
        }

        # pass the PHP scripts to FastCGI server listening on the php-fpm socket
        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;
                
        }

}
[...]

Here are the details of the changes:

  • Add index.php to the index line.
  • Change the server_name from local host to your domain name or IP address (replace the example.com in the configuration)
  • Change the correct lines in “location ~ \.php$ {“ section

Save and Exit

Step Seven—Create a php Info Page

We can quickly see all of the details of the new php configuration.

To set this up, first create a new file:

sudo nano /usr/share/nginx/www/info.php

Add in the following line:

<?php
phpinfo();
?>

Then Save and Exit.

Restart nginx

sudo service nginx restart

You can see the nginx and php-fpm configuration details by visiting http://youripaddress/info.php

Your LEMP stack is now set up and configured on your virtual private server.

See More

After installing LEMP, you can Install WordPress, go on to do more with MySQL (A Basic MySQL Tutorial) or Install phpMyAdmin, Create an SSL Certificate, or Install an FTP Server.

By Etel Sverdlov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Etel Sverdlov
Etel Sverdlov
See author profile
Category:
Tutorial

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
160 Comments
Leave a comment...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

“Uncomment the correct lines in “location ~ .php$ {“ section” —

Can you Give little eloboration on this. What settings needs to uncomment. I am getting. 502 bat gateway error. whil accessing a PHP page.

Thanks in advance

My bad. Typo… You can delete my comments. it worked.

Moisey Uretsky
DigitalOcean Employee
DigitalOcean Employee badge
July 24, 2012

No problem, glad you got it working =]

Hello,

I configured my servet just like this but my server is not behaving as expected when I try to access a URL that doesn’t exist.

If I try to access http://myserver/3i1idaosdj (a file that does not exist), I get a 500 Internal Server Error.

If I try to access http://myserver/3i1idaosdj.php (a file that does not exist and the URL ends with .php), I get a message saying “No input file specified”.

Accessing the error log, I got the following message:

2012/08/08 03:18:45 [error] 6699#0: *1 rewrite or internal redirection cycle while internally redirecting to “/index.html”, client: my ip, server: localhost, request: “GET /3i1idaosdj HTTP/1.1”, host: “my server ip

Also, I configured the 404 error page in /etc/nginx/sites-available/default, via:

error_page 404 /404.html;

And 404.html is properly placed in the website folder. Why this is not working?

This comment has been deleted

    Moisey Uretsky
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 8, 2012

    In the nginx config make sure you updated: server_name example.com;

    And replaced example.com with your actual domain name and then test it through the web.

    If you just have an IP and not a domain name you can use that as well.

    If you’re still having a problem paste us your nginx config and we can help go through it.

    Thanks

    Is there a different HOWTO for PHP7 FPM?

    Here is my /etc/nginx/sites-available/default code:

    http://d.pr/n/KgJt

    Moisey Uretsky
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 8, 2012

    You have a small typo near the end :

    location ~ /.ht { deny all; } }

    It should be:

    location ~ /.ht/ { deny all; } }

    However, this wouldnt be the cause for the issues you were having. I did test the URL via the IP for both a non-existent file, received the 404, and a non-existent file ending in .php and got good 404 errors for both.

    Visiting the IP directly I did get the phpinfo() page, I’m assuming you made an index.php?

    If that’s the case I don’t really see what issue you are having unless you are uploading php files and they aren’t displaying or if you are uploading static content and having an issue.

    What URL are you hitting that you should see something and you dont? And can you show me the file listing in: /srv/www/test/

    Thanks.

    raiyu, thanks for the help, I solved the problem today, that’s why you got the 404 working.

    Here is my modified /etc/nginx/sites-available/default code. Hope it helps in case that someone have the same issue:

    http://d.pr/n/IRuL

    Hello. Your tutorial is good. I want to change the current document root directory to “home/user101/box/devs” How can I do that? I’m new to Nginx. Thanks and more power!

    Moisey Uretsky
    DigitalOcean Employee
    DigitalOcean Employee badge
    October 18, 2012

    Inside the server { } directive there is a root directive that which specifies where to serve files from:

    server { root /home/user101/box/devs; }

    i try everyting this is fun but really boring;

    500 Internal Server Error

    nginx/1.1.19 all the time.

    but still better than regular hosting. i am going old school LAMP :D

    Moisey Uretsky
    DigitalOcean Employee
    DigitalOcean Employee badge
    November 24, 2012

    Check your error log and paste why its returning a 500 error for you.

    For security reasons any error will return 500 publicly so as not to distribute any sensitive error messages to the public internet.

    It could be something as simple that the PHP script you are executing is being executed but is simply failing because of a code issue.

    I’m getting a 502 gateway error…

    [error] 15479#0: 1 connect() failed (111: Connection refused) while connecting to upstream, client: ...*, server: 141.0.170.212, request: “GET /info.php HTTP/1.1”, upstream: “fastcgi://127.0.0.1:9000”, host: “141.0.170.212”

    Seems to me that the server is refusing connections to port 9000, but I’ve not made any changes to disallow this (nor any to allow it really).

    Thanks.

    I’ve not solved the issue directly, but I found that the issue could of been I was using Ubuntu 12.10 x64. I’m now using 12.4 x32 and it works perfectly.

    Nice HowTo! Now, as a totally n00b to nginx, how do I configure it to redirect all WAN incoming traffic to a virtualserver while sending all LAN traffic to another virtual server? Thanks in advance, Martin

    Moisey Uretsky
    DigitalOcean Employee
    DigitalOcean Employee badge
    December 11, 2012

    There are a number of ways to do this, the easiest way is to setup a separate domain / server_name / server block for the internal LAN.

    Then for internal communication since you would be communicating on different IPs that would take care of it.

    So the public communication would be over the public IP with the listen directive, while the local communication can be done by setting the listen directive to a private LAN IP. Since the LAN IP is not visible from the public internet any communication that occurs over it is automatically private.

    And it doesn’t require any complex proxying rules.

    I am having memory issue with ubantu 12.04 droplet. After installing nginx/php/mysql and web2py I have found that memory is is slowly used up and needed to reboot to free up. So discarded every thing and rebuild the droplet. Nothing is installed on it, still memory issues remains.

    Here is the detail: https://docs.google.com/open?id=0B3z4rMfDQSHIcmpLZ1pqU1JVbjg

    Moisey Uretsky
    DigitalOcean Employee
    DigitalOcean Employee badge
    December 20, 2012

    Depending on the process you are running memory isn’t always freed up, instead it is cached until another application needs to use it.

    If you are consistently having issues I’d recommend you install swap space according to this tutorial: https://www.digitalocean.com/community/articles/how-to-add-swap-on-ubuntu-12-04

    This should help in your situation and if you start using a lot of swap you may want to resize to a larger virtual server because you may just be running more applications than your virtual server can support.

    After doing some experiment with ubantu, found that even in a new instance if you just run these : #sudo apt-get update #sudo apt-get -u upgrade the used memory will not be freed long after the execution. It is only freed after you do a reboot.

    so I destroyed my ubantu instance and created a fedora instance:

    Last login: Thu Dec 20 20:13:36 2012 from 180.234.111.171 [root@fedora01 ~]# free -m total used free shared buffers cached Mem: 497 428 68 0 33 359 -/+ buffers/cache: 35 461 Swap: 0 0 0 [root@fedora01 ~]# history 1 free -m 2 free -m 3 yum update 4 free -m 5 free 6 free -m 7 top 8 free -m 9 top 10 free -m 11 history [root@fedora01 ~]#

    The above are all the commands that was run after the instance created. RAM is already used up and i guess wont be released until a reboot.

    Moisey Uretsky
    DigitalOcean Employee
    DigitalOcean Employee badge
    December 21, 2012

    RAM should get freed up as different applications are started and start using memory since a lot of memory gets used as cache and will be automatically replaced.

    Also if you add swap space: https://www.digitalocean.com/community/articles/how-to-add-swap-on-ubuntu-12-04

    You can give your virtual server a buffer so if you do end up using more RAM than your system has you will see your swap space grow. Otherwise you should see that the cached memory is released and used up by other applications without increasing swap usage.

    If you are looking for a tutorial to install and configure a LEMP Stack on Debian (6) Squeeze. I have published a guide on my website located at http://techhelper.info/12/30/2012/debain-squeeze-lemp-stack/ .

    I followed the instructions to the letter (except using 12.10 32bit) and when I look at the info.php, I get:

    “The page you are looking for is temporarily unavailable. Please try again later.”

    Fixed. Needs this (socket instead of port?):

                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
    

    @moebis I had the same problem. Changing to a socket connection made it work for me. Thanks!

    +1 withe the same problem as @moebis and @natebird

    One thing I found with this tutorial is that it doesn’t seem to build the connection between php5 and mysql. I’m still working on getting WordPress to run, but I needed to run a ‘sudo apt-get install php5-mysql’ and that connected them.

    Having permission issues using Transmit by Panic to connect to my droplet. Seems I can login, but all the folders are owned by root. I now can’t login as root, and my user can’t change ownership. What to do?

    Moisey Uretsky
    DigitalOcean Employee
    DigitalOcean Employee badge
    January 17, 2013

    SSH to the server as root and change the ownership of the directory that your user logins into.

    chown username:group /directory/user/logs/in/to

    And then the user will be able to login and upload files.

    Tried this but end up with an error when viewing info.php. The page you are looking for is temporarily unavailable. Please try again later.

    From the error.log “2013/01/21 18:05:46 [crit] 6027#0: *1 connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory) while conne$”

    Any ideas?

    Moisey Uretsky
    DigitalOcean Employee
    DigitalOcean Employee badge
    January 21, 2013

    Looks like fpm was either not running or not configured do a quick :

    ps auxw | grep php-fpm

    To see if its listed in your process list.

    @dvogel That’s because php5-fpm doesn’t use sock so you can find “php5-fpm.sock” under path “/var/run/”;

    If you follow this instruction to use sock, you have to modify the default configuration for php5-fpm. To do this, edit: “/etc/php5/fpm/pool.d/www.conf” and change this line:

    “listen 127.0.0.1:9000”

    to

    “listen /var/run/php5-fpm.sock”

    the same path as you specified in nginx’s default settings.

    After this you should restart your php5-fpm service by using:

    $ sudo service restart php5-fpm

    Then you can check if there is a php5-fpm.sock file under /var/run/, if so, you’ve done a good job.

    BTW, the newest version of php5-fpm has already use sock as default listening option, but it only available for php 5.4 +. Since Ubuntu’s apt-get will install PHP 5.3 as default, so you have to change this option, or use the old way.

    I have followed the steps meticulously but i find that when i go to http://youripaddress/info.php it just asks me if i want to download the info.php file… I think the <?php phpinfo(); ?> code should echo some settings but for me that page doesnt show but instead asks me to download/save the info.php file…is that correct? can anyone help me figure out what im doing wrong? using Ubuntu 12.10 x64 Server
    thx

    @wortheverythingbut: I experienced the same thing, but if you’ll notice that the file has ‘improved’ since this article. The file, info.php, we created showed for me when I read this, http://maketecheasier.com/install-lemp-server-in-ubuntu/2012/05/29.

    It’s the config file, /etc/nginx/sites-available/default, in the section about fast.cgi. Read the article to address the changes with commenting and uncommenting.

    Actually, on Ubuntu 12.04 x32, PHP doesn’t work with sockets, it works with ports (see Albert’s comment above). Using sockets on 12.04 will give you a nice 502 Bad Gateway when accessing any PHP page.

    However, if you’re on Ubuntu 12.10 x32, sockets like in this tutorial, will work (tested). It would be nice to update the tutorial, since it refers to 12.04 and not 12.10

    My server is on a local lan, but I put in the external IP instead of the 192-- IP. Not sure if that is why Chrome just downloads the PHP file instead of showing a processed info.php in the browser window.

    great tuts… these are helpful php packages as well and fixed my featured image resize output in wordpress… sudo aptitude install php5-common sudo aptitude install php5-gd sudo aptitude install php5-cli sudo aptitude install php5-curl

    I can’t seem to get the nginx file changed to how the config. instructions demonstrate. The last portion is where I’m having trouble:

    What needs to be uncommented or added too or what? Or do I just leave as is.

    Can someone give me some hints please.

    pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000W

        location ~ \.php$ {
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
    

    @vic.palm2849 What you have there is correct. I’m betting you’re getting a “This page is not available…” error right? If so, the fix was already stated above, but all you have to do is:

    1. sudo nano /etc/php5/fpm/pool.d/www.conf
    2. comment out the line for: listen = 127.0.0.1:9000
    3. ADD: listen = /var/run/php5-fpm.sock
    4. sudo service php5-fpm restart

    I followed another tutorial to install Passenger and nginx to run my Ruby on Rails sites (https://www.digitalocean.com/community/articles/how-to-install-rails-and-nginx-with-passenger-on-ubuntu). This is up and running and works fine.

    I’ve now ready to upload my old PHP sites so installed php5-fpm and configured as this current article shows. I then added a new server block to my nginx config file (/opt/nginx/conf/nginx.conf), like so:

    server { listen 80; server_name localhost; root /home/rich/www/phptest; index index.php index.html; passenger_enabled off;

        location / {
                try_files $uri $uri/ /index.php;
        }
    
        location ~ \.php$ {
                #fastcgi_pass 127.0.0.1:9000;
                #include fastcgi_params;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
    }
    

    Next I created index.php with the following contents: <?php phpinfo(); ?> And as a sanity check, index.html which outputs a basic HTML page. Restarting PHP5-FPM and nginx and loading the pages, the HTML works fine, but the PHP page is completely blank. No error message or status code shown.

    Tailing nginx’s access.log shows: “GET /index.php HTTP/1.1” 200 31 “-” “Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:19.0) Gecko/20100101 Firefox/19.0” There isn’t any entry in its error.log. If I stop the php5-fpm service, I see a 502 Bad Gateway error in the browser and log. Restarting shows the blank page again.

    Does anyone have any ideas on how to resolve this? Anything I can try? If you need any more info, just ask.

    The index.php contents were simply the php tags and phpinfo();

    abunchofletters i’m getting a blank phpinfo page too my conf looks like yours/article

    hey i made it work by placing:

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    before

    include fastcgi_params;

    if you have a 502 check the file: /etc/php5/fpm/pool.d/www.conf for me the listen directive was set for port 9000 rather than a unix socket, so I had to change the nginx conf to reflect that

    I tried the samething for subdomain but did not work, is there something extra for a subdomain ?

    Marvelous!

    It took me a solid minute to figure out that I had to uncomment (delete the # symbol) specific sections before I saved. Thanks for the solid tutorial yo!

    I tried this before and It worked, right now I have new vps, I guess updated nginx has different configuration, I searched and turns out this,

    fastcgi_pass 127.0.0.1:9000;

    #fastcgi_pass unix:/var/run/php5-fpm.sock;

    if fastcgi_pass unix used, you get the 502, which scares me a lot, but uncomenting 127… works, you should try again, the topic needs update

    I need help now, I am trying this since tomorrow, how can i add www to my nginx confs, I am trying my old configuration file, but it does not seem to work, how can i do this ?

    “o apt-get install php5-fpm” is not working anymore.

    I get an error E: Couldn’t find package php5-fpm

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 14, 2013

    You can add www by simply adding another value after the server_name directive: server_name www.mydomain.com mydomain.com;

    @martin.taklaja try running apt-get update first.

    worked like a charm with @brian_sands tip

    hi, how can I create tumblr like subdmains, for example, username.tumblr.com, and how should I add cnames for this purpose on my dns ? thank you

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 20, 2013

    ^: You can set up a wildcard A record (* A 1.2.3.4) where 1.2.3.4 is your droplet’s IP address. After that you can add a server alias of *.yourdomain.com by simply passing it as an additional argument to the server_name directive.

    I should write " * " as hostname and I should write ip adress as my " ip adress " as an A record, and I should add " *.domain.com " as a server_name to nginx,

    to get subdomain variable should I use this ?, rewrite ^/([/0-9a-zA-Z-_]+)/$ /tab.php?tab_id=$1 last;

    thank you for your help :), I hope I am not bothering you

    Fixed it… the key is here:

    location ~ .php$ { #fastcgi_pass 127.0.0.1:9000; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock;

    change to:

    location ~ .php$ { fastcgi_pass 127.0.0.1:9000; # With php5-fpm: #fastcgi_pass unix:/var/run/php5-fpm.sock;

    which is just swapping which line has the # in front of it.

    BTW, That was in /etc/nginx/sites-available/default

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 27, 2013

    @(Posted May 20th, 2013 18:08): Sorry, it should be a wildcard CNAME record not an A record. Yes, what you said is correct.

    Hi,

    I’ve been stuck with no idea what went wrong at the info.php creation step. I followed everything to that point exactly, and then when I went to save the info.php in the /usr/share/nginx/www patj i get [ Error writing /usr/share/nginx/www/phpinfo.php: No such file or directory ].

    Now I have no idea what i missed. Any help would be appreciated, thanks.

    I’m on Ubuntu 13.04 x64 Server by the way.

    @bradleither you know if this folder exists,if not,you need see yor configuration,look on this explanation . you should fix it , doing these steps . 1 > Open the terminal : CTRL + ALT + T 2 > Put it , exactly : sudo nano / etc / nginx / sites-available / default ( look in this file where are your server folder by default and try using the command of the tutorial to create on correct place) worked for me… 3 > Comment this line : fastcgi_pass 127.0.0.1:9000 ; ( IF YOUR SERVER IS NOT WORKING ) you can comment this line putting before the arguments a "#"4 > The end result goes like it : #fastcgi_pass 127.0.0.1:9000 ; When You 're done you can close this file with CTRL + X, the console go ask if you want to save , Put y , However , the file name be the file name goes apear in a line , just delete last letter of the file and retype, Then press enter . 4 > Restart Nginx with this command : sudo service nginx restart go to your browser to see the Magic happening , Voylaaa .

    Special thanks to yavuzyurtbegendi by the solution…

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 28, 2013

    @bradleither: What’s the output of (“stat /usr/share/nginx/www”)?

    I got it all worked out, thanks.

    First it was that the www folder didn’t exist (no step said you have to add it) so the first thing I did was make it and try to give it permissions, but it wasn’t working for me for some reason either.

    I started over and this time, since it creates a /usr/share/nginx/html folder, what I did was just rename it using sudo mv /usr/share/nginx/html /usr/share/nginx/www and it seems to got me past the hurdle I encountered.

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 29, 2013

    @bradleither Glad to hear that. I’ve just tried installing nginx on a fresh Ubuntu 12.04 droplet and it created a directory called www, not html. What Ubuntu version are you on?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 29, 2013

    Oh I see, you’re on 13.04. I think the nginx package maintainers changed the default directory to /usr/share/nginx/html.

    I got white page screen and I have to add: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; to location ~ .php$

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    May 30, 2013

    Thanks @lucas, updated the article :]

    I got to step five and cannot find sudo nano /etc/php5/pool.d/www.conf or /var/run/php5-fpm.sock in my install.

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 10, 2013

    It could be in /etc/php5/fpm/pool.d/www.conf - please check.

    I just figured out that /etc/php5/pool.d/www.conf should be /etc/php5/fpm/pool.d/www.conf on my recent install

    Thanks Kamal, I just figured that out before I saw your response. Thanks for responding though.

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 10, 2013

    Glad it all works now! :]

    In the tutorial you describe: We need to make another small change in the php5-fpm configuration.Open up www.conf: sudo nano /etc/php5/pool.d/www.conf

    wel its exacly sudo nano /etc/php5/fpm/pool.d/www.conf

    But SOO much thanks for this tutorial, now all i need to find is a tutorial to make on top of the lemp a mailserver, any suggestions where to find that ?

    Making a feeble attempt to do this right - the first time, I have a quick question. In step five, where it mentions editing the line, I noticed that there is a semicolon in front of that line. Should that semicolon be

    Removed from the line? Remain on the line?

    Thanks for the help! -Brad

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 22, 2013

    @brad A semicolon in php5-fpm’s config marks the line as a comment, so you have to remove it for php5-fpm to actually parse it.

    So as to Kamal’s observation on 13.04 64 bit do we create the php.info in the html director or rename the html directory to www? What else would this affect?

    I meant info.php

    btw, Thought I would save everyone some time. When info.php wants to download instead of compiling and running MAKE SURE you UNCOMMENT the following line:

    location ~ .php$ {

    Who wants to do a screenshare via join.me and walk me throught this process? I’ve done this several times w/ no success. Apparently, every time the article mentions something that might be simple to most people like “update your configuration appropriately”, my butt has no freakin’ clue what to “update appropriately”!! I don’t want to remain ignorant, yet I’m running out of time. Everytime I ‘do’ this article, it takes around 20 minutes.

    But I’m looking for one definitive (I follow directions ‘too’ well, sometimes) set of instructions, start to finish, beginning to end, no comments that I have to wonder (does that comment apply to me?), no wondering “uh… what ‘configurations’ am I really supposed to be updating”, or anything else.

    I’d like to record what we do and use it to create a video (yes, I said video) tutorial for other folks to use here on DigitalOcean. So, who’s with me?

    What went wrong with this?

    • Restarting nginx
    • Stopping nginx nginx …done.
    • Starting nginx nginx nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] bind() to [::]:80 failed (98: Address already in use) nginx: [emerg] still could not bind() …done. …done.

    Any advise?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 6, 2013

    @dked146 nginx is still running, “pkill nginx” and “service nginx start” should fix that.

    By default the line cgi.fix_pathinfo=1

    is commented. Should we uncomment it or just change the value to 0 without uncomment it?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 19, 2013

    @Dzulhelmi: Uncomment it and set it to 0.

    I am getting a message Restarting nginx: nginx: [emerg] unexpected “c” in /etc/nginx/sites-enabled/default:67 nginx: configuration file /etc/nginx/nginx.conf test failed

    Can you tell me what I did wrong?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 19, 2013

    @r.l.lopez66: Please paste the output of the following command:

    cat /etc/nginx/sites-enabled/default | curl -F ‘sprunge=<-’ http://sprunge.us

    This guide mention to change listen = listen = 127.0.0.1:9000 to listen = /var/run/php5-fpm.sock

    But actually by changing this, the sample info.php file will show 502 Bad Gateway error.

    After i google, i found this page http://wildlyinaccurate.com/solving-502-bad-gateway-with-nginx-php-fpm which mention just let it at localhost so the php file can be run

    My question is WHY? why it shows that error page? Anyone can explain? It seems like useless if we point to localhost when we really NEED is to have php to run on fpm, right?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 22, 2013

    Dzulhelmi, listening on a socket is technically a bit faster than listening on a TCP port.

    The error depends on your nginx config:

    fastcgi_pass 127.0.0.1:9000;

    …requires you to have php5-fpm listening on port 9000.

    While

    fastcgi_pass unix:/var/run/php5-fpm.sock;

    …requires you to have php5-fpm listening on a socket located in /var/run.

    I dont know why i still get the “No input file selected” Can someone help me?

    This is my nginx http://pastebin.com/2HvkwnHi

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 24, 2013

    @Dzulhelmi: Delete “fastcgi_split_path_info ^(.+.php)(/.+)$;” and uncomment “# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;”

    Restart nginx. That should fix it.

    Also, I believe you want “index.html” to be “index.php” in the following line:

                try_files $uri $uri/ /index.html;
    

    @Kamal Nasser: It still not working. And why i have to delete the “fastcgi_split_path_info ^(.+.php)(/.+)$;” when i already have “cgi.fix_pathinfo = 0;” in php.ini?

    Does this problem because of “fastcgi_pass unix:/var/run/php5-fpm.sock;” and “fastcgi_pass 127.0.0.1:9000;”? I try to use either one, but both not working.

    Or is this because of permission problem? How to know that i am having permission problem?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    July 25, 2013

    Let’s not duplicate this. We’ll stick to this question thread: https://www.digitalocean.com/community/questions/no-input-file-specified

    In comparing this LEMP tutorial with <a href=“https://www.digitalocean.com/community/articles/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu”>How to Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu</a>, I noticed that the MySQL section of the LAMP tutorial outlines more steps than the same section of this tutorial. Is the LAMP tutorial advocating unnecessary steps or is this LEMP tutorial missing some needed steps to properly get MySQL set up correctly?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 1, 2013

    Thanks Pablo, the extra steps are actually recommended. I’ve updated this article :]

    Nice tutorials, got me running in less than an hour, just one quick question… i notice on mysql installation part that libapache2-mod-auth-mysql is included, isn’t this apache module? is it necessary if you are running nginx? would it affect the overall server setup if i excluded this one?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 2, 2013

    @caserjan: You can uninstall it if you want: sudo apt-get remove libapache2-mod-auth-mysql.

    If I install NGINX on a machine which already has Apache, how do i remove Apache from it?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 11, 2013

    @vsync.design: <pre>sudo apt-get remove apache2</pre>

    This article is excellent. I got up and running in 5min. No headaches. No throwing away cloud servers in frustration. Thanks.

    sudo service nginx restart restarting nginx: nginx: [emerg] “location” directive is not allowed here in /etc/nginx/sites-enabled/default:62 nginx: configuration file /etc/nginx/nginx.conf test failed

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 21, 2013

    @edgarlambarena: What’s do you have in /etc/nginx/sites-enabled/default?

    Good article… i think i will translate it to my italian blog and give you credit.

    Thanks for sharing.

    How do i uninstall nginx and do a fresh setup

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    August 28, 2013

    @ajesamson: <pre>sudo apt-get purge nginx* sudo rm -r /etc/nginx</pre>

    @edgarlambarena: I had the same problem as your earlier but I fixed it by removing the # before location ~ .php$

    tutorial worked like a charm! :) Obrigado

    hi i have done as per the above configuration but when i go to the info.php it just download the info.php file on my comp that i created with no details …any clue ?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 9, 2013

    @82.nitin: Please pastebin your nginx virtualhost config.

    I am running php5-fpm, nginx, MySQL with freshly installed WordPress on digital ocean droplet with 500 MB ram. I intermittently get 502 bad gateway error. Errors go away if I refresh the browser and things work out normally after that until the next 502 bad gateway error which also disappears with a browser refresh.

    This is probably needs some parameter tuned somewhere in my setup. Appreciate if you can please advise a solution to this intermittent 502 bad gateway issue I am running into. Thanks.

    I did not have any swap so I just added per instructions https://www.digitalocean.com/community/articles/how-to-add-swap-on-ubuntu-12-04

    Lets see if the intermitten 502 bad gateway errors disappear.

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 11, 2013

    @Nilesh: MySQL usually uses a lot of memory by default.

    Try running mysqltuner.pl (<a href=“http://www.howtoforge.com/tuning-mysql-performance-with-mysqltuner”>http://www.howtoforge.com/tuning-mysql-performance-with-mysqltuner</a>) and see if it helps with MySQL’s memory usage.

    please update the tutorial to reflect this

    Find the line, cgi.fix_pathinfo=1, and change the 1 to 0.

    “Use CRTL W and type cgi.fix to help you find the file to edit”

    location ~ .php$ { #fastcgi_pass 127.0.0.1:9000; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;

    Can someone recommend a solution for this last part above? should I install fast cgi? What is the meaning of this whole “Change the correct lines in “location ~ .php$ {“ section” part???

    ok so when I go to any php file in my /var/www directory it doesn’t do anything, this has to do with that last section doesn’t it? Any help?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 15, 2013

    @SaM5246: Is php5-fpm running? What’s the output of this command? <pre>sudo ps wwaux | grep php</pre>

    After my changes shall it look like this?:

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        #       #fastcgi_pass unix:/var/run/php5-fpm.sock;
        #       fastcgi_index index.php;
        #       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #       include fastcgi_params;
        #}
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
    
    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 22, 2013

    The final server block should look like this: <a href=“https://p.kk7.me/kagacumose.nginx”>https://p.kk7.me/kagacumose.nginx</a>

    or is this correct?

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
                #fastcgi_pass 127.0.0.1:9000;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
    
        }
    
        # deny access to .htaccess files, if Apache's document root
    
    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 22, 2013

    Yes, make sure you include the try_files directive (see the link I posted above).

    Great, thx, that link made this tutorial much easier to follow! And you were right, I had missed the try_files $uri =404;

    I though still get HTTP ERROR: 504.

    And I’ve now added: include fastcgi_params; fastcgi_read_timeout 300; } at the script and set max_execution_time = 300 in php.ini and /etc/php5/fpm/pool.d/www.conf

    but still HTTP ERROR: 504 at http://111.111.1.1/info.php (fake IP number)

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 22, 2013

    php is timing out, does it work if you try to browse to info.php (which contains <pre><php phpinfo();</pre>)?

    That’s what I did above. I also tried in a new browser now, pasted http://111.111.1.1/info.php hit enter and same result. Gateway Timeout. And I downloaded info.php and verified it got <?php phpinfo(); ?>

    <?php phpinfo();…

    Hmm, I ran free -m and it says total 491 used 479… I have Ubuntu 12:04, 64bit, so maybe that causes the problem? so I might need to install swap as above?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    September 22, 2013

    <a href=“http://www.linuxatemyram.com/”>This should help</a>

    ah :-) so I got 354MB free…

    When I turned off the firewall it all worked :-)

    Great tutorial! Straight-forward and works.

    Maybe this is a good idea to add http://wiki.nginx.org/Upstart

    Followed the details to the ‘T’…

    sudo service nginx restart Restarting nginx: nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32 nginx: configuration file /etc/nginx/nginx.conf test failed

    what does this mean?

    nevermind. it works now. instead of using ip address, entering the domain name works just fine.

    server_name example.com;

    I followed this tutorial and everything worked fine. But after following https://www.digitalocean.com/community/articles/how-to-install-express-a-node-js-framework-and-set-up-socket-io-on-a-vps my phpinfo file showed “The page you are looking for is temporarily unavailable.” When I run php5-fpm -t I get: [10-Oct-2013 23:14:41] ERROR: [/etc/php5/fpm/pool.d/www.conf:385] value is NULL for a ZEND_INI_PARSER_ENTRY [10-Oct-2013 23:14:41] ERROR: Unable to include /etc/php5/fpm/pool.d/www.conf from /etc/php5/fpm/php-fpm.conf at line 385 [10-Oct-2013 23:14:41] ERROR: failed to load configuration file ‘/etc/php5/fpm/php-fpm.conf’ [10-Oct-2013 23:14:41] ERROR: FPM initialization failed What can I do to fix this?

    Line 385 in www.conf shows: emergency_restart_threshold 10

    line 386: emergency_restart_interval 1m line 387: process_control_timeout 10s

    Hmm, now I remember…, sorry I think I added these lines from http://www.if-not-true-then-false.com/2011/nginx-and-php-fpm-configuration-and-optimizing-tips-and-tricks/ When I now removed the three lines it all works fine again. But maybe they where good to have?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    October 12, 2013

    @Asterix: These lines belong in php-fpm.conf and not www.conf

    When i put in sudo apt-get install php5-fpm i get E: Cpuldn’t find package php5-fpm

    after doing all of the above, my browser tries to download index.php instead of executing it.

    when I add: <?php phpinfo(); ?> I keep getting an error saying: no file or directory

    EDIT: after i add the file in info and save it it says can not write no such file or directory

    Using Ubuntu 13.04 32, definitely can’t make info.php work, browsers just download the file. All the rest looks fine, now testing WP install

    Can’t get past to that: “Welcome to nginx!” Well, I’m happy for a fist timer, but definitely would like to finish this LEMP install, may wipe out droplet and install Ubuntu 12.04?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    October 16, 2013

    @alex / @csfalcao: Did you follow Step 6?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    October 16, 2013

    @challeesmith: Please post the exact command you’re running

    Thanks a lot for this post. I read this and successfully install my server at <a href=“http://rongmotamhon.net” > http://rongmotamhon.net </a>

    Has anyone managed this on Ubuntu 13.10?

    I ran the command sudo apt-get update && sudo apt-get upgrade followed by the command sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

    However I get this error -

    Reading package lists… Done Building dependency tree
    Reading state information… Done Package libapache2-mod-auth-mysql is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source

    E: Package ‘libapache2-mod-auth-mysql’ has no installation candidate

    I’m aware 13.10 is a new release and I doubt there’s a solution, or might not even be due to 13.10.

    Any help would be appreciated.

    In response to my post above since I can’t edit, I figured it out incase anyone else wishes to set it up on 13.10 as well.

    Firstly I scrapped running the command -

    sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

    And instead ran the commands separately -

    sudo apt-get install mysql-server sudo apt-get install php5-mysql

    And went on with the rest of the steps to complete the set up, as far as I can see everything works without libapache2-mod-auth-mysql, although I’m not quite sure what this does and if it’s even needed.

    Apologies for the third post in a row, but after wondering why we needed libapache2-mod-auth-mysql, I download the .deb and placed it on the server itself and ran the command sudo dpkg -i libapache2-mod-auth-mysql.deb and received the following errors -

    Selecting previously unselected package libapache2-mod-auth-mysql. (Reading database … 56932 files and directories currently installed.) Unpacking libapache2-mod-auth-mysql (from libapache2-mod-auth-mysql.deb) … dpkg: dependency problems prevent configuration of libapache2-mod-auth-mysql: libapache2-mod-auth-mysql depends on apache2.2-common (>= 2.2.3-3); however: Package apache2.2-common is not installed.

    dpkg: error processing libapache2-mod-auth-mysql (–install): dependency problems - leaving unconfigured Errors were encountered while processing: libapache2-mod-auth-mysql

    Also just noticed that on August the 2nd Kamal responded to a previous comment stating we can uninstall this package, so after all it is actually not needed, just a heads up for future 13.10 installers. :)

    I just installed Ubuntu + zpanel is it possible to install nginx with zpanel and not use apache?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    November 1, 2013

    @Martin: Unfortunately zpanel does not support nginx.

    I’m confuse at this part ‘sudo nano /etc/nginx/sites-available/default’

    pls take a look at my code http://d.pr/f/wqEt

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    November 2, 2013

    @PigGy: Run this command: <pre>curl -s https://p.kk7.me/raw/caqonicigi | sudo tee /etc/nginx/sites-available/default</pre> That should update the default file for you.

    @Kamal Nasser wow, it work immediately once I refresh my ipsaddress/php.info thank you very much, now i can move on to next step

    I’m trying to activate mod rewrite but I’m having a problem here First I typed sudo a2enmod rewrite for Apache I can simply type this sudo nano /etc/apache2/sites-available/default but I’m not using Apache. Where do I change this using Nginx?

    hmm ok Thanks Kamal. Trued to get in contact with you but didn’t reply :P I’m FidbecK

    In <b>Step Two</b>, is the <code>libapache2-mod-auth-mysql</code> package needed – given that the article is about installing Nginx, as part of the LEMP stack, and <b>not</b> Apache?

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    November 10, 2013

    @Pablo: Nice catch, it’s not needed. It’s an apache module to allow MySQL auth when using HTTP Basic Authentication. I’ve updated the article. Thanks!

    For any yet to find reason I’m not getting any kind of error even If i misspell the db name :S

    in php.ini I have error_reporting set to E_ALL && E_STRICT (default values),

    *yet to be found bahhhh and it is error_reporting = E_ALL & ~E_DEPRECATED and not E_ALL && E_STRICT Like I wrote :S sorry

    Kamal Nasser
    DigitalOcean Employee
    DigitalOcean Employee badge
    November 13, 2013

    @arobycaronte: Is display_errors set to 1?

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Become a contributor for community

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    DigitalOcean Documentation

    Full documentation for every DigitalOcean product.

    Resources for startups and SMBs

    The Wave has everything you need to know about building a business, from raising funding to marketing your product.

    Get our newsletter

    Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

    New accounts only. By submitting your email you agree to our Privacy Policy

    The developer cloud

    Scale up as you grow — whether you're running one virtual machine or ten thousand.

    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

    *This promotional offer applies to new accounts only.