Comments on Installing Nginx with PHP 7 and MySQL 5.7 (LEMP) on Ubuntu 16.04 LTS

Nginx (pronounced "engine x") is a free, open-source, high-performance HTTP server. Nginx is known for its stability, rich feature set, simple configuration, and low resource consumption. This tutorial shows how you can install Nginx on an Ubuntu 16.04 server with PHP 7 support (through PHP-FPM) and MySQL support (LEMP = Linux + nginx (pronounced "engine x") + MySQL + PHP) .

17 Comment(s)

Add comment

Please register in our forum first to comment.

Comments

By: Couik

Hi,

I followed all instruction.

My server is on in less than 5 minutes :)

 

Thanks very much

Regards

Couik

By: mirec

this tutorial leads to "502 Bad Gateway" on Ubuntu 15.10

By: efero

fix 502

Your configuration may differ somewhat. Look for a block beginning with location ~ \.php$ {, and a line that looks something like fastcgi_pass unix:/var/run/php5-fpm.sock;. Change this to use unix:/var/run/php/php7.0-fpm.sock.

  server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www/html; index index.php index.html index.htm; server_name server_domain_name_or_IP; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }

By: Zotrob Chackli

This sounds more like a LIMP server

By: Thor

Thanks for this really great tutorial, I have a question:

Is the last step (7) is optional? To configure PHP-FPM to listen on TCP?

Thor

 

By: Adam

Yeah why do all these tutorials tell people to set FPM to TCP? its very strange. People please use the socket.. no idea why on a single server the overhead of TCP could be a benefit.

By: sagar

2002 - No such file or directory — The server is not responding (or the local server's socket is not correctly configured).

 

 mysqli_real_connect(): (HY000/2002): No such file or directory

By: jhosepgonzalez

oot@jhosepdeveloper:/home/desarollo/ahorasi# apt-get -y install php7.0-fpmLeyendo lista de paquetes... HechoCreando árbol de dependencias       Leyendo la información de estado... HechoE: No se ha podido localizar el paquete php7.0-fpmE: No se pudo encontrar ningún paquete con la expresión regular «php7.0-fpm»root@jhosepdeveloper:/home/desarollo/masseq#

By: acasado86

Very useful!

I would only add one thing.

 # Add index.php to the list if you are using PHP index index.php index.html index.htm index.nginx-debian.html;

By: vladimir kosyagen

feature request: put a date in your articles because without it, noone knows whether this info is actual or deprecated

By: till

Dates do not matter much for tutorials, tutorials are not news. I could write a tutorial for a 10 year old Ubuntu versions 10 years ago and I can write for it today, do you think that an article for a 10 year old OS would work on today's Ubuntu versions just because it was written today? No, it won't work. What really matters is for which Ubuntu version an article was written as that ensures that it will work for this Ubuntu version now and in many years in the exact same way. Take a look at the headline of the article, it shows the exact Ubuntu version (16.04) this article is made for, so this article will work for the version mentioned in the headline (16 = year 2016 and 04 = april) tells you also that the article must be written after that date.

By: dj steve bee

 

this:

php7.0-fpm reload

 

did not work.. had to do:

 

/etc/init.d/php7.0-fpm restart

 

 

at the end I got:

"Job for nginx.service failed because the control process exited with error code.See "systemctl status nginx.service" and "journalctl -xe" for details."

then did:

 

$ sudo nginx -t

 

nginx: [emerg] "fastcgi_pass" directive is duplicate in /etc/nginx/sites-enabled/default:57

 

nginx: configuration file /etc/nginx/nginx.conf test failed

 

had to undo that last step by commenting:

 

fastcgi_pass 127.0.0.1:9000;

 

in order to get the restart to work.

great tut btw! got me unstuck from a different one I had started with elsewhere!

By: Federico

I've followed the guide but when loadint the info.php I'm asked to download it instead of being shown the php table.

What might be wrong?

By: arnaud dujardin

 your nginx conf is simply not including php thus it's serving the file as an octetstream

By: Hector

I am getting a 502 bad gateway :(

 

 

this is my nginx config

 

 

server {

    listen 80;

    listen [::]:80;

 

    root /home/ftpuser1/Projects/goatform

    index.php;

 

    server_name 107.124.23.50;

 

    location / {

        try_files $uri $uri/ =404;

    }

 

    location ~ \.php$ {

        include snippets/fastcgi-php.conf;

        fastcgi_pass unix:/run/php/php7.0-fpm.sock;

    }

}

 

By: sjwrick

Yeah looks like you have uncomment the other reference two lines down:

    # fastcgi_pass unix:/run/php/php7.0-fpm.sock;

Its either one or the other.

By: Jim

The latest Raspbian release doesn't have a 7.0 version of php-fpm.  I installed php7.3-fpm which brought in a few other necessary php packages.