基于路由器的博客:(二)搭建nginx+php+mysql环境

上一章已经把HG255D的系统刷为OpenWrt,将环境搭建起来就可以上传博客的安装包了

安装软件

(我的固件已经将nginx、php编译进去了,这一步只需安装mysql、spawn和shadow相关的软件)

1
2
3
4
5
opkg update
opkg install block-mount #自动挂载U盘
opkg install mysql-server
opkg install spawn-fcgi zoneinfo-core zoneinfo-asia
opkg install shadow-groupadd shadow-useradd


创建账户、数据库文件夹等

1
2
3
4
5
groupadd www
useradd -g www www
mkdir /mnt/sda1/web #确保挂载了U盘
chown -R www:www /mnt/sda1/web
mkdir -p /mnt/sda1/data /mnt/sda1/data/mysql /mnt/sda1/data/tmp

修改/etc/my.cnf

1
2
datadir                = /mnt/sda1/data/mysql/
tmpdir = /mnt/sda1/data/tmp/

启动mysql

1
/etc/init.d/mysqld start

输入如下命令创建mysql的密码(记住账户密码)

1
/usr/bin/mysqladmin -u root password 123456

登陆数据库

1
mysql -u root -p

连接上mysql,创建数据库(aaaa随意写,记住即可,安装博客时需要填)

1
2
create database aaaa
\g

退出mysql

1
quit

修改/etc/nginx/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
user www www;
worker_processes 1;
pid /var/run/nginx.pid;
error_log /var/log/nginx_error.log;
events {
use epoll;
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
access_log off;

include vhost.conf;
}

修改/etc/nginx/fastcgi_params,解决Nginx文件类型错误解析漏洞

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
if ($request_filename ~* (.*)\.php) {
set $php_url $1;
}
if (!-e $php_url.php) {
return 403;
}

fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;

添加/etc/nginx/vhost.conf(端口需要修改成80以外的端口,我这里设置成82)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server {
listen 82;
server_name localhost;
root /mnt/sda1/web;
index index.html index.htm index.php default.php;
error_page 500 502 503 504 /50x.html;

location = /50x.html {
root html;
}

location ~* ^.+\.php(\/.*)*$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

修改/etc/php.ini

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
short_open_tag = on
#doc_root = "/mnt/www"

extension=ctype.so
extension=curl.so
extension=gd.so
extension=mbstring.so
extension=mcrypt.so
extension=mysql.so
extension=pdo.so
extension=pdo_mysql.so
extension=session.so
extension=sockets.so
extension=tokenizer.so
extension=xml.so

[Date]
date.timezone = Asia/Shanghai

[MySQL]
mysql.default_socket = /var/run/mysqld.sock

启动spawn-fcgi(如需开机自动运行,可将命令添加到/etc/rc.local)

1
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 2 -f /usr/bin/php-cgi

启动nginx

1
2
/etc/init.d/nginx enable
/etc/init.d/nginx start

接着就可以上传类似emlog、TypeCho等博客安装程序了