I-install ang Nginx sa Ngx_Pagespeed (Speed Optimization) sa Debian ug Ubuntu


Sa among katapusan nga artikulo, among gipakita kung giunsa pagpadali ang performance sa Nginx sa Ngx_Pagespeed sa CentOS 7. Sa kini nga panudlo, among ipasabut kung giunsa ang pag-install sa Nginx nga adunay ngx_pagespeed sa Debian ug Ubuntu nga sistema aron mapaayo ang pasundayag sa mga website sa Nginx.

Ang Nginx [engine x] usa ka libre ug open-source, sikat nga HTTP server nga naggahom sa daghang mga site sa web: ilado kaayo sa iyang high-performance ug stability. Naglihok usab kini isip reverse proxy, generic mail ug TCP/UDP proxy server, ug mahimo usab nga i-deploy isip load balancer.

Ang Ngx_pagespeed usa ka libre ug bukas nga gigikanan nga module sa Nginx nga gituyo alang sa pagpaayo sa katulin sa mga site ingon man usab sa pagkunhod sa oras sa pagkarga sa panid; kini sa hilabihan nga pagpaubos sa oras nga gikinahanglan sa mga tiggamit sa pagtan-aw ug pagpakig-uban sa sulod sa imong site.

  • Suporta sa HTTPS nga adunay kontrol sa URL.
  • Pag-optimize sa imahe: pagtangtang sa meta-data, dinamikong pagbag-o, pag-recompression.
  • CSS ug JavaScript minification, concatenation, inlining, ug outlining.
  • Gamay nga kapanguhaan inlining.
  • Paglangan sa hulagway ug pagkarga sa JavaScript.
  • Pagsulat pag-usab sa HTML.
  • Cache tibuok kinabuhi nga extension.
  • Gitugotan ang pag-configure alang sa daghang mga server ug daghan pa.

Lakang 1: Pag-install sa Nginx gikan sa Tinubdan

1. Aron ma-install ang Nginx nga adunay ngx_pagespeed gikan sa gigikanan gikinahanglan nga nagsunod sa mga pakete nga i-install sa sistema.

$ sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip

2. Sunod, i-download ang mga source file sa pinakabag-o nga bersyon sa Nginx (1.13.2 sa panahon niini nga pagsulat) gamit ang wget command ug kuhaa ang mga file sama sa gipakita sa ubos.

$ mkdir -p ~/make_nginx
$ cd ~/make_nginx
$ wget -c https://nginx.org/download/nginx-1.13.2.tar.gz
$ tar -xzvf nginx-1.13.2.tar.gz

3. Sunod, kuhaa ang ngx_pagespeed source files ug unzip ang compressed file nga sama niini.

$ wget -c https://github.com/pagespeed/ngx_pagespeed/archive/v1.12.34.2-stable.zip
$ unzip v1.12.34.2-stable.zip

4. Unya mobalhin ngadto sa unzipped ngx_pagespeed nga direktoryo ug i-download ang PageSpeed optimization library aron ma-compile ang Nginx sama sa mosunod.

$ cd ngx_pagespeed-1.12.34.2-stable/
$ wget -c https://dl.google.com/dl/page-speed/psol/1.12.34.2-x64.tar.gz
$ tar -xvzf 1.12.34.2-x64.tar.gz

Lakang 2: I-configure ug I-compile ang Nginx sa Ngx_Pagespeed

5. Sunod mobalhin ngadto sa nginx-1.13.2 nga direktoryo, ug i-configure ang Nginx nga tinubdan gamit ang mosunod nga mga sugo.

$ cd  ~/make_nginx/nginx-1.13.2
$ ./configure --add-module=$HOME/make_nginx/ngx_pagespeed-1.12.34.2-stable/ ${PS_NGX_EXTRA_FLAGS}

6. Sunod, i-compile ug i-install ang Nginx ingon sa mosunod.

$ make
$ sudo make install

7. Human makompleto ang proseso sa pag-instalar, padagana ang mga sugo sa ubos aron paghimo sa gikinahanglan nga mga symlink para sa Nginx.

$ sudo ln -s /usr/local/nginx/conf/ /etc/nginx
$ sudo ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

Lakang 3: Paghimo Nginx Unit File alang sa SystemD

8. Dinhi, kinahanglan nimo nga mano-mano ang paghimo sa Nginx unit file tungod kay ang systemd mao ang init nga sistema sa mas bag-ong mga bersyon sa Debian ug Ubuntu nga sistema

Una, paghimo sa file /lib/systemd/system/nginx.service.

