Fi Nginx sori ẹrọ pẹlu Ngx_Pagespeed (Iṣapeye Iyara) lori Debian ati Ubuntu


Ninu nkan wa ti o kẹhin, a fihan bi a ṣe le ṣe iyara iṣẹ Nginx pẹlu Ngx_Pagespeed lori CentOS 7. Ninu ẹkọ yii, a yoo ṣalaye bawo ni a ṣe le fi Nginx sori ẹrọ pẹlu ngx_pagespeed lori eto Debian ati Ubuntu lati ṣe ilọsiwaju iṣẹ ti awọn oju opo wẹẹbu Nginx.

Nginx [engine x] jẹ orisun ọfẹ ati ṣiṣi, olupin HTTP olokiki ti o ni agbara ọpọlọpọ awọn aaye lori ayelujara: o mọ daradara fun iṣẹ giga ati iduroṣinṣin rẹ. O tun n ṣiṣẹ bi aṣoju yiyipada, meeli jeneriki ati olupin aṣoju TCP/UDP, ati pe a le fi ranṣẹ ni afikun bi iwọntunwọnsi fifuye.

Ngx_pagespeed jẹ module ọfẹ ati ṣiṣi orisun Nginx ti a pinnu fun imudarasi iyara awọn aaye ati idinku akoko fifuye oju-iwe; o dinku akoko ti o gba fun awọn olumulo lati wo ati ṣepọ pẹlu akoonu lori aaye rẹ.

  • HTTPS ṣe atilẹyin pẹlu iṣakoso URL.
  • Iṣapeye aworan: yiyọ data-meta, atunṣe iwọn agbara, ifasilẹ.
  • CSS ati minifafari JavaScript, isọdọkan, titọka, ati ṣiṣapẹrẹ.
  • Atokun kekere oro.
  • Fifipamọ aworan ati ikojọpọ JavaScript.
  • atunkọ HTML.
  • Ifiranṣẹ igbesi aye Kaṣe.
  • Faye gba atunto fun awọn olupin pupọ ati ọpọlọpọ awọn miiran.

Igbesẹ 1: Fifi sori ẹrọ Nginx lati Orisun

1. Lati fi Nginx sii pẹlu ngx_pagespeed lati orisun ti a beere fun awọn idii atẹle lati fi sori ẹrọ lori eto naa.

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

2. Itele, ṣe igbasilẹ awọn faili orisun ti ẹya tuntun ti Nginx (1.13.2 ni akoko kikọ yi) nipa lilo aṣẹ wget ki o jade awọn faili bi o ti han ni isalẹ.

$ 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. Itele, gba awọn faili orisun ngx_pagespeed ki o si ṣii faili ti a fi rọpọ bii eleyi.

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

4. Lẹhinna gbe sinu itọsọna ngx_pagespeed ti ko ṣii ati gba awọn ile-ikawe ti o dara ju PageSpeed lati ṣajọ Nginx gẹgẹbi atẹle.

$ 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

Igbesẹ 2: Tunto ati ṣajọ Nginx pẹlu Ngx_Pagespeed

5. Nigbamii gbe sinu itọsọna nginx-1.13.2, ati tunto orisun Nginx nipa lilo awọn ofin wọnyi.

$ 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. Nigbamii, ṣajọ ati fi Nginx sii bi atẹle.

$ make
$ sudo make install

7. Lẹhin ti ilana fifi sori ẹrọ ti pari, ṣiṣe awọn aṣẹ ni isalẹ lati ṣẹda awọn ami-ami pataki ti o yẹ fun Nginx.

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

Igbesẹ 3: Ṣiṣẹda Faili Unit Nginx fun SystemD

8. Nibi, iwọ yoo ni lati ṣẹda pẹlu ọwọ faili Nginx kuro nitori siseto jẹ eto init lori awọn ẹya tuntun ti eto Debian ati Ubuntu

Fisrt, ṣẹda faili /lib/systemd/system/nginx.service.

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

Lẹhinna ṣe igbasilẹ faili iṣẹ eto NGINX lẹẹ mọ iṣeto faili faili si faili naa.

