OpenResty 创建虚拟子主机并开启 PHP-FPM 设置防跨站
本文最后更新于 2338 天前,其中的信息可能已经有所发展或是发生改变。

介绍

之前几篇,我们已经介绍了 OpenResty、PHP 和 MariaDB 的安装,那么如何创建虚拟子主机呢,并让 OpenResty 加载 PHP-FPM呢?这就是我们今天要介绍的。

默认主机安装探针和 phpMyAdmin

开启 PHP-FPM

/usr/local/openresty/nginx/conf/nginx.conf 中的这一段修改为,下面 PHP 的版本是 7.1,里面还加入了防跨站参数其他版本记得修改:

######################## default ############################
  server {
  ##运行端口
  listen 80; 
  ##这条意味着非指定域名之外均使用这个设置
  server_name _; 
  
  access_log /data/wwwlogs/access_nginx.log combined; #日志目录
  root /data/wwwroot/default; #网站文件目录
  index index.html index.htm index.php; #首页文件优先级
  
  ##PHP
  location ~ [^/]\.php(/|$) {
      fastcgi_pass unix:/run/php/php7.1-fpm.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
      fastcgi_param PHP_VALUE "open_basedir=$document_root:/tmp/:/proc/";
    }
    
  ##下面的都是缓存
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    expires 30d;
    access_log off;
    }
  location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
    }
  location ~ /\.ht {
    deny all;
    }
  }

下载探针

cd /data/wwwroot/default
wget http://www.yahei.net/tz/tz.zip
unzip tz.zip

访问:http://yourip/tz.php 就可以看到亲切的雅黑探针了。

下载 phpMyAdmin

cd /data/wwwroot/default
wget https://files.phpmyadmin.net/phpMyAdmin/4.7.1/phpMyAdmin-4.7.1-all-languages.zip
unzip phpMyAdmin-4.7.1-all-languages.zip
mv phpMyAdmin-4.7.1-all-languages pma

访问:http://yourip/pma 就可以看到亲切的 phpMyAdmin 了。

虚拟子主机

单独为域名专门开辟虚拟子主机,首先创建储存内容的目录:

mkdir -p /data/wwwroot/主机目录名称
mkdir -p /usr/local/openresty/nginx/conf/vhost/

接下来,创建子主机配置文件,记得将 server_name yourdomian; 改成 server_name 你的域名

cat >> /usr/local/openresty/nginx/conf/vhost/yourdomian.conf << EOF  
  server {
  ##运行端口
  listen 80; 
  
  ##这里需要改成你的域名
  server_name yourdomian; 
  
  access_log /data/wwwlogs/access_nginx.log combined; #日志目录
  root /data/wwwroot/yourdir; #网站文件目录
  index index.html index.htm index.php; #首页文件优先级
  
  ##PHP
  location ~ [^/]\.php(/|$) {
      fastcgi_pass unix:/run/php/php7.1-fpm.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
      fastcgi_param PHP_VALUE "open_basedir=$document_root:/tmp/:/proc/";
    }
    
  ##下面的都是缓存
  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    expires 30d;
    access_log off;
    }
  location ~ .*\.(js|css)?$ {
    expires 7d;
    access_log off;
    }
  location ~ /\.ht {
    deny all;
    }
  }
EOF

然后重启 OpenResty,就可以访问你的 http://你的域名 看看网站的 403 了:

nginx -s reload
上一篇
下一篇