Below is the general setup and configs I use with NGINX + PHP7 + WordPress. The first file is my main NGINX config file.
/etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
client_max_body_size 8M;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
server_name _;
return 444;
}
}
I create a new user adduser username
with a home directory. I include a /home/username/public_html
directory where WordPress will be installed.
Below are my NGINX configuration file(s) for WordPress.
/etc/nginx/conf.d/yourdomain.com.conf
upstream php7 {
server unix:/var/run/php/php7.0-fpm-username.sock;
}
server {
listen 80;
server_name youdomain.com www.youdomain.com;
root /home/username/public_html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.html /index.php$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_read_timeout 300;
fastcgi_pass php7;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ /\. {
deny all;
}
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
}
For PHP I create a pool that will run the PHP process under the previously created user.
/etc/php/7.0/fpm/pool.d/username.conf
[username]
user = username
group = username
listen = /var/run/php/php7.0-fpm-username.sock
listen.owner = nginx
listen.group = nginx
request_terminate_timeout = 300
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
You will need to restart NGINX…
service nginx restart
If the restart fails you can use nginx -t
to help debug the issue.
… and then PHP
service php7.0-fpm restart
Create an index.php
file in /home/username/public_html/
with the following contents
<?php phpinfo(); ?>
Then navigate to http://yourdomain.com - if all is well you should see a PHP information page.
From here you can then continue installing WordPress.
Please note in the above configs you will need to replace
- username with a username of your choice.
- yourdomain.com with your registered domain.
- 7.0 with the PHP version that was installed (ie. 7.1, 7.2, etc…)
You can try the free SlickStack LEMP script on DO servers:
https://github.com/littlebizzy/slickstack
Ubuntu + Nginx (FastCGI Cache) + MySQL + PHP-FPM + Redis (object cache)
…Bash install only, do you know?