Nginx+PHP-FPM搭建Typecho博客


前言
VPS内存资源告急,只好把博客架构换一换,改由Nignx+SQLite来搭建了。这里就是搭建的记录过程。

添加nginx的软件源

rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安装nginx/php-fpm

yum install -y nginx php php-fpm

下载typecho并解压到/var/www/blog

更改nginx和php-fpm的身份为nobody

vim /etc/nginx/nginx.conf
user nobody

vim /etc/php-fpm.d/www.conf
user nobody
group nobody

放置证书
我这里有一份早已从SSL For Free申请好的证书,将ca_bundle.crt、certificate.crt两份文件合并后复制到/etc/ssl/sslforfree路径下

nginx的配置文件
共两份配置文件,第一个是配置443端口的

vim /etc/nginx/conf.d/443.conf
server {
    listen 443 ssl;
    server_name hid3rx.space;
    return 301 $scheme://www.hid3rx.space$request_uri;
    ssl_certificate /etc/ssl/sslforfree/certificate.crt;
    ssl_certificate_key /etc/ssl/sslforfree/private.key;
}

server {
    # 全局设置
    listen 443 ssl;
    server_name www.hid3rx.space;
    root /var/www;
    ssl_certificate /etc/ssl/sslforfree/certificate.crt;
    ssl_certificate_key /etc/ssl/sslforfree/private.key;

    # 日志
    charset utf-8;
    access_log /var/log/nginx/access.log;

    # 首页跳转
    location = / {
        rewrite / /blog last;
    }

    # 博客页面
    location /blog {
        index index.php index.html;
        # URL rewrite
        if (!-e $request_filename) {
            rewrite ^/blog/(.*)$ /blog/index.php/$1 last;
        }
    }

    # PHP pathinfo
    location ~ [^/]\.php(/|$) {
        fastcgi_index index.php;
        fastcgi_pass 127.0.0.1:9000;
        include fastcgi_params;

        set $real_script_name $fastcgi_script_name;
        set $path_info "";
        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;
        fastcgi_param PATH_INFO $path_info;
    }
}

第二份文件是用于配置80端口的

vim /etc/nginx/conf.d/80.conf
server {
    listen 80;
    server_name hid3rx.space;
    return 301 $scheme://www.hid3rx.space$request_uri;
}

server {
    listen 80;
    server_name www.hid3rx.space;
    return 301 https://www.hid3rx.space$request_uri;
}

运行nginx/php-fpm

systemctl start nginx
systemctl start php-fpm

进行安装
此时就可以打开网站链接进行安装了,过程很简单,这里就不再多讲

文件夹权限和SELinux的干扰
如果出现了安装不成功的情况,应该检查一下是不是网站文件的拥有者有没有设置成nobody。
再有就是SELinux权限,blog下所有文件的SELinux权限应与/var/www一致,并且/blog/usr下的所有文件的权限应为httpd_sys_rw_content_t,如果不会用SELinux的话也可以直接把它关掉

发表评论

电子邮件地址不会被公开。 必填项已用*标注