MENU
How To Install Linux, Nginx, MySQL, PHP (LEMP) Stack on Debian 7 TAGGE D I N: LI NUX BAS I CS , NGI NX , MY S QL, P HP , DE BI AN
SHARE
P UBLI S HE D: OCT 31, 2013 • UP DATE D: AUG 4, 2014
Lemp Stack: Basic Info LEMP stack is a group of open source software to get a VPS up and running. The acronym includes the Linux (L), MySQL (M), and PHP (P) of the more traditional LAMP stack, but switches out Apache for Nginx (pronounced engine x). This tutorial explains how to install nginx/MySQL/PHPfpm, as well as all of the required repos to download nginx and phpfpm. It will also review how to configure php and nginx, as well as how to create a php info page.
1) Update AptGet The aptget update command is used to resynchronize the package index files from their sources. If used in combination with the aptget upgrade command, they install the newest versions of all packages currently available. At the moment, we only need to do a thorough update:
sudo apt-get update
2) Install MySQL on your VPS
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
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...
The prompt will ask if you want to change the root password. Go ahead and choose N for this option, as the root password should aready be set; however, for the rest of the questions you can simply reply Y to all unless there is a reason for you to do otherwise. Now let's install nginx!
3) Install and Configure Nginx on your VPS Installation Initial installation is simple with the aptget command.
sudo apt-get install nginx
nginx needs a command to begin running:
sudo service nginx start
Now if you point your browser to your IP address, it should confirm that nginx was successfully installed on your cloud server. *Run the following command to reveal your VPS's IP address.
ifconfig eth0 | grep inet | awk '{ print $2 }'
Configuration
Open up the default virtual host file with this command:
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 /var/run/php5location ~ \.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_ 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
4) Install and Configure PHP Installation You probably guessed it! We will use the aptget command to install PHP FPM:
sudo apt-get install php5-fpm php5-mysql
Configuration 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 php5fpm 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/php5fpm.sock.
listen = /var/run/php5-fpm.sock
Save and Exit. Restart phpfpm:
sudo service php5-fpm restart
5) 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:
Then Save and Exit.
Finishing Up Restart nginx:
sudo service nginx restart
You can see the nginx and phpfpm configuration details by visiting http://youripaddress/info.php Your LEMP stack is now set up and configured on your virtual private server :) By Adam LaGreca
Related Tutorials How To Install and Secure phpMyAdmin with Apache on a CentOS 7 Server How To Configure Varnish Cache 4.0 with SSL Termination on Ubuntu 14.04 How To Install and Secure phpMyAdmin with Nginx on a CentOS 7 Server How To Install and Secure phpMyAdmin with Nginx on an Ubuntu 14.04 Server How To Use Terraform with DigitalOcean
38 Comments kyle November 4, 2013 I think it is safe to include a section that checks/removes apache if its already on your server. Great guide!
infy November 16, 2013 This tutorial actually installs Apache in the first steps. I removed apache manually, but it keeps on reinstalling when I run the suggested php installation command. (libapache2 modauthmysql). Is apache needed? When I open the browser on the IP it shows apache instead of nginx...
Kamal Nasser November 17, 2013 @infy: Thanks, I've updated the article. You do not need libapache2modauthmysql.
peterson.m.chris November 20, 2013 I'm getting these errors (see below) and can't see my php info page. However, I get the "It works!" default page on my IP. Restarting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] still could not bind() nginx.
Kamal Nasser November 20, 2013
@Chris: Rebooting your droplet should fix it.
peterson.m.chris November 21, 2013 I did a sudo reboot, I get the same error. This happened to me a few weeks ago when I tried to set up Nginx as well. Thought I'd give it another try.
Kamal Nasser November 22, 2013 @Chris: You probably have apache listening on port 80. Try to see what's already using that port: sudo netstat -plutn | grep 80
peterson.m.chris December 2, 2013 Yeah looks like apache2 is listening.
Kamal Nasser December 2, 2013 @Chris: Uninstall apache and start nginx: sudo service apache2 stop sudo apt-get remove apache2* sudo service nginx start
peterson.m.chris December 2, 2013 That did it, thanks! So does Apache some with a new droplet already?
Kamal Nasser December 5, 2013 @Chris: It shouldn't, no. The only exceptions are the application images which usually come with Apache preinstalled.
jimojimmyojim December 7, 2013 i was getting this error: bind() to 0.0.0.0:80 failed (98: Address already in use) but i did this: sudo fuser k 80/tcp and then: service nginx start and it was all good. i think another service was using port 80, which had to be stopped and then i started nginx
james.daynz January 22, 2014 Not sure why, nginx working fine, everything went fine but when i go to http://127.0.0.1/info.php it just downloads the file.
m.connelly.2711 January 28, 2014 This doesn' work for nginx
Kamal Nasser January 28, 2014 @m.connelly.2711: What part isn't working for you?
Kamal Nasser January 28, 2014 @james.daynz: Did you restart nginx after modifying its config files?
edreamer2009 February 6, 2014 Excellent tutorial Kamal. I have been using my droplets for other purposes thus far, but
decided to build a LEMP stack and this was a perfect walk through for a first time run at it. Thank you, E
andrius.baliutis February 18, 2014 Hello, i do everything. It seems that all things installed corectly. But then i trying to put ip address in my browser it didint load anything..
dwing February 20, 2014 If youu do not want to install apache, change the order. false: sudo aptget install mysqlserver php5mysql right: sudo aptget install mysqlserver Before: sudo service php5fpm restart Do: sudo aptget install php5mysql Then everything is fine. Also note, that on Debian aptget install mysqlserver will install an old version. Use aptget install mysqlserver5.5 instead.
admin April 6, 2014 Is this tutorial using Debian 7 because it's better suited a LEMP stack as apposed to Ubuntu? I'd like to build a stack best suited for WordPress for speed, caching and concurrency Is this the stack to use? Or can I take my existing premade WordPress Ubuntu 13.10 droplet and modify it using this tutorial? Any clear direction is indeed appreciated
Kamal Nasser April 7, 2014 @Shane: Both OSes work just fine for a LEMP stack. We have tutorials for multiple OSes so you can follow the tutorial on your preferred OS.
parsonsandrew1 May 23, 2014 Thank you for the tutorial. Step 2 installs Apache. @dwing points this out, and I think it would be great (read: necessary) if you could update the tutorial so the installation of Apache is avoided.
hostmaster May 29, 2014 I agree with parsonsandrew1!
Andrew SB May 29, 2014 @dwing, @hostmaster, and @parsonsandrew1: I've edited the article so that php5mysql is installed at the same time as php5fpm that way Apache isn't getting pulled in. Thanks all!
geo.sukarno June 14, 2014 My Droplet using Ubuntu 12.04, still fresh create. I was setup xrdp, restart, and worked. Then i want install nginx only, using this tutorial. But has 2 error, 'ldconfig' and 'startstopdaemon' not found in PATH or not executable. Screenshot: http://picpaste.com/nginxinstallproblem.png How to fix the problem?
geo.sukarno June 15, 2014 Updated. It's my fault, for not using sudo. I guess if I was logged in as root, I do not need to use sudo, it turns out I was wrong. When using sudo, then the error about "not found in PATH or not executable" will not occur.
rideshare7 June 20, 2014 Change the correct lines in “location ~ \.php$ {“ section.. Can you pls help me what does this means?
Andrew SB June 20, 2014 @rideshare7: It is just summarizing the difference between the default Nginx configuration and what you added. So, change this: #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; # include fastcgi_params; #}
into this: 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_ include fastcgi_params; }
rideshare7 June 20, 2014 @astarr: Thanks for a prompt reply. Really appreciated. And the tutorial is a great help :)
elmphn July 3, 2014 Which extra steps would you do if you were to configure more than one fastcgi web servers? For example, if I'd like web1 to 9000 and web2 to 9001 With TCP requests like fastcgi_pass 127.0.0.1:9000; I guess it is all a matter a changing the number but, what about with php5fpm.sock?
Kamal Nasser July 4, 2014 @elmphn: You can simply change the socket's path to whatever you want (on both ends, php5fpm and nginx).
marcos July 27, 2014 Hello, i'm following this tutorial, but when load the page, the css and js is not loading. what happened?
Kamal Nasser July 30, 2014 @marcos: Are there any errors in nginx's error log?
sudo tail /var/log/nginx/error.log
gp+digitalocean July 31, 2014 Hello, "# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000" This comment is incorrect, the configuration uses a unix domain socket.
halsaifm1 August 2, 2014 im getting this when i restart it .... root@example:~# sudo service nginx restart Restarting nginx: nginx: [emerg] unknown directive "104.131.216.41" in /etc/nginx/sites enabled/default:9
Kamal Nasser August 4, 2014 @halsaifm1: What does the default file look like? The line with your IP address should look like this: server_name 104.131.216.41;
Kamal Nasser August 4, 2014 @gp+digitalocean: Thanks, I've corrected it.
halsaifm1 August 6, 2014 thanksssssss it worked !!!!! i thought we only need to put the ip but it seems i need to put the
server name and then the ip . thanks again ;)
Log In to comment
Leave a comment...
S UB MI T COMME NT
This work is licensed under a Creative Commons AttributionNonCommercial ShareAlike 4.0 International License.
Copyright © 2014 DigitalOcean ™ Inc. Proudly Made in NY Terms, Privacy, & Copyright Security
COMMUNI TY Dashboard Overview Tutorials Questions Projects Tutorial Suggestions Get Paid to Write
2,224,955 D R O PLETS LAU N C H ED