openwrt搭建WEB服务器(LNMP)
opkg update
-------------------------------------------------------------------
准备: 安装 USB/挂载/分区 工具
-------------------------------------------------------------------
opkg install kmod-usb-core
opkg install kmod-usb2
opkg install kmod-usb-ohci
opkg install kmod-usb-storage
opkg install kmod-fs-ext4
opkg install e2fsprogs
opkg install mount-utils
opkg install cfdisk
-----------------------------------------------------------------------------------------
第一步 磁盘分区/增加虚拟内存swap/网站根目录挂载mount/环境变量path
-----------------------------------------------------------------------------------------
cfdisk /dev/sda
mkswap /dev/sda1
mkfs.ext4 /dev/sda2
mkdir -p /mydev
mount /dev/sda2 /mydev
vi /etc/config/fstab
-----------------------
配置内容:
config swap
option device /dev/sda1
option enabled
config mount
option target /mydev
option device /dev/sda2
option fstype ext4
option options rw,sync
option enabled 1
-----------------------
echo dest usb /mydev/bg >> /etc/opkg.conf
cat /etc/opkg.conf
vi /etc/profile
---------------
配置内容:
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/mydev/bg/usr/bin:/mydev/bg/usr/sbin:/mydev/bg/usr/lib:/mydev/bg/etc
export LD_LIBRARY_PATH=/mydev/bg/usr/bin:/mydev/bg/usr/sbin:/mydev/bg/usr/lib:/lib:/mydev/bg/etc
---------------
-----------------------------------------------------------------
第二步 安装mysql
-----------------------------------------------------------------
opkg install mysql-server
vi /etc/my.cnf
--------------
配置内容:
datadir = /mydev/data/mysql/
tmpdir = /mydev/data/tmp/
bind-address =0.0.0.0
--------------
mkdir /mydev/data
mkdir /mydev/data/mysql
mkdir /mydev/data/tmp
mysql_install_db --force
/etc/init.d/mysqld enable
/etc/init.d/mysqld start
/usr/bin/mysqladmin -u root password XXXXXX
---------------------------------------
安装网站数据库时要用到的sql语句
mysql -u root -p
show databases;
create database xxx;
---------------------------------------
------------------------------------------------------------------
第三步 添加用户组
------------------------------------------------------------------
opkg install shadow-useradd shadow-groupadd
mkdir /mydev/www
groupadd myweb
useradd -g myweb webadmin
chown -R webadmin:myweb /mydev/www
------------------------------------------------------------------
第四步 安装nginx服务器
------------------------------------------------------------------
opkg install nginx
配置/etc/nginx/(配置好见附件)
/etc/init.d/nginx enable
/etc/init.d/nginx start
------------------------------------------------------------------
第五步 安装spawn-fcgi
------------------------------------------------------------------
opkg install spawn-fcgi
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 2 -f /usr/bin/php-cgi
------------------------------------------------------------------
第六步 安装php
------------------------------------------------------------------
opkg install php5
opkg install php5-mod-pdo-mysql php5-mod-pdo php5-mod-mysql
opkg install php5-mod-gd php5-mod-session
opkg install php5-fastcgi php5-cgi
opkg install php5-mod-xml php5-mod-json
opkg install php5-mod-tokenizer
opkg install php5-mod-mcrypt php5-mod-mbstring php5-mod-ctype php5-mod-curl php5-mod-exif php5-mod-ftp php5-mod-iconv php5-mod-sockets
vi /etc/php.ini
---------------
配置内容:
short_open_tag = on
;doc_root = "/www"
extension=mysql.so
......
[Date]
date.timezone = Asia/Shanghai
[MySQL]
mysql.default_socket = /var/run/mysqld.sock
---------------
--------------------------------------------------------------------
配置Timezone
--------------------------------------------------------------------
修改openwrt主机时区:System〉System〉Timezone:Asia/Shanghai
opkg install zoneinfo-asia zoneinfo-core
至此,LNMP已搭建完成。
接下来你可以配置网站的访问端口,如果从80端口访问需要注意和路由器访问页面冲突;
安装网站cms系统请根据自己的路由器内存量力而为,本人的16M内存安装织梦运行轻松,使用火车头采集上传数据库也无压力,安装wordpress,虽然可以正常安装使用,但明显内存吃紧。
外网访问注意开放端口,并安装DDNS动态域名解析;
内网穿透可使用ngrokc。
--------------------------------------------------------------------
第七步 开放80端口
--------------------------------------------------------------------
vi /etc/config/firewall
---------------
配置内容:
config rule
option src wan
option dest_port 80
option target ACCEPT
option proto tcp
---------------
--------------------------------------------------------------------
配置uhttpd(避免网站页面和路由器登陆地址80端口冲突)
--------------------------------------------------------------------
vi /etc/config/uhttpd
list listen_http '0.0.0.0:80' 修改80端口
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第八步 安装mybb(轻量级cms)
wordpress、
dede、discuzx、phpwind等CMS皆可,但应考虑路由器内存,尽量选用占用内存小的------------------------------------------------------------------------------------------------------------------------------------------------------------------------
下载:
tar -xzvf mybb.tar.gz
解压后放在/mydev/www文件夹中,www是网站根目录,然后打开浏览器访问安装,要用到之前设置的数据库密码,/mydev/data是数据库文件夹
访问浏览器安装前先确保mysql,nginx已启动,可运行以下命令行启动mysql、nginx
/etc/init.d/mysqld enable
/etc/init.d/mysqld start
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 2 -f /usr/bin/php-cgi
/etc/init.d/nginx enable
/etc/init.d/nginx start
--------------------------------------------------------------------
第九步 安装ddns (此处是为了便于网站外网访问,如果只是局域网内访问可以不必操作)
--------------------------------------------------------------------
opkg install ddns-scripts luci-app-ddns
--------------------------------------------------------------------
注册ddns地址
--------------------------------------------------------------------
https://changeip.com
--------------------------------------------------------------------
第十步 启动
--------------------------------------------------------------------
swapon /dev/sda1
mount /dev/sda2 /mydev
/etc/init.d/mysqld enable
/etc/init.d/mysqld start
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 2 -f /usr/bin/php-cgi
/etc/init.d/nginx enable
/etc/init.d/nginx start
--------------------------------------------------------------------
备注:强制手动跟新ip
--------------------------------------------------------------------
https://用户名:密码@nic.changeip.com/nic/update?system=dyndns&hostname=主机名