欢迎光临
我们一直在努力

openwrt安装nginx+php+mysql教程

opkg update

opkg install php5 php5-mod-gd php5-mod-session php5-mod-pdo php5-mod-pdo-mysql php5-mod-mysql php5-mod-mcrypt php5-mod-mbstring php5-fastcgi php5-cgi php5-mod-xml php5-mod-ctype php5-mod-curl php5-mod-exif php5-mod-ftp php5-mod-iconv php5-mod-json php5-mod-sockets php5-mod-sqlite3 php5-mod-tokenizer php5-mod-zip

opkg install mysql-server

opkg install nginx spawn-fcgi zoneinfo-core zoneinfo-asia

opkg install shadow-groupadd shadow-useradd

groupadd www
useradd -g www www
chown -R www:www /mnt/sda1/web

修改/etc/my.cnf

datadir                = /mnt/sda1/data/mysql/

tmpdir                = /mnt/sda1/data/tmp/

创建数据库文件夹

mkdir -p /mnt/sda1/data /mnt/sda1/data/mysql /mnt/sda1/data/tmp

创建默认的数据库

/usr/bin/mysql_install_db --force

启动mysql

/etc/init.d/mysqld start

输入如下命令创建mysql的密码

/usr/bin/mysqladmin -u root password 123456

登陆数据库

mysql -u root -p

连接上mysql之后,创建数据库(可以自己定义,记住就行了,之后会用到)

create database abcd
g

查看当前可用数据库

show databases
g

退出mysql

quit

修改/etc/nginx/nginx.conf

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文件类型错误解析漏洞

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

server {
listen       80;
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

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)

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

启动nginx

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

附送一个owncloud私有云的nginx配置文件。

#参考http://www.right.com.cn/forum/thread-89216-1-1.html安装组件,还需安装php5-mod-dom、php5-mod-simplexml
#由于cpu运算性能不足,只能运行owncloud4.5系列版本,想被坑爹的可以去用owncloud5.0系列版本
#如果owncloud程序是刚上传的,建议修改owncloud文件夹权限chown -R www:www /mnt/sda1/web/owncloud
#这个是/etc/nginx/vhost.conf配置
#如果没有按照下面的设置owncloud WebDAV等,会出现owncloud布局混乱

####################################################
#               owncloud
####################################################
server {
listen       80;  #修改你需要的端口,因为uhttpd已经使用80端口,这里无法使用80端口,或者你修改uhttpd的端口
server_name  localhost; #你的服务器域名,你可以填写你的动态域名
root  /mnt/sda1/web/owncloud; #网站目录
index index.php;
client_max_body_size 20M; # 最大上传文件大小 还需要改php.ini的upload_max_filesize

# 不允许访问的目录
location ~ ^/(data|config|.ht|db_structure.xml|README) {
deny all;
}

# default try order
location / {
try_files $uri $uri/ @webdav;
}

# owncloud WebDAV
location @webdav {
fastcgi_split_path_info ^(.+.php)(/.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

# enable php
location ~ .php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
赞(0) 打赏
未经允许不得转载:NixonLi博客 » openwrt安装nginx+php+mysql教程

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