A LEMP server refers to a server running Linux, Enginx (Nginx), MySql and PHP (or Perl/Python). It is similar to the popular LAMP server except that the underlying web server is managed by Nginx instead of Apache. In this tutorial, we will show you how to install LEMP server in Ubuntu 12.04.
Nginx vs Apache
For those who are not aware, Nginx is an open-source web server that can run faster and use lesser system resources than Apache. Under light load, the differences between Apache and Nginx is negligible. However, on heavy load, Nginx can scale accordingly and run as fast without taking up tons of memory resource. Apache is a beast by itself and can easily take up to several hundreds of RAM for heavy loads.
Note: This tutorial assumes that Ubuntu 12.04 is already installed in your system. Command lines will be used instead of graphical interface since most web server doesn’t come with a desktop manager.
Installing Nginx
In the terminal, type the following:
sudo add-apt-repository ppa:nginx/stable sudo apt-get update sudo apt-get install nginx |
This will add the Nginx PPA to your repository so you are always updated with the latest stable version.
Just in case Apache is installed and running in the background, we need to stop it before Nginx can run.
sudo service apache2 stop |
Start Nginx.
sudo service nginx start |
If the above command doesn’t work, use this:
sudo /etc/init.d/nginx start |
Open a browser and browse to “http://localhost“. For a remote web host, you should type in your IP address instead. You should see the following:
![]()
Installing and Configuring PHP
Installing PHP is easy, but getting it to work with Nginx will require some configuration.
To install PHP5 and other essential modules:
sudo apt-get install php5-cli php5-fpm php5-mysql |
“php5-fpm” is the essential module for PHP to work in Nginx environment, so make sure it is installed.
Configuring Nginx to work with PHP
Open the “default” file in the /etc/nginx/sites-available directory.
sudo nano /etc/nginx/sites-available/default |
Scroll down the list till you see the line index index.html index.htm;. Add a index.php at the end of the line, just before the “;“. It should become like this:
![]()
Next, scroll down further until you see this block of code:
# 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; # } |
Remove the first “#” at the front of end line (except the line # fastcgi_pass unix:/var/run/php5-fpm.sock;) and add an extra line try_files $uri =404; at line 2, so it becomes like this:
location ~ \.php$ { try_files $uri =404; 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; } |
![]()
Save (press “Ctrl + o”) and exit (Ctrl + x) the config file.
Restart Nginx.
sudo service nginx restart |
To test if php5 is working in Nginx, we are going to create a php file, place it in the Nginx folder and see if it shows up in the browser.
sudo nano /usr/share/nginx/www/phpinfo.php |
Add the following code to the blank file:
<?php phpinfo(); ?> |
Save and exit the file.
Now, go to the browser and type “http://localhost/phpinfo.php” or “http://your-ip-address/phpinfo.php”.
![]()
If the php info shows up on the browser, then php is working fine with Nginx.
Installing MySql
In the terminal, type the following:
sudo apt-get install mysql-server |
During the installation, it will prompt you to create the root password.
![]()
Once installed, you are done with the LEMP server setup.
Optional Install: phpmyadmin
Phpmyadmin is not part of the LEMP server setup, but it is very helpful for managing database and is often included in many web server setup.
To install phpmyadmin,
sudo apt-get install phpmyadmin |
When prompted to select either “apache2″ or “lighttpd”, select none and click OK.
![]()
When prompted whether to configure database with dbconfig-common, select No.
![]()
After the installation, open the Nginx’s “default” file:
sudo nano /etc/nginx/sites-available/default |
Add the following code after the php block of code:
location /phpmyadmin { root /usr/share/; index index.php index.html index.htm; location ~ ^/phpmyadmin/(.+\.php)$ { try_files $uri =404; root /usr/share/; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include /etc/nginx/fastcgi_params; } location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ { root /usr/share/; } } location /phpMyAdmin { rewrite ^/* /phpmyadmin last; } |
![]()
Save and exit the file. Restart Nginx.
sudo service nginx restart |
You should be able to connect to phpmyadmin via the URL: “http://localhost/phpmyadmin” or “http://your-ip-address/phpmyadmin“
Receive the latest update in your inbox.







Perfect timing! I’m trying to learn nginx right now for Django deployments.
Josh, hope this is useful for you. Nginx is pretty easy to use. Once you understand how it works, the setting up is just a breeze. I have not tested it on python though.
This tutorial is really cool. Everything worked so far except the last step. I’m getting “The mysqli extension is missing. Please check your PHP configuration.” in /phpmyadmin. There is no mysqli module in phpinfo.php… What can I do?
I uncommented “mysqli.allow_local_infile = On” in php.ini and rebooted. Now it works :) I hope this will help someone.
Thanks. in some instances, you do have to activate certain modules manually. Glad you have got it to work.
If i want to host another website, what changes should i put in the nginx?
You will have to add another configuration file to the “sites-available” account. You can use the “default” file as the template, but you need to add another line “server_name example.com” at the top (remember to change the domain “example.com” to your own domain). Do also remember to change the server_root to point to the folder where the webpages are stored. Lastly, make a symlink to this file from the “sites-enabled” folder.
This is great. Thanks.
But when I go to http://myip/phpmyadmin I get a message that the mysqli extension is missing. Any ideas?
You can try to reinstall the mysql packages again.
sudo apt-get install --reinstall mysql-server php5-mysqlThe tutorial works great!
For the mysqli extension error, just go to php.ini file and at etc/php5 or etc/php and make this line “; mysqli.allow_local_infile = On” look like this “mysqli.allow_local_infile = On”. Thanks a bunch!
Question. How can I change the nginx default document root directory? Thanks!
I’m new to Nginx and want to explore
In the config file under the “sites-available” folder, you can change the
document root to point to another location.
I wonder why “500 Internal Server Error”
root /home/user101/box/devs;
Thanks for this, really helpful – I’m having trouble setting write permission for php-fpm on the site root directory. I’ve tried changing the username used by fpm to ‘nginx’ but still no dice. Also tried adding ‘www-data’ group to the folder permissions. Any ideas? Many thanks!
In your nginx.conf file, did you set the user?
Thanks for your guide.
Just a quick note: you must install python-software-properties first to use the add-apt-repository command
Thanks for the feedback. Depend on the Ubuntu version you are using,
“python-software-properties” could already be pre-installed in the system.
What a such great tutorial!
Thanks. It works perfect!
Well, that’s embarrassing, but when I try to start Nginx for the first time, I get an error message: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use). This message occurs four times before this one: nginx: [emerg] still could not bind(). Someone have any idea about this? I followed the installation steps exactly how they’re written.
It seems that there is already service (probably apache) running at port
80. You can use the command
sudo lsof -i tcp:80to check whichservice is currently running at port 80.
In the image for the fast cgi conf you have both directives uncommented. nginx won’t start because of this ambiguous configuration. BTW I would recommend using a socket rather than a TCP port, because it is faster locally.
I did all above steps till “http://your-ip-address/phpinfo.php”. after typing this i get 502 bad gateway , do you have any idea about this error? what should i di in this situation?
There are plenty of reasons for 502 bad gateway error. It could be that there is syntax error in your “default” configuration, or that the root is not pointed to the “www” folder, or your php5-fpm module is not properly configured.
kalau bisa command line nya yang lengkap dong
I’ve setup everything above. But how can i connect from windows machine to connect into my ubuntu machine. Pls. help.
If both your Windows and Ubuntu machine are connected in the same wifi network, you can even access the webserver by typing in the IP address of the Ubuntu machine (in most cases, it will be something like 192.168.1.xxx)