[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

Fipamọ faili naa ki o pa.

9. Bayi, bẹrẹ iṣẹ nginx fun akoko naa, ki o jẹ ki o bẹrẹ ni bata eto nipa lilo awọn ofin ni isalẹ.

$ sudo systemctl start nginx
$ sudo systemctl enable nginx

Pataki: Lẹhin ti o bẹrẹ iṣẹ Nginx, o le wo aṣiṣe bi o ṣe han ninu iboju iboju ni isalẹ.

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

Lati yanju rẹ, ṣii faili Nginx /etc/nginx/nginx.conf ki o fi ila wọnyi si.

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

Lakotan tun bẹrẹ iṣẹ nginx lẹẹkansi.

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

Igbesẹ 4: Tunto Nginx Pẹlu Module Pagespeed

10. Bayi pe Nginx ti fi sii ati ṣiṣe lori eto rẹ, o nilo lati mu ki module Ngx_pagespeed ṣiṣẹ. Ni akọkọ ṣẹda itọsọna kan nibiti module naa yoo ṣe kaṣe awọn faili fun oju opo wẹẹbu rẹ ati ṣeto awọn igbanilaaye ti o yẹ lori itọsọna yii gẹgẹbi atẹle.

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

11. Lati mu ki module Ngx_pagespeed ṣiṣẹ, ṣii faili iṣeto Nginx.

$ sudo vi /etc/nginx/nginx.conf

Ṣafikun atẹle awọn ila iṣeto ni Ngx_pagespeed laarin apo olupin.

# 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" { }

Akiyesi: Ti o ba ti gbe eyikeyi awọn ogun foju foju nginx sori olupin naa, ṣafikun awọn itọsọna oju-iwe ti o wa loke si bulọọki olupin kọọkan lati jẹki Ngx_pagespeed lori aaye kọọkan.

Atẹle yii jẹ apẹẹrẹ iṣẹ ti faili iṣeto Nginx pẹlu Ngx_pagespeed ti a mu ṣiṣẹ ninu olupin foju aiyipada.

#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;
    #    }
    #}
}

Fipamọ ki o pa faili naa.

12. Lẹhinna ṣayẹwo ti iṣapẹẹrẹ ti faili iṣeto Nginx jẹ aṣiṣe ọfẹ nipa ṣiṣe pipaṣẹ ni isalẹ, ti o ba tọ, iwọ yoo wo iṣẹjade ni isalẹ:

$ 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. Lẹhinna tun bẹrẹ olupin Nginx lati ṣe awọn ayipada to ṣẹṣẹ.

$ sudo systemctl restart nginx

Igbesẹ 5: Idanwo Nginx pẹlu Ngx_pagespeed

14. Bayi ṣe idanwo boya Ngx-pagespeed n ṣiṣẹ ni apapo pẹlu Nginx nipa lilo aṣẹ cURL ni isalẹ.

$ curl -I -p http://localhost

Ti o ba kuna lati wo akọsori ti o wa loke, lẹhinna pada sẹhin lati ṣe igbesẹ 10 ati ni ifarabalẹ lọ nipasẹ awọn itọnisọna lati jẹki Ngx-oju-iwe pẹlu awọn igbesẹ atẹle.

Ibi ipamọ Github ti oju-iwe Ngx: https://github.com/pagespeed/ngx_pagespeed

Ti o ba fẹ ṣe ifipamo olupin ayelujara Nginx, lẹhinna a daba daba kika nipasẹ ẹkọ ti o wulo yii: Itọsọna Gbẹhin si Aabo, Ikunkun ati Ṣiṣe Iṣe ti Nginx.

Òun nì yen! Ninu ẹkọ yii, a ṣalaye bi o ṣe le fi Nginx sori ẹrọ pẹlu ngx_pagespeed lori Debian ati Ubuntu. Ti o ba ni eyikeyi awọn ibeere ṣe firanṣẹ wa ni lilo fọọmu fọọmu awọn asọye wa ni isalẹ.