Nginx has proven to be a very good Web Server capable of serving thousands of requests per second.

Its very popular for developing PHP Enterprise Applications and have it well configured is key in having a high performance PHP Application capable of handling intense workloads and very high traffic websites.

For Nginx to work as very High performance Web Server you need to have configured your Linux kernel as well following this recipe: Linux Kernel Tuning for High Performance LEMP

Step 1. replace the content of the file /etc/nginx/nginx.conf with this:

user nginx;
    error_log  /var/log/nginx/error.log crit;
    pid        /var/run/nginx.pid;
    worker_processes  1;
    worker_rlimit_nofile 16384;
events {
    worker_connections  50000;
    multi_accept on;
    use epoll;
}
http {
    ssl_password_file /etc/nginx/certs/pass_file;
    default_type  application/octet-stream;
    include mime.types;
    access_log  off;
    keepalive_timeout 30;
    fastcgi_read_timeout 3600;
    proxy_read_timeout 3600;
    tcp_nodelay on;
    sendfile on;
    expires -1;
    server_tokens off;
    tcp_nopush on;
    types_hash_max_size 2048;
    fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=microcache:100m max_size=500m inactive=60m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    gzip  on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_min_length 1100;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_proxied any;
    gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
    include /etc/nginx/conf.d/*;
    open_file_cache          max=5000  inactive=20s;
    open_file_cache_valid    30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors   on;
}

 

 worker_processes 1; ### Set this equal to the number of cores or vcores available in the machine

worker_rlimit_nofile 16384; ### This is set with the same number you set in /etc/security/limits.conf

ssl_password_file /etc/nginx/certs/pass_file; ### In this file you will store the password you used for creating SSL Certificates.

 

Step 2. Configure a virtual host with HTTPS following any of the recipes posted in this section.

It is highly recommended that you enable and use HTTPS in HTTP2 with TLS / SSL.

 

Step 3. Restart your Web Server.

$ sudo systemctl restart nginx

Before configuring the Web Server and the PHP Fast page Manager for deliver High Performance PHP Enterprise Application, the Linux kernel where the machine is running need some settings to be optimized.

Step 1. Open a SSH Terminal into your machine.

Step 2. Change Schedulers.

$ sudo grubby --update-kernel=ALL --args="elevator=noop"

Step 3. Update Kernel settings.

$ sudo nano /etc/sysctl.conf

Replace the content of this file with:

#####  Performance kernel conf
##     Dev: Ruben Elizondo
##
vm.swappiness = 1
fs.file-max = 5000000
net.core.netdev_max_backlog = 400000
net.core.optmem_max = 10000000
net.core.rmem_default = 10000000
net.core.rmem_max = 10000000
net.core.somaxconn = 100000
net.core.wmem_default = 10000000
net.core.wmem_max = 10000000
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_congestion_control = bic
net.ipv4.tcp_ecn = 0
net.ipv4.tcp_max_syn_backlog = 12000
net.ipv4.tcp_max_tw_buckets = 2000000
net.ipv4.tcp_mem = 30000000 30000000 30000000
net.ipv4.tcp_rmem = 30000000 30000000 30000000
net.ipv4.tcp_sack = 1
net.ipv4.tcp_syncookies = 0
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_wmem = 30000000 30000000 30000000
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
kernel.shmmax=1073741824
kernel.msgmni=1024
kernel.sem=250 32000 32 1024

save the file.

Step 4. Make open files higher value.

$ sudo nano /etc/security/limits.conf

*** Add the following line right below #@student

*                -	 nofile          16384

save the file.

Step 5. reboot your machine

$ sudo reboot now

 

 

Zend OP Cache is key in having a very high performance Software Application running in PHP.

A good way to see how it is working, which files are cached and more, is using this very nice PHP Script.

Prerequisites:

 

copy ocp.php from my github repository to the folder where your virtual host configuration file is pointing for the index.php

$ nano /var/www/cool_project/public/ocp.php

 Click on the copy raw icon and paste the content of the file.

 

Save the file and if you set any of the virtual hosts configuration files posted here, you just need to open https://your.project.domain/ocp.php and you will see something like this:

 

Click on the Files button link and click on ungroup to get a list of all cached files and some useful stats:

Modern PHP Enterprise Systems Applications require high efficient fast loading web sites. These are the key concepts you must configure in your Nginx Web Server to have a high performance web application:

- Enable caching: Nginx has a built-in caching mechanism that can significantly improve website performance. You can enable caching by adding the following lines to your Nginx configuration file:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 60m;
proxy_cache_valid 404 1m;

- Use gzip compression: Nginx can compress responses before sending them to the client, which can greatly reduce the amount of data that needs to be transferred. You can enable gzip compression by adding the following lines to your configuration file:

gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

- Enable SSL: If you're not already using SSL, consider enabling it for improved security and search engine ranking along with parallel resource loading through HTTP2. You can enable SSL by adding the following lines to your configuration file:

listen 443 ssl;
ssl_certificate /path/to/certificate.crt;
ssl_certificate_key /path/to/private.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;

- Use FastCGI caching: If you're using PHP-FPM, you can enable FastCGI caching to cache the output of PHP scripts. This can greatly improve website performance. You can enable FastCGI caching by adding the following lines to your configuration file:

fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_valid 200 60m;
fastcgi_cache_valid 404 1m;

- Optimize static file serving: Nginx can serve static files very efficiently, but you can further optimize this by adding the following lines to your configuration file:

Optimize static file serving: Nginx can serve static files very efficiently, but you can further optimize this by adding the following lines to your configuration file:

Refer to the sample config files here:

Nginx with PHP-FPM in Vagrant Centos 7 Virtual Machine

Install PHP7 with FPM in a Vagrant Centos 7

Configure PHP in Virtual Machine to work as LEMP Server

Configure Nginx Web Server with SSL in a Vagrant Virtual Machine