Nginx è un server web HTTP ad alte prestazioni gratuito, open source che a differenza di altri server web, non si basa sulla gestione threaded delle richieste ma utilizza invece un evento molto più scalabile guidato (asincrono ) architettura.
Questo utilizza una quantità di memoria molto piccola e prevedibile sotto carico pesante. Nginx in combinazione con il semplice e molto robusto FastCGI Process Manager per PHP (PHP-FPM ) e il server di database più famoso al mondo MySQL può darti molta potenza e prestazioni pur utilizzando un ingombro di memoria ridotto.
Il seguente articolo riguarda come installare e configurare lo stack LEMP su un VPS CentOS 6 e host WordPress velocissimo applicazioni web potenziate.
L'articolo è suddiviso nelle seguenti sezioni:
- Configurazione iniziale
- Installa e configura Nginx
- Installa e configura MySQL
- Installa e configura PHP-FPM
- Installa e configura WordPress
- Imposta la memorizzazione nella cache per prestazioni ottimali
## screen -U -S lemp-stack
Una volta che sei nella sessione dello schermo, assicurati che il tuo CentOS 6 VPS sia completamente aggiornato eseguendo:
## yum update
se hai Apache installato sul tuo VPS, fermalo e rimuovilo eseguendo:
## /etc/init.d/httpd stop ## yum remove httpdPASSAGGIO 1) Installa e configura Nginx
abilitare il repository epel eseguendo:
trova la tua architettura VPS eseguendo uname -m
– VPS a 32 bit:
## wget -P /tmp http://mirror.pnl.gov/epel/6/i386/epel-release-6-8.noarch.rpm ## rpm -Uvh /tmp/epel-release-6-8.noarch.rpm ## rm -f /tmp/epel-release-6-8.noarch.rpm
– VPS a 64 bit:
## wget -P /tmp http://mirror.itc.virginia.edu/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm ## rpm -Uvh /tmp/epel-release-6-8.noarch.rpm ## rm -f /tmp/epel-release-6-8.noarch.rpm
verifica che tutto sia aggiornato
## yum update
Installa Nginx
via yum eseguendo:
## yum install nginx
Vai alla directory di configurazione di Nginx in /etc/nginx/
e modifica nginx.conf
con il tuo editor preferito:
## cd /etc/nginx/ ## vim nginx.conf
user nginx; worker_processes 2; error_log /var/log/nginx/error.log; #error_log /var/log/nginx/error.log notice; #error_log /var/log/nginx/error.log info; 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; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 30; server_tokens off; gzip on; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; gzip_http_version 1.1; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js; # enabled sites include /etc/nginx/sites-enabled/*; }
crea sites-enabled
e sites-available
all'interno di /etc/nginx
directory:
## mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled
impostare la direttiva host virtuale Nginx predefinita aggiungendo quanto segue in /etc/nginx/sites-available/default.conf
server { listen 80 default_server; server_name _; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } }
abilitare la direttiva host virtuale Nginx predefinita creando un collegamento simbolico alla configurazione vhost predefinita in /etc/nginx/sites-enabled/
## cd /etc/nginx/sites-enabled ## ln -s /etc/nginx/sites-available/default.conf
prova la configurazione di Nginx, aggiungilo all'avvio del tuo sistema e infine avvialo da:
## nginx -t ## /etc/init.d/nginx restart ## chkconfig nginx onPASSAGGIO 2) Installa e configura MySQL
installa il server di database MySQL, avvialo e aggiungilo all'avvio del tuo sistema eseguendo i seguenti comandi:
## yum install mysql mysql-server ## service mysqld restart ## chkconfig mysqld on
quindi, esegui il comando seguente per configurare MySQL
## mysql_secure_installation
Enter current password for root (enter for none): Set root password? [Y/n] y Remove anonymous users? [Y/n] y Disallow root login remotely? [Y/n] y Remove test database and access to it? [Y/n] y Reload privilege tables now? [Y/n] y
assicurati che il tuo MySQL non sia esposto all'ascolto sull'IP pubblico del tuo server aggiungendo quanto segue a /etc/my.cnf
## vim /etc/my.cnf [mysqld] bind-address = 127.0.0.1 ...
riavviare il server del database per rendere effettive le modifiche:
## /etc/init.d/mysqld restartPASSAGGIO 3) Installa e configura PHP-FPM
installa PHP-FPM
e alcune utili estensioni PHP eseguendo il comando seguente:
## yum install php-fpm php-mysql php-gd php-mcrypt
modifica /etc/php.ini
e modificare/decommentare quanto segue:
cgi.fix_pathinfo=0 date.timezone = America/New_York memory_limit = 64M expose_php = Off
quindi, modifica /etc/php-fpm.conf
con il tuo editor preferito e decommenta quanto segue:
emergency_restart_threshold = 10 emergency_restart_interval = 1m process_control_timeout = 10
con tutto ciò a posto, imposta un pool PHP-FPM in /etc/php-fpm.d/www.conf
:
## mv /etc/php-fpm.d/www.conf /root/ ## vim /etc/php-fpm.d/www.conf
[wordpress] ;listen = 127.0.0.1:9001 listen = /var/run/php-wordpress.socket user = nginx group = nginx request_slowlog_timeout = 5s slowlog = /var/log/php-fpm/blogcms.log listen.allowed_clients = 127.0.0.1 pm = dynamic pm.max_children = 10 pm.start_servers = 3 pm.min_spare_servers = 2 pm.max_spare_servers = 4 pm.max_requests = 400 listen.backlog = -1 pm.status_path = /status request_terminate_timeout = 120s rlimit_files = 131072 rlimit_core = unlimited catch_workers_output = yes php_value[session.save_handler] = files php_value[session.save_path] = /var/lib/php/session php_admin_value[error_log] = /var/log/php-fpm/wordpress-error.log php_admin_flag[log_errors] = on
riavvia PHP-FPM e aggiungilo all'avvio del tuo sistema:
## /etc/init.d/php-fpm restart ## chkconfig php-fpm on
a questo punto dovresti avere Nginx , MySQL e PHP-FPM attivo e funzionante sul tuo server. Procedi con la creazione di una direttiva vhost per il tuo WordPress applicazione:
## vim /etc/nginx/sites-available/my-wordpress.tld.conf
server { listen 80; server_name my-wordpress.tld; rewrite ^(.*) http://www.my-wordpress.tld$1 permanent; } server { listen 80; server_name www.my-wordpress.tld; client_max_body_size 5m; client_body_timeout 60; access_log /var/log/nginx/my-wordpress.tld-access; error_log /var/log/nginx/my-wordpress.tld-error error; root /var/www/html/my-wordpress.tld/; index index.html index.php; ### root directory ### location / { try_files $uri $uri/ /index.php?$args; } ### security ### error_page 403 =404; location ~ /\. { access_log off; log_not_found off; deny all; } location ~ ~$ { access_log off; log_not_found off; deny all; } location ~* wp-admin/includes { deny all; } location ~* wp-includes/theme-compat/ { deny all; } location ~* wp-includes/js/tinymce/langs/.*\.php { deny all; } location /wp-includes/ { internal; } #location ~* wp-config.php { deny all; } location ~* ^/wp-content/uploads/.*.(html|htm|shtml|php)$ { types { } default_type text/plain; } # location ~* wp-admin { # allow <YOUR_IP>; # allow 127.0.0.1; # deny all; # } ### disable logging ### location = /robots.txt { access_log off; log_not_found off; } location = /favicon.ico { access_log off; log_not_found off; } ### caches ### location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { access_log off; expires max; } location ~* \.(woff|svg)$ { access_log off; log_not_found off; expires 30d; } location ~* \.(js)$ { access_log off; log_not_found off; expires 7d; } ### php block ### location ~ \.php?$ { try_files $uri =404; include fastcgi_params; #fastcgi_pass 127.0.0.1:9001; fastcgi_pass unix:/var/run/php-wordpress.socket; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_intercept_errors on; fastcgi_split_path_info ^(.+\.php)(.*)$; #Prevent version info leakage fastcgi_hide_header X-Powered-By; } }
abilitare la direttiva host virtuale e riavviare nginx eseguendo i seguenti comandi:
## cd /etc/nginx/sites-enabled ## ln -s /etc/nginx/sites-available/my-wordpress.tld.conf ## nginx -t ## /etc/init.d/nginx restart
prova PHP-FPM creando PHP info.php
script nella radice del documento vhost in /var/www/html/my-wordpress.tld/
:
## mkdir -p /var/www/html/my-wordpress.tld/ ## cd /var/www/html/my-wordpress.tld/ ## echo -e "<?php\n\tphpinfo();\n" > info.php
accedi a http://my-wordpress.tld/info.php
per testare il tuo PHP-FPM
Il prossimo passo è installare WordPress all'interno della radice del documento vhost in /var/www/html/my-wordpress.tld/
. Prima di installare WordPress, creiamo prima un database MySQL eseguendo:
## mysql -u root -p
mysql> create database wordpressDB; mysql> grant all on wordpressDB.* to wpUser@localhost identified by 'YOUR_PASS'; mysql> quit
## cd /var/www/html/my-wordpress.tld/ ## wget http://wordpress.org/latest.zip ## unzip latest.zip ## mv wordpress/* . ## rm -rf latest.zip wordpress/
quindi, copia la configurazione di esempio di WordPress e imposta le informazioni del database MySQL:
## cp wp-config-sample.php wp-config.php ## vim wp-config.php
define('DB_NAME', 'wordpressDB'); define('DB_USER', 'wpUser'); define('DB_PASSWORD', 'YOUR_PASS');
## chown nginx: -R /var/www/html/my-wordpress.tld/
apri http://my-wordpress.tld e completa l'installazione di WordPress
PASSAGGIO 5) Imposta la memorizzazione nella cache per prestazioni migliori
Installa PHP-APC (Alternative PHP Cache) eseguendo:
## yum install php-pecl-apc
una volta installato APC, aggiungi quanto segue a /etc/php.d/apc.ini
## cat > /etc/php.d/apc.ini
extension = apc.so apc.enabled=1 apc.shm_segments=1 apc.shm_size=128M apc.num_files_hint=1024 apc.user_entries_hint=4096 apc.ttl=7200 apc.use_request_time=1 apc.user_ttl=7200 apc.gc_ttl=3600 apc.cache_by_default=1 apc.filters apc.mmap_file_mask=/tmp/apc.XXXXXX apc.file_update_protection=2 apc.enable_cli=0 apc.max_file_size=1M apc.stat=1 apc.stat_ctime=0 apc.canonicalize=0 apc.write_lock=1 apc.report_autofilter=0 apc.rfc1867=0 apc.rfc1867_prefix =upload_ apc.rfc1867_name=APC_UPLOAD_PROGRESS apc.rfc1867_freq=0 apc.rfc1867_ttl=3600 apc.include_once_override=0 apc.lazy_classes=0 apc.lazy_functions=0 apc.coredump_unmap=0 apc.file_md5=0 apc.preload_path
e riavvia PHP-FPM affinché le modifiche abbiano effetto.
## /etc/init.d/php-fpm restart
controlla se APC viene caricato eseguendo:
## php -m | grep -w apc
oppure aprendo il info.php
script nella radice del documento.
La prossima cosa da fare è accedere alla tua amministrazione WordPress e installare il W3 Total Cache Plugin . Affinché W3 Total Cache Plugin funzioni, devi prima abilitare Pretty URLs
in
Settings->Permalinks->Custom Structure
:
http://my-wordpress.tld/%postname%/
e quindi procedere con l'installazione di W3 Total Cache
. Una volta installato, vai su
Performance->General Settings
e abilita/disabilita le seguenti opzioni:
Page cache: enabled Page cache method: Disk: Enhaced Minify: disabled Database Cache: enabled Database Cache Method: Opcode: Alternative PHP Cache (APC) Object Cache: enbabled Object Cache Method: Opcode: Alternative PHP Cache (APC) Browser Cache: disabled CDN: this is up to you.
Fai clic su Save all settings
per inviare le modifiche.
Aggiungi quanto segue in /var/www/html/my-wordpress.tld/nginx.conf
## cat > /var/www/html/my-wordpress.tld/nginx.conf
# BEGIN W3TC Page Cache cache location ~ /wp-content/cache/page_enhanced.*html$ { add_header Vary Cookie; } # END W3TC Page Cache cache # BEGIN W3TC Page Cache core set $w3tc_rewrite 1; if ($request_method = POST) { set $w3tc_rewrite 0; } if ($query_string != "") { set $w3tc_rewrite 0; } if ($request_uri !~ \/$) { set $w3tc_rewrite 0; } if ($http_cookie ~* "(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle)") { set $w3tc_rewrite 0; } if (!-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html") { set $w3tc_rewrite 0; } if ($w3tc_rewrite = 1) { rewrite .* "/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html" last; } # END W3TC Page Cache core
assicurati che la proprietà della radice del documento sia corretta da:
## chown nginx: -R /var/www/html/my-wordpress.tld/
Il passaggio successivo consiste nel dire a Nginx di utilizzare questo file di configurazione. Modifica /etc/nginx/sites-enabled/my-wordpress.tld.conf
e aggiungi/decommenta quanto segue:
include /var/www/html/my-wordpress.tld/nginx.conf; ... location ~* wp-config.php { deny all; }
prova il file di configurazione di Nginx e riavvialo per rendere effettive le modifiche eseguendo:
## nginx -t ## /etc/init.d/nginx restart
Puoi anche modificare il file di configurazione di WordPress /var/www/html/my-wordpress.tld/wp-config.php
e definisci quanto segue in modo che WordPress non debba interrogare il database per l'URL del sito:
define('WP_HOME', 'http://my-wordpress.tld'); define('WP_SITEURL', 'http://my-wordpress.tld');
Naturalmente, se sei uno dei nostri clienti di hosting VPS Linux, non devi fare nulla di tutto questo, chiedi semplicemente ai nostri amministratori, siediti e rilassati. I nostri amministratori lo configureranno immediatamente per te.
PS. Se questo post ti è piaciuto condividilo con i tuoi amici sui social network utilizzando i pulsanti a sinistra o semplicemente lascia una risposta qui sotto. Grazie.