您现在的位置是:首页 > 技术人生 > 服务器相关服务器相关

nginx添加php站点 多站点搭建

高晓波2020-12-10【服务器相关】人已围观

简介本篇记录一下nginx+php5.6.40搭建php站点, centos7搭建nginx、php环境:《centos7 编译安装php 5.6.40》《centos7 手动编译安装 Nginx 1.18.0》

1、添加站点相关目录,方便集中

本篇记录一下nginx+php5.6.40搭建php站点, centos7搭建nginx、php环境:《centos7 编译安装php 5.6.40》《centos7 手动编译安装 Nginx 1.18.0》

1、添加站点相关目录,方便集中管理、配置
#站点配置目录
mkdir /usr/local/nginx/conf/vhost

#php站点运行目录
mkdir /usr/local/nginx/www

#ssl证书存放目录
mkdir /usr/local/nginx/cert

#php版本控制目录
mkdir /usr/local/nginx/conf/php

2、添加站点目录,将网站程序上传至此目录下
mkdir /usr/local/nginx/www/gaoxiaobo.com

#上传站点程序至此目录,此处省略

#更改站点目录归属用户、用户组为www
chown -R www gaoxiaobo.com
chgrp -R www gaoxiaobo.com

3、如果站点使用https,上传站点ssl证书至cert目录(可省)
#创建站点证书目录
mkdir /usr/local/nginx/cert/gaoxiaobo.com
#上传证书至此目录下


4、添加站点配置文件
cd /usr/local/nginx/conf/vhost
vi gaoxiaobo.com.conf
 
server
{
    listen 80;
    listen 443 ssl http2;
    #绑定域名
    server_name www.gaoxiaobo.com gaoxiaobo.com;
    index index.php index.html index.htm default.php default.htm default.html;
    #网站根目录
    root /usr/local/nginx/www/gaoxiaobo.com;
    
    #error_page 404/404.html;

    #不带www跳转到www
    if ($host ~ '^gaoxiaobo.com'){
            return 301 https://www.gaoxiaobo.com$request_uri;
    }


    #http跳转https
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }

    #ssl证书配置
    ssl_certificate    /usr/local/nginx/conf/cert/gaoxiaobo.com/1_www.gaoxiaobo.com_bundle.crt;
    ssl_certificate_key    /usr/local/nginx/conf/cert/gaoxiaobo.com/2_www.gaoxiaobo.com.key;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497  https://$host$request_uri;
    
    #PHP版本配置
    include /usr/local/nginx/conf/php/enable-php-56.conf;
    
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log /dev/null;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      12h;
        error_log off;
        access_log /dev/null; 
    }
    

    access_log  /usr/local/nginx/logs/gaoxiaobo.com.log;
    error_log  /usr/local/nginx/logs/gaoxiaobo.com.error.log;
}

上述配置根据自己目录情况进行修改。

5、enable-php-56.conf配置(其他版本php可参考修改)

 
/usr/local/nginx/conf/php/enable-php-56.conf
 
location ~ [^/]\.php(/|$)
{
        try_files $uri =404;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include /usr/local/nginx/conf/fastcgi.conf;
        

        set $real_script_name $fastcgi_script_name;
		if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
	         set $real_script_name $1;
	         set $path_info $2;
		 }
		fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
		fastcgi_param SCRIPT_NAME $real_script_name;
}

6、修改nginx配置,导入vhost目录配置文件
vi /usr/local/nginx/conf/nginx.conf

#添加以下配置,导入vhost目录配置文件
include /usr/local/nginx/conf/vhost/*.conf;

Tags:nginx   php

很赞哦! ()

文章评论