死磕nginx系列--nginx服務器作web服務器

nginx 作靜態服務器

HTML頁面以下javascript

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>圖片展現</h1>

<div>
    <img src="/static/images/1.png">
</div>
</body>
</html>

上傳相關文件,生成以下路徑php

tree html/
html/
├── index.html
└── static
    └── images
        └── 1.png
## 配置nginx.conf 配置文件
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
/data/app/nginx/sbin/nginx -t 
nginx: the configuration file /data/app/nginx-1.10.3/conf/nginx.conf syntax is ok
nginx: configuration file /data/app/nginx-1.10.3/conf/nginx.conf test is successful
/data/app/nginx/sbin/nginx -s reload

瀏覽器訪問:
css

這個時候咱們能夠把static靜態頁面給拆分出來html

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /static/ {
            root /data/db;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

將靜態文件遷移到/data/db目錄下,並重啓nginx服務。java

mv html/static/ /data/db/
/data/app/nginx/sbin/nginx -t 
/data/app/nginx/sbin/nginx -s reload

測試圖片是否可否獲取:node

curl -I http://192.168.56.12/static/images/1.png
HTTP/1.1 200 OK
Server: nginx/1.10.3
Date: Sun, 08 Apr 2018 09:31:35 GMT
Content-Type: image/png
Content-Length: 32239
Last-Modified: Sun, 08 Apr 2018 09:21:26 GMT
Connection: keep-alive
ETag: "5ac9df16-7def"
Accept-Ranges: bytes

對圖片開啓gzip壓縮

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

    gzip on;
    gzip_min_length 1k;
    gzip_buffers    4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types image/png;
    gzip_vary on;
    
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /static/ {
            root /data/db;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
/data/app/nginx/sbin/nginx -t 
/data/app/nginx/sbin/nginx -s reload

對比兩次響應頭信息,開啓gzip 壓縮後響應頭多了Content-Encoding: gzip,開啓壓縮成功。
mysql

nginx 反向代理後端服務器

配置nginx環境

user  www www;
worker_processes 8;
error_log  /data/logs/nginx_error.log  crit;
pid        /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
  use epoll;
  worker_connections 65535;
}
http
{
  include       mime.types;
  default_type  application/octet-stream;
  #charset  gb2312;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
  sendfile on;
  tcp_nopush     on;
  keepalive_timeout 60;
  tcp_nodelay on;
include  gzip.conf;
include blog.biglittle.cn.conf;

gzip.conf文件內容nginx

gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;

blog.biglittle.cn.conf文件內容web

##  server
  {
    listen 80 default;
    server_name blog.biglittleant.cn;
    index index.html index.htm index.php;
    root html;
    location ~ .*\.(php|php5)?$
    {
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
  }

fastcgi.conf文件內容sql

fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;

安裝PHP 環境

下載PHP文件,並安裝基礎依賴包

wget http://cn2.php.net/distributions/php-7.1.2.tar.gz
 yum -y install  libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel libcurl-devel libjpeg-turbo-devel openssl openssl-devel

編譯安裝

./configure --prefix=/data/app/php-7.1.2 --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo -enable-tokenizer --enable-zip --enable-bcmath --enable-sockets --with-gettext
make && make install 
ln -s /data/app/php-7.1.2/ /data/app/php7
cp php.ini-development /data/app/php7/lib/php.ini
cp sapi/fpm/php-fpm.service /usr/lib/systemd/system/

修改配置文件

vim /data/app/php7/lib/php.ini
# 查找 mysqli.default_socket,修改爲:
mysqli.default_socket = /data/app/mysql/mysql.sock
date.timezone = PRC

好了,PHP 7 已經安裝好,下面驗證一下

shell > /data/app/php7/bin/php -v
PHP 7.0.5 (cli) (built: Apr  8 2016 00:08:04) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

再查看下已經安裝的模塊

/data/app/php7/bin/php -m

接着配置 php-fpm文件

# copy php-fpm 的配置文檔
cp /data/app/php7/etc/php-fpm.conf.default /data/app/php7/etc/php-fpm.conf
cp /data/app/php7/etc/php-fpm.d/www.conf.default /data/app/php7/etc/php-fpm.d/www.conf

其中 www.conf 中要留意如下這個值 listen = 127.0.0.1:9000

配置 php-fpm 啓動服務腳本

修改啓動腳本,把裏邊 prefix 相關的內容用實際路徑代替

vim /usr/lib/systemd/system/php-fpm.service
PIDFile=/usr/local/php7/var/run/php-fpm.pid
ExecStart=/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf
# 從新載入 systemd
systemctl daemon-reload
systemctl start php-fpm 
ss -lntup |grep 9000

編寫PHP測試文件

vim /data/app/nginx/html/hello.php 編寫一個PHP測試文件。

<html>
 <head>
  <title>PHP 測試</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?>
 <?php phpinfo(); ?>
 </body>
</html>

測試是否可用

/data/app/nginx/sbin/nginx -t 
/data/app/nginx/sbin/nginx -s reload

相關文章
相關標籤/搜索