$ sudo vi /lib/systemd/system/nginx.service

Dayon i-download ang NGINX systemd service file i-paste ang unit file configuration ngadto sa file.

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

I-save ang file ug isira kini.

9. Karon, sugdi ang serbisyo sa nginx sa pagkakaron, ug himoa kini nga magsugod sa system boot pinaagi sa paggamit sa mga sugo sa ubos.

$ sudo systemctl start nginx
$ sudo systemctl enable nginx

Importante: Human magsugod sa serbisyo sa Nginx, mahimo nimong makita ang usa ka sayup sama sa gipakita sa screen shot sa ubos.

systemd[1]: nginx.service: PID file /run/nginx.pid not readable (yet?) after start: No such file or directory 

Aron masulbad kini, ablihi ang Nginx configuration /etc/nginx/nginx.conf file ug idugang ang mosunod nga linya.

#pid  logs/nginx.pid;
to
pid  /run/nginx.pid;

Sa katapusan i-restart ang serbisyo sa nginx pag-usab.

$ sudo systemctl daemon-reload
$ sudo systemctl start nginx
$ sudo systemctl status nginx

Lakang 4: I-configure ang Nginx Uban sa Pagespeed Module

10. Karon nga ang Nginx na-install ug nagdagan sa imong sistema, kinahanglan nimo nga palihokon ang module sa Ngx_pagespeed. Una paghimo og direktoryo diin ang module mag-cache sa mga file para sa imong website ug itakda ang angay nga permiso niini nga direktoryo sama sa mosunod.

$ sudo mkdir -p /var/ngx_pagespeed_cache
$ sudo chown -R nobody:nogroup /var/ngx_pagespeed_cache

11. Aron mahimo ang Ngx_pagespeed module, ablihi ang Nginx configuration file.

$ sudo vi /etc/nginx/nginx.conf

Idugang ang pagsunod sa Ngx_pagespeed nga mga linya sa pagsumpo sulod sa block sa server.

# Pagespeed main settings

pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;


# Ensure requests for pagespeed optimized resources go to the pagespeed
# handler and no extraneous headers get set.

location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }

Mubo nga sulat: Kung nag-deploy ka bisan unsang nginx virtual host sa server, idugang ang mga direktiba sa pagespeed sa ibabaw sa matag block sa server aron mahimo ang Ngx_pagespeed sa matag site.

Ang mosunod mao ang usa ka nagtrabaho nga panig-ingnan sa Nginx configuration file uban sa Ngx_pagespeed enabled sa default virtual host.

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
pid   /run/nginx.pid;

events {
    worker_connections  1024;
}
http {
    include       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  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
	# Pagespeed main settings
	pagespeed on;
	pagespeed FileCachePath /var/ngx_pagespeed_cache;
	# Ensure requests for pagespeed optimized resources go to the pagespeed
	# handler and no extraneous headers get set.
	location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
	location ~ "^/ngx_pagespeed_static/" { }
	location ~ "^/ngx_pagespeed_beacon" { }
	location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

I-save ug isira ang file.

12. Dayon susiha kon ang syntax sa Nginx configuration file kay error free pinaagi sa pagpadagan sa command sa ubos, kon kini husto, imong makita ang output sa ubos:

$ sudo nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

13. Pagkahuman i-restart ang Nginx server aron ma-epekto ang bag-ong mga pagbag-o.

$ sudo systemctl restart nginx

Lakang 5: Pagsulay sa Nginx sa Ngx_pagespeed

14. Karon sulayi kung ang Ngx-pagespeed karon nagtrabaho kauban sa Nginx gamit ang cURL command sa ubos.

$ curl -I -p http://localhost

Kung napakyas ka sa pagtan-aw sa ulohan sa ibabaw, unya balik sa lakang 10 ug mabinantayon nga pag-adto sa mga panudlo aron mahimo ang Ngx-pagespeed sa sunud-sunod nga mga lakang.

Ngx-pagespeed Github repository: https://github.com/pagespeed/ngx_pagespeed

Kung gusto nimo nga ma-secure ang Nginx web server, nan among gisugyot nga basahon kini nga mapuslanon nga panudlo: Ang Katapusan nga Giya sa Secure, Harden ug Improve Performance of Nginx.

Mao na! Sa kini nga panudlo, among gipasabut kung giunsa ang pag-install sa Nginx sa ngx_pagespeed sa Debian ug Ubuntu. Kung naa kay mga pangutana ipadala kanamo gamit ang among porma sa komentaryo sa ubos.