VPSã§é㶠-ãã®ï¼ï¼ ï¼Ubuntué¯12.04ã«LXCãã¤ã³ã¹ã³ãã¦éç£åä»®æ³ç°å¢ãæ§ç¯ãã Part1ï¼åæå°å ¥ç·¨
çæ§ããã«ã¡ã¯ï¼
ä»æ¥ã¯LXCï¼Linux Containers http://lxc.sourceforge.net/ )ï¼lxc version: 0.7.5ï¼ãUbuntuåã«ä»è¾¼ãã§ã¿ãå®é¨ãè¡ãã¾ãã
åºæ¬çã«ã¯ããã®ãã«ãï¼https://help.ubuntu.com/12.04/serverguide/lxc.html ï¼ãå
å®ãã¦ã¦æ®ã©ããã§äºè¶³ãã¾ãããæ¥æ¬èªã®æ
å ±ãå°ãªãã®ã§ä¸å¿ç§ãè¨é²ãã¨ãã¾ãã
ï¼äºã Ubuntu Weekly Recipe第226åãLXCã§è»½éä»®æ³ç°å¢ã®æ´»ç¨ http://gihyo.jp/admin/serial/01/ubuntu-recipe/0226 ãèªãã§ããã¨ããããï¼
Part1ã¯ã¤ã³ã¹ãã¼ã«âã¨ããããç«ã¡ä¸ããæã¾ã§ã
Part2以éã¯éè¦ã¨æ¸ãæ°ãããã°ï¼LXCå
ã§VPNç°å¢æ§ç¯ã»ãã¡ã¤ã«å
±æé¯æ§ç¯çï¼ã
ãããã¯ã¼ã¯å¨ãããããã®ã§ã¤ãããªVPSã«ä»è¾¼ãã§ãã£ããã¼ããåã«ã¾ãã¯å¿ ããã¼ã«ã«ã®VirtualBoxå ã®ã¯ãªã¼ã³ã§ã¶ã£å£ãã¦ããUbuntu12.04ä»®æ³ç°å¢çã§ååã«å®é¨ãã¦ã¿ã¾ããããå°è¨å®æ¼ãã«ãã£ã¦ãã©ãã£ã¦ãç§ã¯ä¸åé¢ç¥ãããã¾ãããå ¨ã¦èªå·±è²¬ä»»ã§ã
(2012/08/28追è¨ï¼
VirtualBoxã¨LXCãåãæ¯è¦ä¸ã§åæã«åããã¨ãVirtualBoxå ã®OSãèµ·åããã¨ãã«æ¯è¦ä¸¸ãã¨ããªã¼ãºããã®ã§ãã©ã¡ããä¸æ¹ã ãåããã¹ããï¼VirtualBoxå ã§åãã¦ããUbuntuå ã«ä»è¾¼ãã LXCã®èµ·åã¯åé¡ç¡ããæ¯è¦ä¸ã«ä¸¦åã§VirtualBoxã¨LXCãèµ·åãããã¨åç¾ï¼
ããªã¼ãºåç¾ç°å¢ï¼
- VirtualBox4.1.16ã4.1.20
- LXC lxc version: 0.7.5
- æ¯è¦Ubuntu 12.04 64bit (corei3,corei5ãã·ã³ã®å®æ©2å°ã§åç¾ãVT-xãBIOSã§æå¹ãã¤VirtualBoxã§ãæå¹ï¼VTç¡å¹ã§ã®åç¾å®é¨ã¯ã¾ã ï¼)
(2012/08/28追è¨ãããï¼
ï¼ãããã®VPS2G(vps)å
LXCç°å¢å
Ubuntu(vpsvm0)ã«xrdpãå
¥ãã¦vpsvm0ï¼ã®GUIç°å¢ï¼âsshãã³ãã«âvpsâsshãã³ãã«âWin7ãã·ã³ã®ãªã¢ã¼ããã¹ã¯ãããã§GUIãããã£ã¦ãå³ï¼
A:ã¤ã³ã¹ãã¼ã«æé
Ubuntu 12.04ã ã¨è¶ ç°¡åã§æåæãæé ï¼ï¼lxcã®ã¤ã³ã¹ãã¼ã«
root@xut:~# apt-get install lxc ããã±ã¼ã¸ãªã¹ããèªã¿è¾¼ãã§ãã¾ã... å®äº ä¾åé¢ä¿ããªã¼ãä½æãã¦ãã¾ã ç¶æ æ å ±ãèªã¿åã£ã¦ãã¾ã... å®äº 以ä¸ã®ç¹å¥ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ããã¾ã: bridge-utils cgroup-lite cloud-utils debootstrap euca2ools libapparmor1 libyaml-0-2 python-boto python-m2crypto python-paramiko python-yaml ææ¡ããã±ã¼ã¸: btrfs-tools lvm2 qemu-user-static 以ä¸ã®ããã±ã¼ã¸ãæ°ãã«ã¤ã³ã¹ãã¼ã«ããã¾ã: bridge-utils cgroup-lite cloud-utils debootstrap euca2ools libapparmor1 libyaml-0-2 lxc python-boto python-m2crypto python-paramiko python-yaml ã¢ããã°ã¬ã¼ã: 0 åãæ°è¦ã¤ã³ã¹ãã¼ã«: 12 åãåé¤: 0 åãä¿ç: 0 åã 2,069 kB ã®ã¢ã¼ã«ã¤ããåå¾ããå¿ è¦ãããã¾ãã ãã®æä½å¾ã«è¿½å 㧠13.6 MB ã®ãã£ã¹ã¯å®¹éãæ¶è²»ããã¾ãã ç¶è¡ãã¾ãã [Y/n]? ï¼å¾ç¥ï¼
Yæ¼ãã¦ãºãããã£ã¨ã¤ã³ã¹ã³å®äºã
æé ï¼ï¼isc-dhcp-serverã®ã¤ã³ã¹ãã¼ã«
ã½ãã½ãå¢ããéç£åä»®æ³ç°å¢ãä½ãããã«LXCç°å¢ç¨ã®dhcpãµã¼ãã¼ãç«ã¦ã¾ãã
root@xut:~# apt-get install isc-dhcp-server ããã±ã¼ã¸ãªã¹ããèªã¿è¾¼ãã§ãã¾ã... å®äº ä¾åé¢ä¿ããªã¼ãä½æãã¦ãã¾ã ç¶æ æ å ±ãèªã¿åã£ã¦ãã¾ã... å®äº ææ¡ããã±ã¼ã¸: isc-dhcp-server-ldap 以ä¸ã®ããã±ã¼ã¸ãæ°ãã«ã¤ã³ã¹ãã¼ã«ããã¾ã: isc-dhcp-server ã¢ããã°ã¬ã¼ã: 0 åãæ°è¦ã¤ã³ã¹ãã¼ã«: 1 åãåé¤: 0 åãä¿ç: 0 åã 429 kB ã®ã¢ã¼ã«ã¤ããåå¾ããå¿ è¦ãããã¾ãã ãã®æä½å¾ã«è¿½å 㧠1,005 kB ã®ãã£ã¹ã¯å®¹éãæ¶è²»ããã¾ãã åå¾:1 http://ftp.riken.jp/Linux/ubuntu/ precise-updates/main isc-dhcp-server amd64 4.1.ESV-R4-0ubuntu5.2 [429 kB] 429 kB ã 0ç§ ã§åå¾ãã¾ãã (701 kB/s) ããã±ã¼ã¸ãäºåè¨å®ãã¦ãã¾ã ... Selecting previously unselected package isc-dhcp-server. (ãã¼ã¿ãã¼ã¹ãèªã¿è¾¼ãã§ãã¾ã ... ç¾å¨ 238643 åã®ãã¡ã¤ã«ã¨ãã£ã¬ã¯ããªãã¤ã³ã¹ãã¼ã«ããã¦ãã¾ãã) (.../isc-dhcp-server_4.1.ESV-R4-0ubuntu5.2_amd64.deb ãã) isc-dhcp-server ãå±éãã¦ãã¾ã... man-db ã®ããªã¬ãå¦çãã¦ãã¾ã ... ureadahead ã®ããªã¬ãå¦çãã¦ãã¾ã ... isc-dhcp-server (4.1.ESV-R4-0ubuntu5.2) ãè¨å®ãã¦ãã¾ã ... Generating /etc/default/isc-dhcp-server... isc-dhcp-server start/running, process 13449 isc-dhcp-server6 stop/pre-start, process 13498
ã¤ã³ã¹ãã¼ã«ã¯ä»¥ä¸ï¼ãªãã¦ç°¡åãªãã§ãããã
B:è¨å®ãã¡ã¤ã«æ¢è¨ªï¼å ¨ã¦æ¯è¦å´ï¼
lxcã®è¨å®ãã¡ã¤ã«ã¯ãããã¸ãã«ããã¾ãã1:/etc/lxcä»¥ä¸ ï¼ç¢ºèªã®ã¿ï¼
root@xut:/# tree -F /etc/lxc /etc/lxc âââ auto/ã (èªåèµ·åã³ã³ããæå®ç¨ãã£ã¬ã¯ããªï¼ âââ lxc.conf (lxcå ±éè¨å®ãã¡ã¤ã«ï¼ 1 directory, 1 file
root@xut:/# cat /etc/lxc/lxc.conf lxc.network.type=veth lxc.network.link=lxcbr0 lxc.network.flags=up root@xut:/#
ç¹ã«å¼ãå¿ è¦ãªãã
2:/etc/init/以ä¸ï¼ç¢ºèªã®ã¿ï¼
root@xut:/etc/init# ls|grep lxc lxc-net.conf ï¼lxc networkã®èµ·åã¹ã¯ãªããï¼ lxc.conf (lxcã®èµ·åã¹ã¯ãªããï¼ root@xut:/etc/init#
lxc-net.confå ã§ã¯
echo 1 > /proc/sys/net/ipv4/ip_forward
ã ã®
ï¼ä¸ç¥ï¼ # set up the lxc network echo 1 > /proc/sys/net/ipv4/ip_forward mkdir -p ${varrun} brctl addbr ${LXC_BRIDGE} ifconfig ${LXC_BRIDGE} ${LXC_ADDR} netmask ${LXC_NETMASK} up iptables -A POSTROUTING -s ${LXC_NETWORK} -t nat -j MASQUERADE dnsmasq -u lxc-dnsmasq --strict-order --bind-interfaces --pid-file=${varrun}/dnsmasq.pid --conf-file= --listen-address ${LXC_ADDR} --dhcp-range ${LXC_DHCP_RANGE} --dhcp-lease -max=${LXC_DHCP_MAX} --dhcp-no-override --except-interface=lo --interface=${LXC_BRIDGE} || cleanup touch ${varrun}/network_up ï¼å¾ç¥ï¼
ã ã®iptableså¨ãã®è¨å®ãå ¨é¨ãä»»ãã§ãã£ã¦ããã¦ã¾ããç¹ã«å¼ãå¿ è¦ãªãã
ããLXCã³ã³ããï¼192.168.40.50 portçªå·10222ï¼ã«ç´æ¥ã°ãã¼ãã«IPã¢ãã¬ã¹ããæ¥ç¶ãããæã«ã¯
iptables -A PREROUTING -t nat -p tcp --dport 10222 -i eth0 -j DNAT --to 192.168.40.50:10222
çãä¸è¨ iptables -A POSTROUTING è¡ã®æ¬¡ãããã«ã¨ãããã追è¨ãã¦ï¼ï¼ ufw allow 10222 ãæ¯è¦ã»LXCã³ã³ããåæ¹ã§å®è¡ï¼ãã¾ãã°è¯ãã§ããããï¼192.168.40.50ã®LXCã³ã³ããã®sshã®ãã¼ãã10222çªã«ãã¦ããå ´åçï¼
3:/etc/default/lxc ï¼å¿
è¦ã«å¿ãã¦è¦ç·¨éï¼
LXCã®ãããã¯ã¼ã¯(ããªãã¸æ¥ç¶ï¼å¨ãã®è¨å®ãã¡ã¤ã«ã§ãã
root@xut:/etc/default# cat lxc # MIRROR to be used by ubuntu template at container creation: # Leaving it undefined is fine #MIRROR="http://archive.ubuntu.com/ubuntu" # or #MIRROR="http://<host-ip-addr>:3142/archive.ubuntu.com/ubuntu" # LXC_AUTO - whether or not to start containers symlinked under # /etc/lxc/auto LXC_AUTO="true" # Leave USE_LXC_BRIDGE as "true" if you want to use lxcbr0 for your # containers. Set to "false" if you'll use virbr0 or another existing # bridge, or mavlan to your host's NIC. USE_LXC_BRIDGE="true" # If you change the LXC_BRIDGE to something other than lxcbr0, then # you will also need to update your /etc/lxc/lxc.conf as well as the # configuration (/var/lib/lxc/<container>/config) for any containers # already created using the default config to reflect the new bridge # name. # If you have the dnsmasq daemon installed, you'll also have to update # /etc/dnsmasq.d/lxc and restart the system wide dnsmasq daemon. # LXC_BRIDGE="lxcbr0" # LXC_ADDR="10.0.3.1" # LXC_NETMASK="255.255.255.0" # LXC_NETWORK="10.0.3.0/24" # LXC_DHCP_RANGE="10.0.3.2,10.0.3.254" # LXC_DHCP_MAX="253" # LXC_SHUTDOWN_TIMEOUT=120 LXC_BRIDGE="lxcbr0" LXC_ADDR="192.168.40.1" LXC_NETMASK="255.255.255.0" LXC_NETWORK="192.168.40.0/24" LXC_DHCP_RANGE="192.168.40.2,192.168.40.254" LXC_DHCP_MAX="253" LXC_SHUTDOWN_TIMEOUT=120 root@xut:/etc/default#
ç¹ã«å¼ãå¿
è¦ãªããï¼âã®ä¾ã§ã¯LXC_ADDRå¨ããå¼ã£ã¦ã¾ããï¼
ï¼lxcbr0ã§ããªãã¸æ¥ç¶ãããããªãå ´åã¨ããLXC_ADDRãå¤ãããå ´åã«ç·¨éï¼
4:/etc/default/ufw ï¼å¿
è¦ã«å¿ãã¦è¦ç·¨éï¼
ã©ã¤ããªUbuntu使ããªã ufwï¼Ubuntuã®ã½ããã¦ã§ã¢ãã¡ã¤ã¢ã¦ã©ã¼ã«ï¼ã使ã£ã¦iptalbesã®è¨å®ããã¦ããçã§ããufwãæå¹ãªå ´åã¯
DEFAULT_FORWARD_POLICY="ACCEPT"
ã«ããå¿ è¦ãããã¾ããï¼ãã®è¨å®ãå¿ããã¨LXC_NETWORKã®å¤ã«åºããªãï¼
# /etc/default/ufw # ï¼ä¸ç¥ï¼ # Set the default forward policy to ACCEPT, DROP or REJECT. Please note that # if you change this you will most likely want to adjust your rules #DEFAULT_FORWARD_POLICY="DROP" DEFAULT_FORWARD_POLICY="ACCEPT" ï¼å¾ç¥ï¼
ï¼è¨å®ããªããããè¦ service ufw restartï¼
5:/etc/dhcp/dhcpd.confï¼è¦ç·¨éï¼
LXC_NETWORKç¨ã®DHCPé¯ã®è¨å®ã¯ã ããããããªæããï¼ããã§ã¯192.168.40.50ã192.168.40.120 ãå²ãå½ã¦ã¦ããï¼
ddns-update-style none; option domain-name "lxc.local"; option domain-name-servers 192.168.40.1; default-lease-time 600; max-lease-time 7200; authoritative; log-facility local7; subnet 192.168.40.0 netmask 255.255.255.0 { option routers 192.168.40.1; option subnet-mask 255.255.255.0; option broadcast-address 192.168.40.255; option domain-name lxc; option domain-name-servers 192.168.40.1; range dynamic-bootp 192.168.40.50 192.168.40.120; default-lease-time 7200; max-lease-time 43200; }
6: /etc/default/isc-dhcp-serverï¼è¦ç·¨éï¼
root@xut:~# cat /etc/default/isc-dhcp-server # Defaults for dhcp initscript # sourced by /etc/init.d/dhcp # installed at /etc/default/isc-dhcp-server by the maintainer scripts # # This is a POSIX shell fragment # # On what interfaces should the DHCP server (dhcpd) serve DHCP requests? # Separate multiple interfaces with spaces, e.g. "eth0 eth1". #INTERFACES="" INTERFACES="lxcbr0"
INTERFACES="lxcbr0"ã«éå®ãã¦ããã¾ãããã
ï¼è¨å®ããªããããè¦ service isc-dhcp-server restartï¼
7: /etc/dnsmasq.d/lxcï¼ç¢ºèªã®ã¿ï¼
root@xut:~# cat /etc/dnsmasq.d/lxc bind-interfaces except-interface=lxcbr0 root@xut:~#
C:LXCã³ã³ããï¼ä»®æ³ç°å¢ï¼ä½ææé
ã³ãã³ãä¸çºæµãã¦ãã¨ã¯è¦ã¦ãã ããï¼ååã®ã¿OSã«å¿ è¦ãªããã±ã¼ã¸ã®ãã¦ã³ãã¼ãã«åæ°åç¨åº¦æéããããã¾ãã2åç®ä»¥éã¯çéãï¼(ubuntuã®ææ°ç12.04ç°å¢ã vm0ã¨ããååã§ä½æãã¦ããä¾ï¼
root@xut:~# lxc-create -t ubuntu -n vm0 No config file specified, using the default config debootstrap 㯠/usr/sbin/debootstrap ã§ã Checking cache download in /var/cache/lxc/precise/rootfs-amd64 ... installing packages: vim,ssh Downloading ubuntu precise minimal ... I: Retrieving Release I: Retrieving Release.gpg I: Checking Release signature I: Valid Release signature (key id 630239CC130E1A7FD81A27B140976EAF437D05B5) I: Retrieving Packages I: Validating Packages I: Retrieving Packages I: Validating Packages I: Resolving dependencies of required packages... I: Resolving dependencies of base packages... I: Found additional base dependencies: libbsd0 libedit2 libgpm2 libgssapi-krb5-2 libk5crypto3 libkeyutils1 libkrb5-3 libkrb5support0 libpython2.7 libwrap0 openssh-client openssh-serv er vim-runtime I: Checking component main on http://archive.ubuntu.com/ubuntu... I: Retrieving adduser I: Validating adduser ï¼ä¸ç¥ï¼ Processing triggers for resolvconf ... locale: Cannot set LC_CTYPE to default locale: No such file or directory locale: Cannot set LC_MESSAGES to default locale: No such file or directory locale: Cannot set LC_ALL to default locale: No such file or directory invoke-rc.d: policy-rc.d denied execution of start. Processing triggers for initramfs-tools ... Download complete Copy /var/cache/lxc/precise/rootfs-amd64 to /var/lib/lxc/vm0/rootfs ... Copying rootfs to /var/lib/lxc/vm0/rootfs ... ## # The default user is 'ubuntu' with password 'ubuntu'! # Use the 'sudo' command to run tasks as root in the container. ## 'ubuntu' template installed 'vm0' created root@xut:~#
ã¤ã³ã¹ãã¼ã«ãçµäºãã㨠/var/lib/lxc/vm0ï¼vm0ï¼ä»»æã®ä»®æ³ç°å¢åã»ã³ã³ããåï¼ ä»¥ä¸ã«ãããªæãã§ä»®æ³ç°å¢ã®ãã¡ã¤ã«ç¾¤ãä½æããã¾ãã
root@xut:/var/lib/lxc/vm0# ll åè¨ 20 drwxr-xr-x 3 root root 4096 7æ 31 22:52 ./ drwxr-xr-x 3 root root 4096 7æ 31 22:41 ../ -rw-r--r-- 1 root root 1249 7æ 31 22:52 config ï¼vm0ã®è¨å®ãã¡ã¤ã«ï¼ -rw-r--r-- 1 root root 110 7æ 31 22:52 fstab drwxr-xr-x 22 root root 4096 7æ 31 22:51 rootfs/ ï¼vm0ã®ã«ã¼ããã£ã¬ã¯ããªï¼ root@xut:/var/lib/lxc/vm0# find ./ -maxdepth 2 ./ ./fstab ./rootfs ./rootfs/opt ./rootfs/tmp ./rootfs/lib ./rootfs/proc ./rootfs/bin ./rootfs/sys ./rootfs/root ./rootfs/var ./rootfs/srv ./rootfs/home ./rootfs/sbin ./rootfs/lib64 ./rootfs/media ./rootfs/selinux ./rootfs/run ./rootfs/dev ./rootfs/boot ./rootfs/mnt ./rootfs/etc ./rootfs/usr ./config root@xut:/var/lib/lxc/vm0#
/var/lib/lxc/vm0/configã§vm0åºæã®è¨å®ãè¨è¿°ãã¾ããï¼é常ã¯ç¹ã«ç·¨éã®å¿ è¦ãªãï¼
root@xut:/var/lib/lxc/vm0# cat config lxc.network.type=veth lxc.network.link=lxcbr0 lxc.network.flags=up lxc.network.hwaddr = 00:16:3e:e5:9f:2e lxc.utsname = vm0 lxc.devttydir = lxc lxc.tty = 4 lxc.pts = 1024 lxc.rootfs = /var/lib/lxc/vm0/rootfs lxc.mount = /var/lib/lxc/vm0/fstab lxc.arch = amd64 lxc.cap.drop = sys_module mac_admin lxc.pivotdir = lxc_putold # uncomment the next line to run the container unconfined: #lxc.aa_profile = unconfined lxc.cgroup.devices.deny = a # Allow any mknod (but not using the node) lxc.cgroup.devices.allow = c *:* m lxc.cgroup.devices.allow = b *:* m # /dev/null and zero lxc.cgroup.devices.allow = c 1:3 rwm lxc.cgroup.devices.allow = c 1:5 rwm # consoles lxc.cgroup.devices.allow = c 5:1 rwm lxc.cgroup.devices.allow = c 5:0 rwm #lxc.cgroup.devices.allow = c 4:0 rwm #lxc.cgroup.devices.allow = c 4:1 rwm # /dev/{,u}random lxc.cgroup.devices.allow = c 1:9 rwm lxc.cgroup.devices.allow = c 1:8 rwm lxc.cgroup.devices.allow = c 136:* rwm lxc.cgroup.devices.allow = c 5:2 rwm # rtc lxc.cgroup.devices.allow = c 254:0 rwm #fuse lxc.cgroup.devices.allow = c 10:229 rwm #tun lxc.cgroup.devices.allow = c 10:200 rwm #full lxc.cgroup.devices.allow = c 1:7 rwm #hpet lxc.cgroup.devices.allow = c 10:228 rwm #kvm lxc.cgroup.devices.allow = c 10:232 rwm root@xut:/var/lib/lxc/vm0#
D:LXCã³ã³ããèµ·å確èª
ã³ã³ã§ä¸æ¦æ¯è¦ï¼è¦ªæ©ï¼èªä½ããªãã¼ããã¾ããããï¼ãªãã¼ãç´å¾ã«æ¯è¦ã§ifconfig -aãã¦ã¿ãå³ï¼
root@xut:~# ifconfig -a eth0 Link encap:ã¤ã¼ãµããã ãã¼ãã¦ã§ã¢ã¢ãã¬ã¹ 08:00:27:fa:a8:39 inetã¢ãã¬ã¹:192.168.25.40 ããã¼ããã£ã¹ã:192.168.25.255 ãã¹ã¯:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 ã¡ããªãã¯:1 RXãã±ãã:317 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã¬ã¼ã :0 TXãã±ãã:422 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã£ãªã¢:0 è¡çª(Collisions):0 TXãã¥ã¼é·:1000 RXãã¤ã:27923 (27.9 KB) TXãã¤ã:149231 (149.2 KB) lo Link encap:ãã¼ã«ã«ã«ã¼ããã㯠inetã¢ãã¬ã¹:127.0.0.1 ãã¹ã¯:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 ã¡ããªãã¯:1 RXãã±ãã:2 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã¬ã¼ã :0 TXãã±ãã:2 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã£ãªã¢:0 è¡çª(Collisions):0 TXãã¥ã¼é·:0 RXãã¤ã:100 (100.0 B) TXãã¤ã:100 (100.0 B) lxcbr0 Link encap:ã¤ã¼ãµããã ãã¼ãã¦ã§ã¢ã¢ãã¬ã¹ 7a:33:e0:87:e5:0e inetã¢ãã¬ã¹:192.168.40.1 ããã¼ããã£ã¹ã:192.168.40.255 ãã¹ã¯:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 ã¡ããªãã¯:1 RXãã±ãã:0 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã¬ã¼ã :0 TXãã±ãã:2 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã£ãªã¢:0 è¡çª(Collisions):0 TXãã¥ã¼é·:0 RXãã¤ã:0 (0.0 B) TXãã¤ã:108 (108.0 B) root@xut:~#
lxcbr0ãæ°ãã«åºæ¥ã¦ãã¾ããï¼/etc/default/lxcãç·¨éããã®ã§ããã®ä¾ã§ã¯inetã¢ãã¬ã¹:192.168.40.1 ã«ãªã£ã¦ãããæ¯è¦ã®eth0ã¯192.168.25.40ï¼
ã¤ã³ã¹ã³ããvm0ãèµ·åãã¦ã¿ã¾ãã
èµ·åã¯
lxc-start -n vm0
ã¾ãã¯
lxc-start -n vm0 -d
ããå¾ã§
lxc-console -n vm0
ã·ã£ãããã¦ã³ã¯
lxc-shutdown -n vm0
å¼·å¶åæ¢ã¯
lxc-stop -n vm0
ã§ãã
ï¼èµ·åããå³ï¼
ï¼vm0ç°å¢ã§ã®ifconfig -aï¼
root@vm0:~# ifconfig -a eth0 Link encap:Ethernet HWaddr 00:16:3e:e5:9f:2e inet addr:192.168.40.50 Bcast:192.168.40.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:17 errors:0 dropped:0 overruns:0 frame:0 TX packets:11 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:1594 (1.5 KB) TX bytes:1338 (1.3 KB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) root@vm0:~# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface default 192.168.40.1 0.0.0.0 UG 100 0 0 eth0 192.168.40.0 * 255.255.255.0 U 0 0 0 eth0 root@vm0:~#
dhcpã§ã¡ããã¨192.168.40.50ãå²ãå½ã¦ãããããã§ãã
(vm0ãèµ·åããå¾ã§ã®æ¯è¦å´ã®ifconfig -a)
root@xut:/home/xut# ifconfig -a eth0 Link encap:ã¤ã¼ãµããã ãã¼ãã¦ã§ã¢ã¢ãã¬ã¹ 08:00:27:fa:a8:39 inetã¢ãã¬ã¹:192.168.25.40 ããã¼ããã£ã¹ã:192.168.25.255 ãã¹ã¯:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 ã¡ããªãã¯:1 RXãã±ãã:14750 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã¬ã¼ã :0 TXãã±ãã:7298 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã£ãªã¢:0 è¡çª(Collisions):0 TXãã¥ã¼é·:1000 RXãã¤ã:16875022 (16.8 MB) TXãã¤ã:2053244 (2.0 MB) lo Link encap:ãã¼ã«ã«ã«ã¼ããã㯠inetã¢ãã¬ã¹:127.0.0.1 ãã¹ã¯:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 ã¡ããªãã¯:1 RXãã±ãã:2 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã¬ã¼ã :0 TXãã±ãã:2 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã£ãªã¢:0 è¡çª(Collisions):0 TXãã¥ã¼é·:0 RXãã¤ã:100 (100.0 B) TXãã¤ã:100 (100.0 B) lxcbr0 Link encap:ã¤ã¼ãµããã ãã¼ãã¦ã§ã¢ã¢ãã¬ã¹ 3a:9f:33:53:15:e1 inetã¢ãã¬ã¹:192.168.40.1 ããã¼ããã£ã¹ã:192.168.40.255 ãã¹ã¯:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 ã¡ããªãã¯:1 RXãã±ãã:3144 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã¬ã¼ã :0 TXãã±ãã:11494 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã£ãªã¢:0 è¡çª(Collisions):0 TXãã¥ã¼é·:0 RXãã¤ã:160963 (160.9 KB) TXãã¤ã:16614801 (16.6 MB) vethR9OLBE Link encap:ã¤ã¼ãµããã ãã¼ãã¦ã§ã¢ã¢ãã¬ã¹ 3a:9f:33:53:15:e1 UP BROADCAST RUNNING PROMISC MULTICAST MTU:1500 ã¡ããªãã¯:1 RXãã±ãã:3144 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã¬ã¼ã :0 TXãã±ãã:11501 ã¨ã©ã¼:0 æ失:0 ãªã¼ãã©ã³:0 ãã£ãªã¢:0 è¡çª(Collisions):0 TXãã¥ã¼é·:1000 RXãã¤ã:204979 (204.9 KB) TXãã¤ã:16615107 (16.6 MB) root@xut:/home/xut#
(注æç¹ï¼
åæã¦ã¼ã¶ã¼å:ubuntuããã¹ã¯ã¼ããubuntuãªã®ã§
å³å¹sudo adduser hogehogekun ã§å¥ã®ä»»æã®ã¦ã¼ã¶ã¼hogehogekunãä½ã£ã¦
/etc/groupã®
sudo:x:27:ubuntuã
sudo:x:27:hogehogekunã«ãã¤ã¤
sudo deluser ubuntuãã¦ãã¾ãã¾ãããã
ãã¨ã¯apt-get install ufw;ufw enable ããå¾ã§å¥½ãã«å¼ãåãã¹ãã
lxc-*ã³ãã³ãã®ä¸è¦§ã¯
https://help.ubuntu.com/12.04/serverguide/lxc.html#lxc-admin ãããã«æ¸ãã¦ããã¾ãã®ã§ä¸èªãã¦ã¿ãã°ã¨ãã·
ä»æ¥ã¯ããã¾ã§ãPart2ã«ç¶ãããã
ï¼ãã®åã«ããã¯ã¢ããæé é¢é£ã¨ã³ããªã¼ã®å®çµç·¨ãå
ãâ¦ï¼
ã§ã¯ã§ã¯ï¼
VPSã§é㶠-ãã®ï¼ï¼ ï¼darã¨mysqldumpãã¤ãã£ã¦ãµã¼ãã¼ã®æ次ãã«ããã¯ã¢ããã¨æ¥æ¬¡å·®åããã¯ã¢ãããåãï¼ä¸ç·¨ï¼ãã¼ã«ã«é¯å´ã¸ã®è»¢éã¹ã¯ãªããã®å·»ï¼
ã¿ãªããããã«ã¡ã¯ãæ¬æ¥ã¯ä¸ç·¨ï¼ãã¼ã«ã«é¯å´ã¸ã®è»¢éã¹ã¯ãªããã®å·»ã§ãã(åç·¨ï¼VPSé¯å´ã¯ãã¡ã)
転ã°ã¬å
ã®æã転ãã å ´åã¯æ¬¡ãã転ã°ãªãããã«ããã«å¯¾çãã¾ãããï¼ä½ãåä½å®é¨ã¯ä¸å¯§ã«ãï¼
ãã¼ã«ã«é¯ï¼æå å´ï¼ã¹ã¯ãªãã
ãã¼ã«ã«ï¼æå ï¼å´ã®ããã¯ã¢ããã¹ã¯ãªããã¯ãããªæããã¹ã¯ãªãã5:rootã¦ã¼ã¶ã¼ã®crontab -e
45 06 * * * /root/bin/backup_sasv01.sh >/backup/remote/sasv01.log 2>&1 #55 06 * * * /root/bin/backup_sasv02.sh >/backup/remote/sasv02.log 2>&1
å®éã¯ãã®ãã¼ã«ã«(æå å´ï¼ã®èªé¯èªä½ã®ããã¯ã¢ããã¹ã¯ãªããããååã¨ã³ããªã¼ã®ã¹ã¯ãªãã1ã®æ§ã«èµ°ããã¦ãã¾ãã説æã§ã¯çç¥ãã¾ãã
VPSé¯ã®æ次ï¼æ¥æ¬¡ã®ããã¯ã¢ãããã¡ã¤ã«éã3æ-4æå°ã«ä½æå®äºããæå ã®èªé¯èªä½ã®ããã¯ã¢ãããå®äºããå¾ã®é åããè¦è¨ãã£ãæ©æ6æå°é ã«
/root/bin/backup_sasv01.sh /root/bin/backup_sasv02.sh
â¦ã¨ãã£ã/root/bin/backup_VPSå´ãµã¼ãã¼å.shãé 次起åãã¦ãã¾ãã
VPSé¯ãå¢ãããã¯ã¢ãã対象ãå¢ããååã ãã
- mkdir -p /backup/remote/sasv01/dailybk;
- mkdir -p /backup/remote/sasv01/dar_file;
- chmod -R 600 /backup/remote/sasv01
ã®æ§ã«ããã¯ã¢ãããã¡ã¤ã«è¨ç½®ç¨ãã£ã¬ã¯ããªãæåã§ä½æãã¦ãã¾ãã
ã¾ãã/backup/ã®ãã¼ããã·ã§ã³çãæ°ãã¤ãã¦ãã ãããï¼å¥é¯ã®æ å ±ãè©°ã¾ã£ãéè¦ãã¡ã¤ã«ãªã®ã§chmod 600 /backupã§root以å¤ä¸ç¨æã«è¦ããªãããã«ãã¦ããï¼
ãã£ã¬ã¯ããªæ§æã®å³
/backup/local/以ä¸ãæå
ã®èªé¯ã®ããã¯ã¢ããä¿åé å
/backup/remote/VPSå´ãµã¼ãå1/,/backup/remote/VPSå´ãµã¼ãå2/ 以ä¸ãVPSé¯å´ã®ä¿åé å
ã¹ã¯ãªãã6:/root/bin/backup_VPSå´ãµã¼ãã¼å1.sh
å¤ããã¡ã¤ã«ãåé¤ãã¦rsyncã§è»¢éãã¦ãã ãã§ããï¼èªè¨¼éµã®ã¿ã§ã®sshæ¥ç¶çã¯æ¢ã«åºæ¥ãæ§ã«ãªã£ã¦ããåæã§ããï¼
å¤ããã¡ã¤ã«ãåé¤ããããããã¨ãåé¤ããªãããä¿åæ¥æ°ãä½æ¥ã«ãããçã¯ç°å¢ã«ãã£ã¦å¤ãã£ã¦ããã¨æãã¾ãã
ç§ã®å ´åããããæ´ã«é常ã¯ç¹ãã§ããªãå¤ä»ãHDDã«æ¯æ1度æ¥ç¶ãã¦rsync(deleteç¡ãï¼ããã¦åãå¤ããå¥ã®å ´æã«ä¿ç®¡ãã¦ãã¾ãã
ï¼ï¼æéè¦ãã¡ã¤ã«ã¯ç©ççã«é¢ããå¥ã®å ´æã«ã転éï¼å¥é¯ã«ãæå·åãã¦ä¿ç®¡ãã¦ãã¾ãã
å°éé·ç«äºæ°´å®³çªçã§ã®ç ´æç´å¤±ãèãéè¦åº¦ã«ãããã¾ããæé«5éä½ã®ä¿®å¾©ã«ã¼ãã«ãã¦ãã¾ããï¼
#!/bin/sh NOW=`date +%Y%m%d` BASE_DIR="/backup/remote/sasv01/dailybk" #VPSå´ãµã¼ãåã®ãã£ã¬ã¯ããªã«å¿ ãæ¸ãæãã LOCALDAR_DIR="/backup/remote/sasv01/dar_file" #VPSå´ãµã¼ãåã®ãã£ã¬ã¯ããªã«å¿ ãæ¸ãæãã PORT="10222" #VPSãµã¼ãã¼å´ãã¼ãçªå·ãå¿ ãæ¸ãæããã(æ¨æº22) SVUSR="[email protected]" #VPSã«sshæ¥ç¶ããã¦ã¼ã¶ã¼å@æ¥ç¶å (sshã¦ã¼ã¶ã¼å@IPã¢ãã¬ã¹)å ¬ééµèªè¨¼ã§ãã¼ãã¹ã¯ã¼ãæ¥ç¶å¯è½ãªããã¯ã¢ãããã¡ã¤ã«è»¢éå°ç¨ã¦ã¼ã¶ã¼ãä½æãã¦ããããã®ã¦ã¼ã¶ã¼ã®æä½åºæ¥ãç¯å²ãéå®ãããå ´åã¯opensshå´è¨å®ã§è¦èª¿æ´ï¼è§£èª¬çç¥ï¼ LOCAL_DIR="$BASE_DIR/$NOW" #ãã¼ã«ã«ï¼æå ï¼å´ã®æ¥ä»æ¯ãã£ã¬ã¯ã㪠#VPSãµã¼ãã¼å´ã®ããã¯ã¢ããä¿åç¨ãã£ã¬ã¯ããªãåå¨ããªãå ´åã¯ãããã¦èªåã§ä½ããªãã§å¦ççµäº if [ ! -e "$BASE_DIR" ]; then echo "$BASE_DIR not exists ,exit" exit 1 fi if [ ! -e "$LOCALDAR_DIR" ]; then echo "$LOCALDAR_DIR not exists ,exit" exit 1 fi #æ¬æ¥ã®æ¥ä»yyyymmddã®ãã£ã¬ã¯ããªãåå¨ããªãå ´åã¯çæãåå¨ããå ´åã¯ãããå¦ççµäº if [ ! -e "$LOCAL_DIR" ]; then mkdir $LOCAL_DIR else echo 'dir exists' exit 1 fi cd $BASE_DIR #96æ¥ä»¥ä¸æã«touchãããdailybké ä¸ã®20*ã§å§ã¾ããã£ã¬ã¯ããªã¯èªååé¤ #ï¼VPSå´ã§ã¯31æ¥ä¿ç®¡ãæå å´ã§ã¯95æ¥ä¿ç®¡ï¼ #æå å´ãã£ã¹ã¯å®¹éãèãæä½32æ¥ä»¥ä¸ã§åèªè¦èª¿æ´ find $BASE_DIR -mtime +96 -type d -name "20*"|xargs rm -rf #96æ¥ä»¥ä¸æã«touchããã*.darãã¡ã¤ã«ã¯èªååé¤ find $LOCALDAR_DIR -mtime +96 -type f -name "*.dar"|xargs rm -f #ãªã¢ã¼ãï¼VPSãµã¼ãã¼å´ï¼ããrsyncã§mysqlã®ãã³ããã¡ã¤ã«ã¨darã®ã¢ã¼ã«ã¤ããã¡ã¤ã«ãåä¿¡ nice -n 19 rsync -auvz -e "ssh -p $PORT" $SVUSR:/backup/local/dailybk/ $LOCAL_DIR nice -n 19 rsync -auvz -e "ssh -p $PORT" $SVUSR:/backup/local/dar_file/ $LOCALDAR_DIR
- æå ã«ãã96æ¥ä»¥ä¸æã«touchããããã¡ã¤ã«ã¯åé¤ããrsync(deleteãªãã·ã§ã³ç¡ãï¼ã§VPSãµã¼ãå´ã®ã¿ã«ãããã¡ã¤ã«ã転éãã¦ãã¾ãã
- mysqlã®ãã³ããã¡ã¤ã«ã¯VPSå´ã«ã¯ææ°1æ¥åããããã¯ã¢ãããåãã¦ããªãã§ããããã¼ã«ã«å´ã§ã¯96æ¥ä¿ç®¡ã¨ãã¦ãã¾ããï¼96=3ã¶æã¨ã¡ãã£ã¨ï¼æ°æ¥åã¯ãã¼ã¸ã³ï¼
- å³å¯ã«ã¯å®å ¨ã®ããã«ååã®ã¹ã¯ãªãã3ãæ¹é ãVPSå´ã«7æ¥åãæå å´ã«96æ¥åä¿ç®¡ã®æ§ã«ããã»ãããæå å´é¯è½ã¡ã«ãããã¡ã¤ã«è»¢éæ¼ãã§ã®mysqldumpããã¯ã¢ããæ¯æããé²æ¢ã§ããã®ã§è¯ããã¨æãã¾ãã
- ç§ã®å ´åVPSã®ãã£ã¹ã¯å®¹éãå°ãªãï¼ãã©ã³ã¶ã¯ã·ã§ã³ç¡ãã®éçã«è¿ãCMSã³ã³ãã³ããããªãï¼ä½ãé¯ã«ãã£ã¦ã¯DBã®æ°ã¯å¤ãã®ã§å®¹éã¯é£ãï¼ï¼ææªã§ãæ次+æ¥æ¬¡å·®ådarã®ä¸ã«å«ã¾ãã¦ãã/var/lib/mysql/DBå/* ãããã¼ã¿ãæ»ããããmysqldumpã«ã¤ãã¦ã¯ãã®ããã«ãªã£ã¦ãã¾ãã
- åæ¥ã¨ã³ããªã¼ã®ã¹ã¯ãªãã3ã®ã³ã¡ã³ãã«å¾ãVPSå´ã«ã3æ¥åmysqlã®ãã³ããä¿åããããã«ããå ´åã«ã¯ãæ¯æ¥çæããã /backup/remote/VPSé¯å/dailybk/yymmdd/ 以ä¸ã«3æ¥åã®mysql_dailybk_mysqldump_yyyymmdd.tar.gzããä¿åãããããã«ãªãã¾ããVPSå´ã»ãã¼ã«ã«å´ã®ç©ºã容éãååã«ããå ´åã«ã¯ãã³ãã¼ã®å¤ããã¡ãã®ã»ããããå®å ¨ã§ãã
å¾ç·¨(ããã¯ã¢ããï¼è»¢éåä½ç¢ºèªï¼ããã¯ã¢ããããã®å¾©å 確èªç·¨ï¼ã«ç¶ãã
ä»æ¥ã¯å¿ããã®ã§ããã¾ã§ã
VPSã§é㶠-ãã®ï¼ï¼ ï¼darã¨mysqldumpãã¤ãã£ã¦ãµã¼ãã¼ã®æ次ãã«ããã¯ã¢ããã¨æ¥æ¬¡å·®åããã¯ã¢ãããåãï¼åç·¨ï¼VPSé¯å´ã§ãã³ãä½æã®å·»ï¼
ã¿ãªãã¾ãä¹
ãã¶ãã§ãã
ã©ããã®ã¬ã³ãµãå±ããã¼ã¿ã¼ããµã£é£ã°ããã¨èãã¾ããã
æãã§ããã¼ãé¯å±ãªãã¦ä¿¡ç¨ã§ãã¾ãããã¼ãèªè¡ãå¿
è¦ã§ããã¼ã
ã¨ããããã§å®é¯ã»VPSé¯ã使ã人ã
ï¼ã«éããçã
æ§ï¼ã¯èªåã®æå
ã«å¿
ãæ¥æ¬¡ããã¯ã¢ãããæã£ã¦ããã¾ãããã
ï¼åºæ¥ãã°éé»ããªãç°å¢ãç©ççã«é¢ããç°å¢ã«ã³ãã¼âå¥ã®å ´æã«æ°ä¸ä»£åã¾ã§å®æçã«ä¿åããã¦ããã¾ããããHDDãªãã¦ã¿ãã¿ãããªããã ãã
ç§ã¯æ±äº¬å¤§éªã¢ã¡ãªã«è¥¿æµ·å²¸ã«åæ£ããã¦ãã¾ãã
ãã¨æéè¦ãã¼ã¿ã®æ°GBã¯ãã¹ã¯ã¼ãä»ãå§ç¸®ï¼æå·åãã£ã¹ã¯ã¤ã¡ã¼ã¸ã«ä¿åãããã¤ãmicrosdã«å
¥ãã¦å¿µã«ã¯å¿µãå
¥ãã¦ãã¾ãã
èªè¡ãéè¦ãä»äººã®å®å ¨å®£è¨ã¯ä¿¡ãããªãä¿¡ããããã®ã¯èªåã ããï¼å²ã¨ãã¸ï¼
ããã¾é¢ä¿ãªããã©HDDä¸èª¿æã¯fsckæããåã«ddrescueã§ãã£ã¹ã¯ã¤ã¡ã¼ã¸ã®ã¯ãã¼ã³ãä½ã£ãããã¦ãã£ã¹ã¯ã¤ã¡ã¼ã¸å´ãæä½ãã¾ããããâ¦
ã¦ã«ãã¦ã¼ã¶ã¼åãããã¯ã¢ããã¹ã¯ãªããå ¬é
ãã¼ã¿æµåºãæããã©ãã¼ã¿æ¹æ¶ããã®ãããæããã¨ããããã§ä»æ¥ã¯ç§ã®ä½¿ã£ã¦ããVPSé¯å ãã¡ã¤ã«ããã¯ã¢ããã¹ã¯ãªãããå ¬éãã¦ããã¾ãã
ãã©ã³ã¶ã¯ã·ã§ã³å¦ççããªããªãã£ã¦ããé¯ã¯å¯¾è±¡ã§ã¯ããã¾ããããæ£ããéç¨ãããã¨ã§å ¨ãã¼ã¿ãã¹ã復æ§ä¸å¯ï¼¼(^o^)ï¼ï½µï¾ï¾ãé¿ãããã¨ã¯åºæ¥ãã¨æãã¾ããï¼ã ãããè¯ãæãã«æ»ããä½ãå®å ¨å®ç§ã§ã¯ãªãããã©ã³ã¶ã¯ã·ã§ã³ãã¼ã¿ã¯ã©ã£ãã«æ¶ãããããã¨ããæããï¼
ï¼ã¤ãã§ã«/etc以ä¸ã¨ãã¯mercurialãå°å ¥ãã¦å±¥æ´ç®¡çããã¦ããã¾ããããé¯å±ãä¿¡ããæ´æ°å·®åããã¤ã§ãåããããã«ãã¦ãããã¨ãéè¦ã ã¨æãã¾ããï¼
ã¾ããã¼ã¿ã¼ãã¹ãï¼ããã¯ã¢ãããã¼ã¨ã§çµããä¼ç¤¾ãªãã¦æãç«ã£ã¦ãã»ããå¥è·¡ã§ãããï¼ãã¼ã«ã·ããã§é»ç£æ³¢ãäºã å°çã®å±æ©ã¬ã¼ã¨ãããããã®ã¯å¥ãããããªã£ããç´ã¨ãç³çã¨ãå£é ä¼æ¿ããæ®ããªãããï¼
ã注æï¼
ãã®ã¹ã¯ãªããã®éç¨ãã¹ã»å°æ¥ã®ããã°ã©ã æ§é ã®å¤åãªã©ã§ããã¯ã¢ããã¹ã¯ãªãããåãããã¼ã¿ãé£ã°ãã¦ãç§ã¯ä¸å責任ãæã¡ã¾ããã
å·®åããã¯ã¢ãããè¨ãã¦ãã£ã¹ã¯ãã«ã§ãµã¼ãã¼ãåæ¢ãã¦ãç¥ãã¾ããã
å®æçã«ããã¯ã¢ãããåãã¦ãããã®ç¢ºèªã»å¾©æ§åºæ¥ããã®ç¢ºèªã»ãã£ã¹ã¯å®¹éã足ãã¦ãããã®ç¢ºèªçã¯ããªãã®ä»äºã§ãã
ããã¾ã§ãææ³ã®ç´¹ä»ã§ãã®ã§èªå·±è²¬ä»»ã§ãããã«æ¸ãã¦ãããã¨ãéµåã¿ã«ããªãã§ãã ããã
åææ¡ä»¶
åºæ¬çãªã¤ã³ã¹ãã¼ã«ã«ã¤ãã¦ã¯çç¥ãã¾ããã¾ããæå ï¼èªå® çï¼ã«ãã¼ã¿ã転éããããã¯ã¢ããç¨ã¦ã¼ã¶ã¼ã¯ãã¹ãã¬ã¼ãºç¡ãã®å ¬ééµèªè¨¼ã§sshæ¥ç¶ãã¾ããããã¡ãã®èª¬æãçç¥ãã¾ããï¼ããããªãå ´åã¯å¯¾è±¡å¤ã§ãã®ã§ä»¥ä¸ã®ã¹ã¯ãªããã¯ä½¿ããªãã§ãã ãããï¼
- ä¸æ¥ä¸åã®ç§æçãªããã¯ã¢ããã§è¯ããï¼ãªã¢ã«ã¿ã¤ã ã«ããã¯ã¢ãããããã»ãã©ã³ã¶ã¯ã·ã§ã³ããªããªãªç°å¢ã¯å¯¾è±¡å¤ãé²è¦§ä¸»ä½ã®PHP-MySQL製CMSãå®ç©VPSã«çªã£è¾¼ãã ç°å¢ãæ³å®ãï¼
- Linuxã§ããã(Ubuntu 10.04LTS)
- sh(bash)ãåãã
- ã³ãã³ãã©ã¤ã³ã§PHPãåããï¼å¼ã³åºãé¨åãPHPã§ä½ã£ã俺俺ã¹ã¯ãªãããªã®ã§ãï¼
- dar(ãã£ã¹ã¯ã¢ã¼ã«ã¤ã)ãã¤ã³ã¹ãã¼ã«ãã¦ããã( aptitude install dar )
- 使ã£ã¦ããDBã¯mysqlã§ããã(postgresql対å¿çããããã©ä»åã¯ç°¡åã®ããã«çç¥ï¼
- VPSãµã¼ãã¼ã®ç°å¢æ§ç¯ä½èªåã§åºæ¥ãã
- ãã¼ã«ã«ââVPSãµã¼ãã¼éã§å ¬ééµèªè¨¼ãã¤ãã¼ãã¹ã¯ã¼ãã§æ¥ç¶ã§ããããã¯ã¢ãããã¡ã¤ã«è»¢éå°ç¨ã¦ã¼ã¶ã¼ãä½æåºæ¥ãã
- æå ï¼ãã¼ã«ã«ï¼ã¾ãã¯å¥ä¼ç¤¾ã«Linuxé¯ããã£ã¦rsyncãåºæ¥ãã
- ä¸ã¶æåã®å·®åããã¯ã¢ãããã¡ã¤ã«ãä¿åå¯è½ãªååãªãã£ã¹ã¯ç©ºã容éãVPSå´ããã¼ã«ã«å´åæ¹ã«åå¨ããã
ã¨ããè¦ã¯VPSã¨ãå°ç¨é¯ã®ç°å¢ã§ããå ±æã¬ã³ãµãã¯å¯¾è±¡å¤ã§ãã
dar (http://dar.linux.free.fr/) ã®æ¦è¦ã«ã¤ãã¦ã®èª¬æãçãã¾ãããdar ããã¯ã¢ãããã§ã°ã°ã£ã¦å 人ã®è§£èª¬ãã¼ã¸ãè¦ã¦ãã ããã
darã®ä½ãå¬ãããç°¡åã«ããã¨ã
- ãã«ããã¯ã¢ããã1ãã¡ã¤ã«ã«å§ç¸®ãã¦ä¿åã§ããã
- ãã«ããã¯ã¢ãããå ã«å·®åããã¯ã¢ãããä½æã§ããã®ã§æ¥æ¬¡ã§ã¯å·®åã®ã¿ä½æãã¦è² è·ãæ¸ãããã
- ããã¯ã¢ãã対象å¤ã»å§ç¸®å¯¾è±¡å¤çã®ç´°ããªå¶å¾¡ãå¯è½ã§ããã
ã£ã¦ãã¨ã§ãã
æ¦è¦
ãã£ã¦ããã¨ã¯åç´ã§å³ã§æ¸ãã¨ãããªæããVPSé¯å´ã§æ¯æ¥ï¼æä¸åãã«ããã¯ã¢ãããæ¯æ¥å·®åããã¯ã¢ããï¼ã®ããã¯ã¢ãããã¡ã¤ã«ãçæãã¦ä¿åãã¦ãã
â
ä¿åãããã¡ã¤ã«ãæå ã®ãµã¼ãã¼å´èµ·åã§rsyncã§åå¾ãããï¼æå ã®ãµã¼ãã¼èµ·åãªã®ã§ãæå å´ãåºå®IPãããªãã¦ãOKï¼
- crontabèµ·åã®PHPã¹ã¯ãªããããdarã使ã£ã¦VPSãµã¼ãã¼å ãã¡ã¤ã«ã®ããã¯ã¢ãã
- mysqldumpã使ã£ã¦DBã®ããã¯ã¢ãã
- crontabèµ·åã®rsyncã§æå å´ãµã¼ãã¼ã¨ããã¯ã¢ãããã¡ã¤ã«ã®åæ
ã¦ãªãããã§ãã
ãããªæãã§æ¯æ1æ¥ã«ãã¡ã¤ã«ã®ãã«ããã¯ã¢ããããã以å¤ã®æ¯æ¥ã¯ãã«ããã¯ã¢ããããã®å·®åããã¯ã¢ãããã¦ãã¾ãã
å·®åããã¯ã¢ããã¯å¤åããªããã°æ®ã©å®¹éãé£ãã¾ããã
ã¹ã¯ãªãã
以ä¸ã«ã¹ã¯ãªãããè²¼ã£ã¦ããã¾ããæ¬æ¥ã¯VPSãµã¼ãã¼å´ãæå å´ãµã¼ãã¼ã«rsyncããé¨åã«ã¤ãã¦ã¯ä¸ç·¨ã»å¾ç·¨ã§ã
ãã£ã¹ã¯ã®ããã¯ã¢ããèªä½ã¯ã«ã¼ãã¦ã¼ã¶ã¼ã«å®è¡ããã¾ãã
ï¼ä¸è¬ã¦ã¼ã¶ã¼ã®èªããé åã®ã¿ããã¯ã¢ããã§ãããªã好ãã«ãã¦ãã ãããï¼
ã¹ã¯ãªãã1ï¼VPSãµã¼ãã¼å´ãrootã¦ã¼ã¶ã¼ã®crontab -e
# m h dom mon dow command 10 03 * * * /root/bin/mysql-backup.sh >/dev/null 2>&1 40 03 1 * * /root/bin/dar_backup.php -f > /backup/local/dar.log 2>&1 40 03 2-31 * * /root/bin/dar_backup.php -d > /backup/local/dar.log 2>&1
mysql-backup.shãdar_backup.php ãããã¯ã¢ããã¹ã¯ãªããã§ãã/root/bin/ã«è¨ç½®ãã¦ãã¾ãã
- æ¯æ¥ã®mysql-backup.shã¯mysqlã®ãã³ããã¡ã¤ã«ã®ããã¯ã¢ããã
- æ¯æ1æ¥ã¯darã®ãã«ããã¯ã¢ããã
- æ¯æ2æ¥ã31æ¥ã¯darã®å·®åããã¯ã¢ãããï¼ãã«ããã¯ã¢ããã¨ã®å·®åãªã®ã§æ¯æ¥ã®å·®ã大ããã¨ãã£ã¹ã¯ãã«ã«ãªãå±éºãã注æï¼
ããããªã£ã¦ãã¾ãã
ã¹ã¯ãªãã2ï¼VPSãµã¼ãã¼å´ãããã¯ã¢ãããã¡ã¤ã«ä¿åãã£ã¬ã¯ããªã®ä½æã¹ã¯ãªãã
ãã®ã¹ã¯ãªããã¯ããã¯ã¢ããç°å¢æ§ç¯æã®ååã®ã¿ä½¿ãã¾ãã
/root/bin/mkbackupdir.sh
#!/bin/sh mkdir -p /backup/local/dailybk mkdir -p /backup/local/dar_file mkdir -p /backup/local/mysql mkdir -p /backup/local/psql chmod 600 /backup chown -R backupusr:users /backup chmod 600 /backup/local/dailybk chmod 600 /backup/local/dar_file chmod 600 /backup/local/mysql chmod 600 /backup/local/psql
backupusrã¯ãVPSãµã¼ãã¼å´ã®ããã¯ã¢ãããã¡ã¤ã«rsyncç¨ã®å°ç¨ã¦ã¼ã¶ã¼ã§ããä»»æã®ã¦ã¼ã¶ã¼ãä½ã£ã¦ãã ããã
ããã¯ã¢ãããããã¡ã¤ã«ã¯root権éã¦ã¼ã¶ã¼ã¨backupusrã®ã¿ãèªã¿åãå¯è½ã§ãã
/backupã¨ãã¦ãã¾ããããã¯æããã¯ã¢ããç¨ãã¼ãã£ã·ã§ã³ãåãã¦ããåæ®ã§ããç¹ã«æ·±ãæå³ã¯ããã¾ããã
/backup/local/ãªã®ã¯ç¸äºããã¯ã¢ãããããã¨ãã« /backup/remote/ã¨ã /bakup/ç¸æé¯å/ ã¨ãã®æ§æã«ããå ´åãããããã§ãã
ã¹ã¯ãªãã3ï¼VPSãµã¼ãã¼å´ãDBæ¯ã®mysqldumpå©ç¨ã®ãã³ããã¡ã¤ã«çæã¹ã¯ãªãã
ï¼2012/06/25 VPSå´ã«ã3æ¥åä¿åããå ´åã®ä¾ãã³ã¡ã³ãã¢ã¦ããã¦è¿½å ï¼$BACKIDIRãæ¯åãã£ã¬ã¯ããªåé¤ããã®ãããã¦$BACKIDIRå
ãã¡ã¤ã«åé¤ã«å¤æ´ï¼
/root/bin/mysql-backup.sh (è¦chmod 700)
#!/bin/bash PATH=/usr/local/sbin:/usr/bin:/bin NOW=`date +%Y%m%d` # ããã¯ã¢ããå ãã£ã¬ã¯ã㪠BACKDIR=/backup/local/mysql #空ç½ä¸å¯æ³¨æ DAILY_BACKDIR=/backup/local/dailybk #空ç½ä¸å¯æ³¨æ # MySQLrootãã¹ã¯ã¼ã ROOTPASS=hogehgoerootpass # backupï¼è»¢éç¨ï¼ã®ã¦ã¼ã¶ã¼ BACKUPUSR=backupusr # ããã¯ã¢ããå ãã£ã¬ã¯ããªå åæ¥ãã³ãåé¤ find $BACKIDIR -type f -name "*.dump.sql.gz"|xargs rm -f chmod -R 600 $BACKDIR chmod -R 600 $DAILY_BACKDIR touch $BACKDIR touch $DAILY_BACKDIR # ãã¼ã¿ãã¼ã¹ååå¾ DBLIST=`ls -p /var/lib/mysql | grep / | tr -d /` # ãã¼ã¿ãã¼ã¹ãã¨ã«ããã¯ã¢ãã for dbname in $DBLIST do nice -n 19 mysqldump -uroot -p$ROOTPASS --default-character-set=binary $dbname | nice -n 19 gzip > $BACKDIR/$dbname.dump.sql.gz; chmod 600 $BACKDIR/$dbname.dump.sql.gz; done #dumpãã¡ã¤ã«ãå§ç¸®ãã¦ä¿åãVPSå´ã«ã3æ¥åä¿åããå ´åã¯ä¸äºè¡ã®æ¹ãæå¹ã« nice -n 19 tar zcf $DAILY_BACKDIR/mysql_dailybk_mysqldump.tar.gz $BACKDIR #find $DAILY_BACKDIR -type f -mtime +2 -name "mysql*dailybk*.gz" |xargs rm -f #nice -n 19 tar zcf $DAILY_BACKDIR/mysql_dailybk_mysqldump_$NOW.tar.gz $BACKDIR chmod 600 $DAILY_BACKDIR/mysql*dailybk*.gz chown $BACKUPUSR:users $DAILY_BACKDIR/mysql*dailybk*.gz chown -R $BACKUPUSR:users $BACKDIR chown -R $BACKUPUSR:users $DAILY_BACKDIR
ROOTPASS(mysqlã®rootã¦ã¼ã¶ã¼ã®ãã¹ã¯ã¼ãï¼ãBACKUPUSRã¯é©å®å¤æ´ãã¦ãã ããã
ï¼mysqlã®DBå
ããDBåãåå¾ãã/var/lib/mysqlã®ãã£ã¬ã¯ããªããDBåãåå¾ãã¦ãã®ã§ä¹±æ´ã§ããåãã®ã§OKï¼
ããã§ã¯
- mysqldumpãããããåããªãããã¼ã¿ãã¼ã¹æ¯ã«ãã³ããã¡ã¤ã«ãä½æãã¦
- /backup/local/mysql/以ä¸ã«ææ°ãã³ããDBæ¯ã«ä¿åãã¦
- ã¤ãã§ã«$DAILY_BACKDIR/mysql_dailybk_mysqldump.tar.gz ã«åºãã¦ä¿åã
- èªã¿åã権éã¨ææè ã®å¤æ´ããã¦ãã¾ãã
ã¾ã次åãããèµ·åæã«ååããããã¡ã¤ã«ã®ãã³ãã¯/backup/local/mysql/ãã¾ããã¨rm -rf ãã¦ãã¾ãã
ã¹ã¯ãªãã4ï¼VPSãµã¼ãã¼å´ãdarã®ãã£ã¹ã¯ã¢ã¼ã«ã¤ãçæã¹ã¯ãªãã
/root/bin/dar_backup.php (è¦chmod 700)
#!/usr/bin/php -q <?php if (!isset($argv[1])){ echo "å¼æ°ãããã¾ãã -f or -d \n"; exit; } $ptn = $argv[1]; //backupãã¡ã¤ã«ã®èªVPSå è¨ç½®å ´æ define("log_path", "/backup/local/darbk.log"); define("bk_base", "/backup/local/dar_file"); /** * #VPSãµã¼ãã¼å´ããã®rsyncã¯è¡ããããã¼ã«ã«å´ããã®rsyncã«èµ·åæ¹æ³ãå¤æ´ããããå»æ¢ * define("sync_use",false) ; * define("sync_to","remote-back@example.com:/backup/remote/mysvname/") ; */ define("nowtime", time()); // dar ã®èµ·åãªãã·ã§ã³ã-Z以ä¸ã¯è² è·åæ¸ã®ããäºéã«å§ç¸®ããªããã¡ã¤ã«ãï¼http://dar.linux.free.fr/doc/man/dar.html $option = <<<___END___ -v -D -m 256 -y9 -Z "*.gz" -Z "*.Z" -Z "*.bz2" -Z "*.tgz" -Z "*.lzh" -Z "*.zip" -Z "*.jpg" -Z "*.JPG" -Z "*.jpeg" -Z "*.JPEG" -Z "*.png" -Z "*.PNG" -Z "*.gif" -Z "*.GIF" -Z "*.mpg" -Z "*.MPG" -Z "*.mpeg" -Z "*.MPEG" -Z "*.mp3" -Z "*.rm" -Z "*.jar" -Z "*.exe" -Z "*.EXE" -Z "*.msi" -Z "*.MSI" -Z "*.divx" -Z "*.flv" -Z "*.mov" -Z "*.wma" -Z "*.iso" -Z "*.img" -Z "*.ISO" -Z "*.IMG" -Z "*.swf" -Z "*.slc" -Z "*.ts" -Z "*.class" ___END___; $option = preg_replace("/\r/iu", " ", $option); $option = preg_replace("/\n/iu", " ", $option); define("option_param", $option); // ããã¯ã¢ãã対象ã®ãã£ã¬ã¯ããª(/ç´ä¸ã®ãã£ã¬ã¯ããªåã®ã¿æå®å¯è½ï¼ãé¤å¤ãã£ã¬ã¯ããªã¯"p"=>array("dir1","dir2",⦠),ã§æå®ã $bk_arr = array( // array("t"=>"backup"), array("t" => "bin"), array("t" => "boot"), array("t" => "dev"), array("t" => "etc"), array("t" => "home"), array("t" => "lib"), array("t" => "lost+found"), array("t" => "media"), array("t" => "misc"), array("t" => "mnt"), // array("t"=>"net"), array("t" => "opt"), // array("t"=>"proc"), array("t" => "root"), array("t" => "sbin"), array("t" => "selinux"), array("t" => "srv"), // array("t"=>"sys"), array("t" => "tmp"), array("t" => "usr", "p" => array("local/delegate")), // ããã§ã¯/usr/local/delegeteã¯é¤å¤ãã¦ãã array("t" => "var", "p" => array("cache", "spool/squid", "log", "named/chroot/proc")), // ããã§ã¯ /var/cache, /var/spool/squid, /var/logçãé¤å¤ãã¦ããããã£ã¹ã¯ç©ºã容éã®é¢ä¿ãã/var/logã¯rsyncãã¦ãã®ã§é¤å¤ ); if ("-f" === $ptn || "-d" === $ptn){ $msg = date("Y-m-d H:i:s", nowtime) . " Backup START\n"; echo $msg ; switch ($ptn){ case "-f":// æ次ç¨ãã«ããã¯ã¢ãã $pre_cmd = get_cmd_list("full", null, true); $cmd_list = get_cmd_list("full", $bk_arr); break; case "-d":// æ¥æ¬¡ç¨å·®åããã¯ã¢ãã $pre_cmd = get_cmd_list("diff", null, true); $cmd_list = get_cmd_list("diff", $bk_arr); break; default: $cmd_list = array(); break; } foreach($pre_cmd as $k => $v){ echo $v . "\n"; system($v); //32æ¥ä»¥ä¸æã®darãã¡ã¤ã«ã®åé¤ } foreach($cmd_list as $k => $v){ echo $v . "\n"; system($v); //darããã¯ã¢ããã®é 次å®è¡ } $msg = date("Y-m-d H:i:s", time()) . " Backup END \n"; echo $msg ; } /** * #VPSãµã¼ãã¼å´ããã®rsyncã¯è¡ããããã¼ã«ã«å´ããã®rsyncã«èµ·åæ¹æ³ãå¤æ´ããããå»æ¢ * if(sync_use){ * if("-f"===$ptn||"-d"===$ptn||"-r"===$ptn){ * $msg = date("Y-m-d H:i:s",time()). " Rsync START \n"; * echo $msg ; * $cmd = "rsync -auvz -e ssh --delete ".bk_base." ".sync_to." " ; * system($cmd); * $msg = date("Y-m-d H:i:s",time()). " Rsync END \n"; * echo $msg ; * } * } */ // ä¸é£ã®ã³ãã³ãã®åççæ function get_cmd_list($ptn, $bk_arr, $pre_flg = false) { $cmd_arr = array(); $diffstr = ""; if ($pre_flg){ // 32æ¥ä»¥ä¸æã®darãã¡ã¤ã«ã®åé¤ã³ãã³ãçæ $cmd = "for i in `/usr/bin/find " . bk_base . ' -name "*full*.dar" -mtime +32 ` ; do /bin/rm -f $i ; done ; ' ; $cmd_arr[] = $cmd; $cmd = "for i in `/usr/bin/find " . bk_base . ' -name "*diff*.dar" -mtime +32 ` ; do /bin/rm -f $i ; done ; ' ; $cmd_arr[] = $cmd; }else{ // darããã¯ã¢ããã®é 次å®è¡ã³ãã³ãçæ foreach($bk_arr as $k => $v){ $dname = $v["t"]; $fullpath_h = bk_base . "/" . $dname . "-full-"; $bkpath_h = bk_base . "/" . $dname . "-" . $ptn . "-"; $bkpath = $bkpath_h . date("YmdHi"); $tgt = " -R /{$dname} "; $no_tgt = ""; if ("diff" === $ptn){ $cmd = "/bin/ls -tr " . $fullpath_h . '*.dar|tail -n1 ' ; echo $cmd . "\n" ; $lsf = system($cmd); if (!$lsf){ // echo " -----basefile none_a\n" ; continue; } $lsf = preg_replace("/^(.+)\.\d+\.dar$/iu", "\\1", $lsf); $diffstr = " -A " . $lsf ; } if (isset($v["p"])){ foreach($v["p"] as $k2 => $v2){ $no_tgt .= " -P " . $v2 ; } } $tgt .= $no_tgt ; $cmd_arr[] = " nice -n 10 dar " . option_param . " -c {$bkpath} {$tgt} {$diffstr} "; } } return $cmd_arr; } exit; ?>
$option ã®ãªãã·ã§ã³ãã©ã¡ã¼ã¿ã¯http://dar.linux.free.fr/doc/man/dar.html ã man darãä»ã®æ¹ã®dar解説ãµã¤ããã確èªãã ããã
- y9ã¯å§ç¸®ã¬ãã«
- Zã§å§ç¸®ä¸è¦ãã¡ã¤ã«ãå§ç¸®å¯¾è±¡ã«ããªãã
ã¿ãããªã®ã1è¡æ¯ã«è¨è¿°ãã¾ãã
$bk_arr ã®é
åé¨åã§ããã¯ã¢ãã対象ã®/ç´ä¸ã®ãã£ã¬ã¯ããªåã¨ãããã¯ã¢ããé¤å¤å¯¾è±¡ã®ãã£ã¬ã¯ããªåãæå®ãã¾ãã
ä¾ãã°
array("t"=>"usr","p"=>array("local/delegate")),
ã®é¨åã§ã¯/usrãããã¯ã¢ãã対象ã«å«ãããã/usr/local/delegeteã¯ããã¯ã¢ãã対象ããé¤å¤ãã¦ãã¾ãã
get_cmd_list()
ã®é¨åã§ã¯darãã¡ã¤ã«ã®ä½æã³ãã³ãã¨ã32æ¥ä»¥ä¸æã®darãã¡ã¤ã«ã®åé¤ã³ãã³ããçæãã¦ãã¾ãã
- /root/bin/dar_backup.php -fã§å¼ã°ããå ´åã«ã¯ãã«ããã¯ã¢ããã
- /root/bin/dar_backup.php -dã§å¼ã°ããå ´åã«ã¯ææ°ã®ãã«ããã¯ã¢ããããã¼ã¹ã«å·®åããã¯ã¢ããã
çæããããã«ãã¦ãã¾ãã
32æ¥ä»¥ä¸æã®darãã¡ã¤ã«ãåé¤ããã¨ã6/1ã«ãã«ããã¯ã¢ãããåã£ãå¾ã® 5/28ã®å·®åããã¯ã¢ããã¨ãã¯ã¯ã£ããè¨ã£ã¦ã´ãã§ãããã®ãããã¯å¥½ãã«èª¿æ´ãã¦ãã ããã以åã¯VPSå é¨å´ä¿åæéã96æ¥ã«ãã¦ããã®ã§ãããã£ã¹ã¯å®¹éã®é¢ä¿ã§æ念ãã¾ãããï¼VPSå´å é¨ã§ã¯31æ¥ä¿æããæå ã«è»¢éããã¨ãã«å·¥å¤«ãã¦æå ã§ã¯3ã¶ææ®ãæ§æã«ããã®ãè¯ããã¨æãã¾ããï¼
ä¸ã§ã´ãã§ã´ãã§ãªããããããåãã¦ãã¾ããããã§$optionã¨$bk_arrã®ãã©ã¡ã¼ã¿ãã
nice -n 10 dar -v -D -m 256 -y9 -Z "*.gz" -Z "*.Z" -Z "*.bz2" -Z "*.tgz" -Z "*.lzh" -Z "*.zip" -Z "*.jpg" -Z "*.JPG" -Z "*.jpeg" -Z "*.JPEG" -Z "*.png" -Z "*.PNG" -Z "*.gif" -Z "*.GIF" -Z "*.mpg" -Z "*.MPG" -Z "*.mpeg" -Z "*.MPEG" -Z "*.mp3" -Z "*.rm" -Z "*.jar" -Z "*.exe" -Z "*.EXE" -Z "*.msi" -Z "*.MSI" -Z "*.divx" -Z "*.flv" -Z "*.mov" -Z "*.wma" -Z "*.iso" -Z "*.img" -Z "*.ISO" -Z "*.IMG" -Z "*.swf" -Z "*.slc" -Z "*.class" -c /backup/local/dar_file/var-diff-201206230037 -R /var -P cache -P spool/squid -P log -P named/chroot/proc -A /backup/local/dar_file/var-full-201206010340
ã®æ§ãªåªæãä½ã£ã¦ãã¾ããç§å¦ã¨ãªãã¨ãããã¨ãã§ç©èªã¯å§ã¾ãï¼ã¨ããç§å¦ã®ã¤ã³ãªãã¨ãããï¼
ä¸è¨ã®ã¹ã¯ãªãããåããã¦ã¿ã
åããã¨ãããªæãã§ããããã¨ããã¯ã¢ãããã¡ã¤ã«ãçæããã¾ããmysqlã®ããã¯ã¢ããã¯ãã£ã¹ã¯å®¹éç¯ç´ã®ããã«ææ°åã®ã¿ãä¿åããã¦ãã¾ãã
ï¼æå å´é¯ã¸ã®è»¢éæã«æ¥ä»ãã¨ãã£ã¬ã¯ããªãä½æãã¦ãã¡ã¤ã«ãä¿ç®¡ãã¦ãã¾ããï¼
ãã£ã¹ã¯å®¹é15GBã®VPSã§/backup以ä¸ã¯1.6GBä½ã§ãã
ï¼æ¥æ¬¡å·®åéã«ãã£ã¦å¤ãã£ã¦ãã¾ããããã¯æ®ã©æ´æ°ã®ç¡ããµã¼ãã¼ï¼
ç¶ãã¯ä¸ç·¨ï¼æå ï¼ãã¼ã«ã«ï¼é¯å´ç·¨ã¸ã
ï¼åã®è¯ã人ã¯ããã ãã§ãªãã¨ããªãã¨æãã¾ããå¾ç·¨ã¯å®éã«åããã¦rsyncããã話ï¼ããã¯ã¢ããããã®å¾©å ä¾ã®ã話ã§ããï¼
ã§ã¯ã§ã¯ï¼
VPSã§é㶠-ãã®ï¼ï¼ ï¼è»½éãã¤ãã¼Webãµã¼ãã¼ Cherokee Web Server ã§ãã㶠Part1:ã¹ã¯ã·ã§ç·¨
çããããã«ã¡ã¯ï¼
æè¿Apacheã§ãnginxã§ããªãCherokee Web Server ( http://www.cherokee-project.com )ã£ã¦ããWebãµã¼ãã¼ãããã£ã¦éãã§ãã¾ãã
æVPSé¯ãé ãã®ã§ä½ã軽ãWebé¯ãç¡ããæ¢ãã¦ãæã«çºè¦ãã
- Webã ãã§å種è¨å®ãå®çµã§ããæ©è½
- ã¯ã³ã¯ãªãã¯ã§CMSãã¤ã³ã¹ãã¼ã«åºæ¥ãç¬èªæ©è½
ãé¢ç½ãããªã®ã§è©¦ãã¦ã¿ã¾ãããå¤ånginxã¨ãã®å½±ã«é ãã¦éæåãã¦ããã ããã
ããããæ°åã«æ¸¡ã£ã¦Apacheã®ä»£æ¿ã¨ãã¦VPSé¯ã§ä½¿ãããªãããç 究ãã¦ããã¾ãã
I.ã¹ã¯ã·ã§ã¨ç´¹ä»åç»ãªã©ãªã©
ä»æ¥ã¯ã¹ã¯ã·ã§ã¨ã³ã¡ã³ãã®ã¿ãï¼LANç°å¢å ã®Ubuntu10.04LTS & Ubuntu11.10ã§åä½ç¢ºèªãã¾ãããï¼ã¾ãã¯å ¬å¼ã®åç»ããã
ï¼ï¼Webã ãã§å種è¨å®ãå®çµã§ããæ©è½ã¯ãããªæãããã¹ã³ãããã£ã©ãããããï¼
Cherokee Web Server - Introduction
ZendServer( http://www.zend.com/products/server/ )ãColdFusion( http://www.adobe.com/jp/products/coldfusion-family.html ) ã®ç®¡çç»é¢ï¼ http://d.hatena.ne.jp/dix3/20090222/1235252687 ï¼ã¿ããã«Webç管çç»é¢ã§å
¨é¨å®çµã§ãã¾ãã
ï¼ï¼ã¯ã³ã¯ãªãã¯ã§CMSãã¤ã³ã¹ãã¼ã«åºæ¥ãç¬èªæ©è½ã¯ãããªæãï¼
Cherokee Market: Installation and Drupal 7
å®éã«è©¦ãã¾ãããè²ããªCMSã2-3åã§ã¤ã³ã¹ãã¼ã«åºæ¥ã¾ãããªã©ããã¯ãªã ã
II.å®éã«ã¤ã³ã¹ã³ããã®ã§ã¹ã¯ã·ã§ãªã©ãªã©
é°å²æ°è¯ãããªæããç©äºå ¨ã¦è¡¨ã£é¢ããå ¥ãç§åãã§ããç§æçãªãã£ã©ãç¬ç¹ã®é°å²æ°ãé¸ãåºãã¦ã¾ãï¼
ããã³ãç»é¢ã¯ãããªæã(Cherokee Web Server 1.2.101ï¼ï¼
ãµã¼ãã¼ã®åæ¢ãCPU使ç¨çãã¡ã¢ãªã¼ä½¿ç¨éçã ã
ã¹ã¯ã·ã§ã¯è±èªã§æ®ã£ã¦ã¾ããããããªé¢¨ã«æ¥æ¬èªåãããã¦ããã¿ããã§ããé ãä¸ããã¾ããï¼
Cherokee Marketï¼
ãã®Webãµã¼ãã¼ãªããCMSãWebç»é¢ã ãã§ã¤ã³ã¹ãã¼ã«åºæ¥ããã ãâ¦ï½ºï¾ï½¸ï¾
CMSãWebã·ã¹ãã ãè²ã é¸æã§ãã¾ãï¼
ç¡æã®ãã®ã¨ãProçã®æ§ãªææã®ãã®ãããã¾ãã
http://cherokee-market.com ãè¦ãã¨
- Drupal 6 , Drupal 6 Pro , Drupal 6 Enterprise
- Drupal 7 , Drupal 7 Pro , Drupal 7 Enterprise
- Joomla
- Liferay CE
- Magnolia CMS , Magnolia CMS Pro
- MediaWiki
- MoinMoin
- Moodle
- Nagios
- Nuxeo DAM
- Nuxeo DM
- OpenX
- phpBB
- phpMyAdmin , phpMyAdmin Pro
- Redmine
- SilverStripe CMS
- StatusNet
- Sugar CRM
- Trac
- Vanilla
- WordPress , WordPress Pro , WordPress Enterprise
- WsgiDAV
- Zen Cart
ãä»ã®ã¨ããCherokee Marketã«å¯¾å¿ãã¦ããã¿ããã§ãã
ããã«CMSã®æ触ã試ããã®ã¯ã¢ãã°ãµãªç§ã«ã¯ããã¿ãªã§ãã
ãã¤ã¯ãã½ãã謹製WebMatrix ( http://www.microsoft.com/web/webmatrix/ )ã®*nixçã¿ãããªã
Concrete5ãå
¥ãã¦ãããªãããªâ¦
Drupal7ãã¤ã³ã¹ãã¼ã«ä¸ã®å³ï¼
ç»é¢ã®ã¦ã£ã¶ã¼ãã«æ²¿ã£ã¦å¿ è¦é ç®ãå ¥åãã¦ããã°ã¤ã³ã¹ã³å®äºã
ï¼ä¾åé¢ä¿ã§ã¨ã©ã¼ãæãæã«ã¯ãã®æ¨ã®æ示ãã§ã¦ä¸æãã¾ããç´°ããæåã®èª¬æã¯æ¬¡å以éã«ï¼
Drupal7ã¤ã³ã¹ãã¼ã«å®äºã®å³ï¼
ä»åã¯MySQLã§ã¯ãªãSQLite3ã§ã¤ã³ã¹ãã¼ã«ãã¦ã¿ã¾ãããDrupalããªããã¹ã²ã¼é²åãã¦ã¾ãããâ¦
ãã®ä»ç®¡çç»é¢çã ï¼VirtualHost管çç»é¢ï¼:
ãããªæãã§ãã¼ãã£ã«ãã¹ããONã»OFFã»åå¥ã®è¨å®ãªã©å ¨é¨ä¸å 管çã§ãã¾ããç´ æ´ãããã
ãã¼ãã£ã«ãã¹ãã®ãã©ãã£ãã¯ã¢ãã¿ã¼:
ãã¼ãã£ã«ãã¹ãæ¯ã®è©³ç´°è¨å®ç»é¢ï¼Behaviorã¿ãï¼:
ã¾ã 調æ»ä¸ã§ãããã£ãã·ã¥å¶å¾¡ã ã®ã¢ã¯ã»ã¹å¶å¾¡ã ã®è²ã Webç»é¢ã ãã§è¨å®åºæ¥ãã¿ããã§ãã
ãã®ä»ç®¡çç»é¢çã ï¼ä¸è¬è¨å®â¦ãããã¯ã¼ã¯è¨å®çï¼:
MIMEã¿ã¤ãè¨å®ï¼
III.第ä¸å°è±¡
ããã¸ãæ°ã«å ¥ãã¾ãããCherokeeããããããCMSãã¤ã³ãã©å ã§ç´ ã®ç¶æ ã§ä½¿ãç¨éçã§ã¯ãCherokee Marketçµç±ã§ãµã¯ã£ã¨ç°å¢ãä½ãä¸ããã®ãããããããï¼ãã¨CMSã®ç 究ç¨ã¨ãã«ã
次åã¯VPSãµã¼ãã¼ã¸ã®ã¤ã³ã¹ãã¼ã«ãæ³å®ãã¦ã
UbuntuServer10.04LTS (Lucid)32bitã¨ãUbuntuServer 11.10 (Oneiric Ocelot) 32bit
ã®ä»®æ³ãã·ã³ã«ææ°ã®Cherokee Web Server 1.2.101ã ã¤ã³ã¹ãã¼ã«ããæ¹æ³ãè¨è¿°ãã¾ãã
ã§ã¯ã§ã¯ï¼ ï¼å¤åã¤ã¥ããã°ããå¾ ã¡ãâ¦ï¼
åè
Cherokee Web Server ï¼
http://www.cherokee-project.comã¦ã£ãããã£ã¢ã®è§£èª¬ ï¼
http://en.wikipedia.org/wiki/Cherokee_%28web_server%29ããã¥ã¡ã³ã ï¼
http://www.cherokee-project.com/doc/Cherokee Market ï¼
http://cherokee-market.com/Hosting Websites with Cherokee – Linode Library ï¼
http://library.linode.com/web-servers/cherokee
FuelPHPåä½å®é¨ - oil console ï¼ PHP Interactiveæ¹ã使ã£ã¦ ããã¥ã¢ã«ã®ä¾æã³ããã§ããããªã¡ã½ããã試ãã¦ã¿ããâ彡
çæ§ããã«ã¡ã¯ï¼ FuelPHP Advent Calendar 2011 17æ¥ç®ã§ãã
æ¨æ¥ã¯ @madmamor ããã® ãFuelPHPã®coreã¯ã©ã¹ãæ¡å¼µãã¦ã¿ãã ãã§ããã
æ¬æ¥ã®ãé¡ã¯ãoil console ï¼ PHP Interactiveæ¹ã使ã£ã¦ ããã¥ã¢ã«ã®ä¾æã³ããã§ããããªã¡ã½ããã試ãã¦ã¿ããâ彡ãã§ãã
- oil console ã使ã£ã¦ã³ãã³ãã©ã¤ã³ã§FuelPHPã®ã¡ã½ãããå¼ã³åºãã¦ã¿ãã
- PHP InteractiveããFuelPHPã®ã¡ã½ãããå¼ã¹ãããã«å¾®æ¹é ãã¦ãããã¥ã¢ã«ä¾æã³ããã§è²ããªã¡ã½ããã試ãã¦ã¿ããã
ã®éçºã»å¦ç¿è£å©ãã¿2æ¬ç«ã¦ã§ããã¾ãã
ï¼ããã¥ã¡ã³ãã® http://docs.fuelphp.com/packages/oil/console.html ãä»å該å½ããç®æã«ãªãã¾ããï¼
1. oil consoleã£ã¦ä½ãï¼
FuelPHPã®ããã¥ã¢ã«ãèªãã§ããæä¸ãã¡ãã£ã¨ã ãã³ã¼ãã®æçã試ãããæãä½ãã®ã³ã³ããã¼ã©ã¼ãã¡ã¤ã«ã«<?php ï¼ä¸ç¥ï¼ public function action_hogehoge() { $arr1= Arr::average(array('1', 2, 4, '8')); var_dump($arr1); $arr2 = array('foo', 'bar', 'baz', 'yay'); Debug::dump(Arr::to_assoc($arr2)); } ï¼ä¸ç¥ï¼ ?>
ï¼Arr Class ã®ãåå¼·ä¸ - http://docs.fuelphp.com/classes/arr.html ï¼
ã¿ããã«ã½ã¼ã¹ãããããæ¸ãã¦ãã©ã¦ã¶ãIDEã§ç¢ºèªããã®ã£ã¦ã«ãã¿ã«ã¤ã§ãããã
FuelPHPã«ã¯ããããæç¨ã®consoleæ©è½ããããã³ãã³ãã©ã¤ã³ããè²ã
試ããã¨ãã§ãã¾ãã ( http://docs.fuelphp.com/packages/oil/console.html)
oilã³ãã³ãã¯ã½ã¼ã¹ã®README.md,fuel,docs,publicçã¨åãé層ã«ããã¾ããï¼
-
- -
phpæ¨æºã®date()ãdate_sunrise()ãªãã¦ã®ã試ãã¦ã¿ãããFuelPHPã®Arr::average()ãArr::to_assoc()ã試ãã¦ãå³ï¼
FuelPHPãæåããå¼ã³åºããphp -a ã¿ãããªãã®ã§ããã
ãã ã
<?php $arr1= Arr::average(array('1', 2, 4, '8')); var_dump($arr1); $arr2 = array('foo', 'bar', 'baz', 'yay'); Debug::dump(Arr::to_assoc($arr2)); ?>
ã®æ§ãªä¸è¡ã³ã¼ããããã¥ã¢ã«ããã³ãããã¦è©¦ãããæã«ã¯ã¾ãè¯ããã§ããã
<?php $people = array( array( "name" => "Jack", "age" => 21 ), array( "name" => "Jill", "age" => 23 ) ); print_r( Arr::assoc_to_keyval($people, 'name', 'age') ); ?>
ã¿ããã«ã³ã¼ãã«æ¹è¡ãå
¥ã£ã¦ãã¾ã£ãå ´åãã¾ãåãã¾ããorz
ãããããã½(`д)ï¾ï½·ï¾ï½²ï¾ï½° ï¼
2. PHP Interactiveã§FuelPHPã®ã¯ã©ã¹ãå¼ã³åºããããã«ããã°ããããããï¼
ã§ããã®oilã³ãã³ãã®consoleæ©è½ã§ããä¸è¡æ¯ã«readline()ã§å ¥åãèªã¿è¾¼ãã§evalãã¦ãããã§è¤æ°è¡å¯¾å¿ãããã®ã¯é¢åããã㧠PHP Interactive - http://www.hping.org/phpinteractive/ ã®åºçªã§ãã奥ããã
ããã¤ã¯æ¹è¡OKã§è¤æ°è¡å
¥åããªãããã³ã¼ãã®æçãã¤ã³ã¿ã©ã¯ãã£ãã«åããã¦è©¦ããã¹ã°ã¬ã¢ãã§ãã
ãããªæãï¼
ã§ãPHP Interactiveã«FuelPHPã®bootstrap.phpãèªã¿ãã¾ããã°ããããããï¼ã¨ããããã§å¾®æ¹é ãã¦ã¿ã¾ããã以ä¸ãã®è¨é²ã
注æï¼ããã¯ãã©ã¼ã å ã«æ¸ããã³ã¼ããevalãã¦å®è¡ãã¦ãwebããã°ã©ã ãªã®ã§æ±ºãã¦å ¬éãµã¼ãã¼ã«è¨ç½®ããªãã§ãã ãããï½½ï¾ï¾ï½°ï¾ï½¶ï½°ã®é¤é£ãé éãªã¢ã³ã³åã«ãªã£ã¦ãã¾ãã¾ããããã¾ã§ãåå¼·ç¨ã®å¤é¨ããç¹ãããªããã¼ã«ã«ç°å¢ã®ã¿ã§å®é¨ãã¦ãã ããã
PHP Interactiveæ¹ã§ãåå¼·ãã¦ãã¤ã¡ã¼ã¸å³ï¼
é«ç´ãªã³ã¼ãã¼ã§ã飲ã¿ãªãã軽ããã«ãµã³ãã«ã³ã¼ãããã©ãã°ã»ã¢ã³ãã»ãããããã¦updateãã¿ã³æ¼ãã¦åä½ç¢ºèªã
ãã§ãã³ã¼ããåããããã®å ´ã§å°ããã¤ãªãã·ã§ã³ãå¤ãå¤ãããããªããç解ãæ·±ãã¦ããã¾ãããªãã¦ã¤ã³ããªã¸ã§ã³ã¹ãªãã§ãããï¼â¦
ã§ã¯å¾®æ¹é ä½æ¥ã«çæãã¾ãããã
以ä¸ãã¼ã«ã«ç°å¢ãå¤ããã¤ãªããé¯ã«æ±ºãã¦è¨ç½®ããªããã¨ãï½½ï¾ï¾ï½°ï¾ï½¶ï½°ã®é¤é£ï¼ä»¥ä¸ç¥
ä½æ¥ï¼ï¼PHP Interactiveã®ã½ã¼ã¹ããã¦ã³ãã¼ãããã
ã½ã¼ã¹ã¯ ttp://www.hping.org/phpinteractive/phpinteractive-0.2.tar.gz ããåå¾ãã¦ãã ãããï¼ãµã¤ã㯠http://www.hping.org/phpinteractive/ ï¼
ä½æ¥ï¼ï¼ã½ã¼ã¹ã解åãã¦public/phpinteractive以ä¸ã«è¨ç½®ããã¼ããã·ã§ã³ãchown,chmodã§æ£ãããã¦ããã
(scripts以ä¸ã¯å±¥æ´ä¿åãã£ã¬ã¯ããªãªã®ã§apacheã¦ã¼ã¶ã¼ã®æ¸ãè¾¼ã¿æ¨©éå¿
é ï¼
ä½æ¥ï¼ï¼public/phpinteractive以ä¸ã«.htaccessãæ°è¨
ã½ã¼ã¹ï¼ public/phpinteractive/.htaccess
php_flag short_open_tag On
phpinteractiveã®ã½ã¼ã¹ãã·ã§ã¼ããªã¼ãã³ã¿ã°ã使ã£ã¦ããã®ã§ãã®ãã£ã¬ã¯ããªé
ä¸ã®ã¿short_open_tagãæå¹ã«ãã¦ãã¾ãã
ï¼ãµããã£ã¬ã¯ããªã§.htaccessã使ããªãå ´åã¯apacheã®confãã¡ã¤ã«ã§ AllowOverride Allã«ãªã£ã¦ãããè¦ç¢ºèªï¼
ä½æ¥ï¼ï¼public/phpinteractive/includefuel.phpãæ°è¨
ã½ã¼ã¹ï¼ public/phpinteractive/includefuel.php
<?php /** * Set error reporting and display errors settings. You will want to change these when in production. */ error_reporting(-1); ini_set('display_errors', 0); /** * Website document root */ define('DOCROOT', dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR); /** * Path to the application directory. */ define('APPPATH', realpath(dirname(dirname(__DIR__)).'/fuel/app/').DIRECTORY_SEPARATOR); /** * Path to the default packages directory. */ define('PKGPATH', realpath(dirname(dirname(__DIR__)).'/fuel/packages/').DIRECTORY_SEPARATOR); /** * The path to the framework core. */ define('COREPATH', realpath(dirname(dirname(__DIR__)).'/fuel/core/').DIRECTORY_SEPARATOR); // Get the start time and memory for use later defined('FUEL_START_TIME') or define('FUEL_START_TIME', microtime(true)); defined('FUEL_START_MEM') or define('FUEL_START_MEM', memory_get_usage()); // bypass shutdown_handler define('MY_INTERACTIVE_MODE', true); // Boot the app require APPPATH.'bootstrap.php'; //FUEL_ENV check if('development'!==\Fuel::$env) { echo 'development mode is required.'; exit; } ?>
oilãåèã«ä½ãã¾ãããMY_INTERACTIVE_MODE ã®å®æ°ã¯ç¬èªã«è¿½å ãã¦ãã¾ãã
ãã£ããæ¬çªã®å
¬éãµã¼ãã¼ã«ã¢ãããã¼ããã¦ãã¾ã£ãæã®æ°ä¼ãã®ãã\Fuel::$envãdevelopment以å¤ã®æã¯ä½ãããããã§çµäºããã¦ãã¾ãã(åè http://docs.fuelphp.com/installation/instructions.html#/setting_the_environment )
ä½æ¥ï¼ï¼public/phpinteractive/index.phpã微修æ£
ä¿®æ£ç®æã¯ä»¥ä¸ã®ã¨ããã§ãã
å¤æ´ç®æã®ç»é¢ã·ã§ããï¼
ãªãªã¸ãã«ã¨ã®å·®åï¼
root@star:/var/vhosts/contact.pizw.net/public/phpinteractive# diff index.php orig_index.php 2d1 < include('./includefuel.php'); 94c93 < if(!@unlink("scripts/$name")) --- > if(!@unlink("scripts/$name")) 135c134 < if (rename_script($current, $newname) == 0) --- > if (rename_script($current, $newname) == 0) 441,442c440 < //echo(htmlentities($code)) < echo(htmlentities($code, ENT_QUOTES, mb_internal_encoding())); --- > echo(htmlentities($code)) 498,499c496 < //$script_output = "<pre>".htmlentities($script_output)."</pre>"; < $script_output = "<pre>".htmlentities($script_output, ENT_QUOTES, mb_internal_encoding())."</pre>"; --- > $script_output = "<pre>".htmlentities($script_output)."</pre>";
2è¡ç®ã§ã¯æ°è¨ããincludefuel.phpãincludeãã¦ãã¾ããããã§FuelPHPã®ã¯ã©ã¹ãå¼ã¹ãããã«ãªãã¾ãã
441,498è¡ç®ä»è¿ã¯æ¥æ¬èªæååãåããªãããã«ããç®çã§ã®ä¿®æ£ã§ãã
ä½æ¥ï¼ï¼fuel/app/bootstrap.phpã§Error::ã®ã¯ã©ã¹ãæ¡å¼µåºæ¥ãããã«å¤æ´
ã½ã¼ã¹ï¼ fuel/app/bootstrap.php
<?php // Load in the Autoloader require COREPATH.'classes'.DIRECTORY_SEPARATOR.'autoloader.php'; class_alias('Fuel\\Core\\Autoloader', 'Autoloader'); // Bootstrap the framework DO NOT edit this require COREPATH.'bootstrap.php'; Autoloader::add_classes(array( // Add classes you want to override here // Example: 'View' => APPPATH.'classes/view.php', 'Error' => APPPATH.'classes/error.php',//âãã®è¡ã追å )); // Register the autoloader Autoloader::register(); /** * Your environment. Can be set to any of the following: * * Fuel::DEVELOPMENT * Fuel::TEST * Fuel::STAGE * Fuel::PRODUCTION */ Fuel::$env = (isset($_SERVER['FUEL_ENV']) ? $_SERVER['FUEL_ENV'] : Fuel::DEVELOPMENT); // Initialize the framework with the config file. Fuel::init('config.php'); ?>
PHP Interactiveã®å ¥åå´ã§PHPã®ææ³ã¨ã©ã¼ã«ãªããããªæ¸ãæ¹ãããå ´åãfuel/core/classes/error.phpã®shutdown_handler()ãåå¿ã(fuel/core/bootstrap.phpå ã®register_shutdown_functionãåç §)ããã§exitãã¦ãã¾ãã®ã§ãããããã§ã¯PHP Interactiveã®ç»é¢ã表示ãããªããªãã次ã®æä½ã«å°ããã\Error::shutdown_handler()ãå¾®å¤æ´ãã¾ãã
ãããªæãã§æ¢ã¾ãã¨ç¶è¡ã§ããªãã¦å°ãã§ãï¼
ä½æ¥ï¼ï¼fuel/app/classes/error.phpãæ°è¨
ã³ã¢ã®ã¯ã©ã¹ãã¡ã¤ã«ãæ¡å¼µãã¾ãã(ããã¥ã¡ã³ãï¼ http://docs.fuelphp.com/general/extending_core.html)
ã½ã¼ã¹ï¼ fuel/app/classes/error.php
<?php /** * Core Class Extends Example (error.php) * @package * @version 0.01 * @author mataga * @license MIT License * @copyright 2011 mataga * @link http://twitter.com/mataga */ class Error extends Fuel\Core\Error { /** * Native PHP shutdown handler * * @return string */ public static function shutdown_handler() { $last_error = error_get_last(); // Only show valid fatal errors if ($last_error AND in_array($last_error['type'], static::$fatal_levels)) { $severity = static::$levels[$last_error['type']]; logger(Fuel::L_ERROR, $severity.' - '.$last_error['message'].' in '.$last_error['file'].' on line '.$last_error['line']); $error = new \ErrorException($last_error['message'], $last_error['type'], 0, $last_error['file'], $last_error['line']); //MY_INTERACTIVDE_MODE -- ADD BEGIN if(defined('MY_INTERACTIVE_MODE') && MY_INTERACTIVE_MODE) { echo \Debug::dump($last_error); return; } //MY_INTERACTIVDE_MODE -- ADD END if (\Fuel::$env != Fuel::PRODUCTION) { static::show_php_error($error); } else { static::show_production_error($error); } exit(1); } } }//endofclass /* End of file error.php */ ?>
//MY_INTERACTIVDE_MODE ã§æãã 5è¡ã追å ããã ãã§ããããã§ãã¼ã¹ã¨ã©ã¼ãèµ·ãã¦ã PHP Interactiveæ¹çµç±ã®å ´åã«ã¯ãããªæãã§å¦çã¯ç¶è¡ããã¾ãã
ä½æ¥ï¼ï¼åä½ç¢ºèª
ttp://example.com/phpinteractive/ ãéãã¦ãããã試ãã¹ã
ãããªæãã§Arr::ã®å®é¨ããã¦ã¿ããããã¨ãããã«ç´°ããåãã確èªã§ãã¦ä½¿ãæ¹ãè¦ããããã§ãã
ã¨ããããã§FuelPHPã®ããã¥ã¡ã³ããµã¤ã( http://docs.fuelphp.com/ )ã®ãµã³ãã«ã³ã¼ããåããã¾ãã£ã¦ãããªããçæãã¤ã¹ã¿ã¼ã«ãªãã¾ãããï¼
触ãã°è§¦ãã»ã©æ·±ãå³ããããã¾ããã
ææ¥ã¯ @NEKOGET ããã®ãFuelPHPã®å¼ã³æ¹ããã«ãªãã¾ãã
ã§ã¯ã§ã¯ï¼
FuelPHPåä½å®é¨ - å®é¨ããã½ã¼ã¹ãModulesã«éãè¾¼ãã¦ã¢ã¸ã¥ã¼ã«åå²ãã¦ã¿ãã
çæ§ããã«ã¡ã¯ï¼ FuelPHP Advent Calendar 2011 12æ¥ç® ã¯ç§ @mataga ãæ
å½ãã¾ãã
æ¨æ¥ã¯ @madmamor ããã® ãFuelPHPã§Facebookã¢ããªãä½ã£ã¦ã¿ãããå®è£
ç·¨ã ãã§ããã
æ¬æ¥ã®ãé¡ã¯ãå®é¨ããã½ã¼ã¹ãModulesã«éãè¾¼ãã¢ã¸ã¥ã¼ã«åå²ãã¦ã¿ãããã§ãã
- FuelPHPã®ã½ã¼ã¹ç¾¤ãæ©è½æ¯ã«ã¢ã¸ã¥ã¼ã«åå²ãã¦ã¿ãã
- HMVCæ©æ§ã®ãããã§ã³ã³ããã¼ã©Aã®ä¸ã§å¥ã¢ã¸ã¥ã¼ã«ã®ã³ã³ããã¼ã©ã¼Bãå¼ã³åºããã§ãã
ã®ãµã¯ãªé¨åãä½é¨ããã¦ããã¾ãã
ï¼ããã¥ã¡ã³ãã® http://docs.fuelphp.com/general/modules.html , http://docs.fuelphp.com/general/hmvc.html ãä»å該å½ããç®æã«ãªãã¾ããï¼
â»1 ã¢ã¸ã¥ã¼ã«åå²ï¼Moduleså ã¸ã®ãã£ã¬ã¯ããªæ§æ移åï¼ãè¡ããªãã¦ãã
http://docs.fuelphp.com/general/controllers/base.html#/controller_in_subdir ã§è§£èª¬ããã¦ããæ§ã«
- app/classes/controller/ã³ã³ããã¼ã©ã¼ã½ã¼ã¹A.php
- app/classes/controller/ãµããã£ã¬ã¯ããª1/ã³ã³ããã¼ã©ã¼ã½ã¼ã¹B.php
- app/classes/controller/ãµããã£ã¬ã¯ããª1/ãµããã£ã¬ã¯ããª2/ã³ã³ããã¼ã©ã¼ã½ã¼ã¹C.php
- app/classes/controller/ãµããã£ã¬ã¯ããª1/ãµããã£ã¬ã¯ããª2/ãµããã£ã¬ã¯ããª3/ã³ã³ããã¼ã©ã¼ã½ã¼ã¹D.php
ãªãã¦è¸å½ã¯åºæ¥ãããã§ãããç§ã¯Modulesãæãã¦ãã¾ãªãã®ã§ããã§ã¯ç¡è¦ãã¾ãã
I:HMVCã£ã¦ãªãããï¼ååã ãç¡é§ã«ã«ãã³ã¤ã¤ãã ãã©ï¼
ã®è©³ç´°ã¤ãã¦ã¯HMVCâ¦"Hierarchical Model View Controller"ã§ã°ã°ã£ã¦ãããã¨ãã¦ï¼ä¸¸æãï¼ãæ¥æ¬èªè¶
俺訳ã§è¨ãã¨ãæ©è½æ¯ã«MVCæ§é ãã¢ã¸ã¥ã¼ã«åå²ï¼ãã©ã«ãåãï¼ãã¦é層åãã¦ãã¾ãï¼ã¢ã¸ã¥ã¼ã«ã®ä¸ããå¥ã®ã¢ã¸ã¥ã¼ã«ãå¼ã¹ãããã«ãã¦ãã¾ãããã£ã¦äºã§ãã
ã½ã¼ã¹ã®ã¢ã¸ã¥ã¼ã«åãæ¨ãé²ãããã¨ã§HMVCâ¦"Hierarchical Model View Controller"ãã¬ã·ã¬ã·ä½¿ãã¾ã£ãã
â»1ã®éãã³ã³ããã¼ã©ã¼ã®é層åãªãå¥ã«ã¢ã¸ã¥ã¼ã«åå²ããªãã¦ãå®ç¾åºæ¥ãã¨ããã°åºæ¥ãã®ã§ãããæè§ãªãç¾ããã¢ã¸ã¥ã¼ã«åå²ããããããªã¤ã«ãããã®æã¯æ·±ãçªã£è¾¼ã¾ãªãã§ãã ãããï¼ä¸é¨ã®æ©è½ã ãã¢ã¸ã¥ã¼ã«åãããã¨ãå¯è½ã§ããï¼
"ãããã¯æ§é ã§æ©è½ãåãåºãããã®ãã®å¯è½ãªCMSãä½ãå ´åã«ãã©ã®ãããªãã£ã¬ã¯ããªæ§æã«ããã°ãã¾ãåæ¥ãã¦ä½æ¥ãé²ãããããï¼"â¦ãæ³åãã¦ã¿ãã¨ããããããããªã¨æãã¾ãã建ç¯ã§è¨ãã¨2x4å·¥æ³ã¿ãããªãæºã®ä¸ããã·ã£ã¨éä»åããããæ§æ ¼ãªç§ã«ããã¿ãªï¼
ã¢ã¸ã¥ã¼ã«åããæã®ãã£ã¬ã¯ããªæ§é ã®éã:
èããããæããã£ã¦äºã§treeã³ãã³ãã®ç»é¢ãã³ãããã¿ãªãï¼ä»¥å¾ç»åAï¼å·¦å´ã¯ãµã³ãã«ã½ã¼ã¹stationwagon ( https://github.com/abdelm/stationwagon ãé¢é£è¨äº http://d.hatena.ne.jp/dix3/20111119/1321684421)ã®ã½ã¼ã¹ç¾¤ã«ãFuelPHPæ¨æºæ·»ä»ã®app/classes/controller/welcome.php(ã³ã³ããã¼ã©ï¼ã¨app/views/welcome/index.phpï¼ãã¥ã¼ï¼ãæ··ãããã®ã
å³å´ã¯stationwagonç±æ¥ã®ã½ã¼ã¹ã app/modulesï¼ã¢ã¸ã¥ã¼ã«ç¨ãã£ã¬ã¯ããªï¼/sbox(ã¢ã¸ã¥ã¼ã«åï¼ä¿ºå½åï¼/以ä¸ã«ç§»åããFuelPHPæ¨æºæ·»ä»ã®welcomeç»é¢ãapp/modules/welcome/以ä¸ã«ç§»åãããã®ã
åå²ã®ä»æ¹ãããã§ã¯ç°¡åã®ããã½ã¼ã¹ã®ç±æ¥ã§åãã¦ãã¾ãããå®æ¦ã§ã¯æ©è½ã§åããäºã(adminã¢ã¸ã¥ã¼ã«ã»cartã¢ã¸ã¥ã¼ã«ã»blogã¢ã¸ã¥ã¼ã«ã»usersã¢ã¸ã¥ã¼ã«ã»widgetã¢ã¸ã¥ã¼ã«etcï¼ãæ³åããã°ã¢ã¸ã¥ã¼ã«åããã¡ãªãããæ³åãããããã¨ãï¼cart,widgetã¢ã¸ã¥ã¼ã«ã¯blogã¢ã¸ã¥ã¼ã«å ããå¼ã³åºããè¤éãªæ§æï¼
II:Modulesåä½æ¥æé
ã¨ããããã§ã¢ã¸ã¥ã¼ã«åã®ç´ æ´ãããããç解é ããã¨ãã¦ãä½æ¥æé ãæ¸ãã¦ããã¾ããä¸åº¦å®é¨åç°å¢ stationwagonã®ã½ã¼ã¹ãå ¬éããã¦ããã®ã§( https://github.com/abdelm/stationwagon )ãããã®ã½ã¼ã¹ãModulesã«ç§»åããæé ã追ããªããåä½å®é¨ãã¦ã¿ã¾ãããã
åºæ¥ãã°ãã¡ãã®è¨äº çãåèã«äºãstationwagonç°å¢ãæºåãã¦ã²ã¨éã触ãããå¾ã®æ¹ãåããè¯ãã¨æãã¾ãã
æé ï¼ï¼app/modules/sbox(ä»»æã®ã¢ã¸ã¥ã¼ã«å)/以ä¸ã«è²ããªãã£ã¬ã¯ããªãä½æãã
ãã¤ã³ãï¼
root@star:/var/vhosts/sandbox/stationwagon/fuel/app/modules# tree -d
.
âââ sboxï¼ã¢ã¸ã¥ã¼ã«åï¼å¥½ãã«å½åï¼
âââ classesï¼å¿ é ï¼
â âââ controllerï¼ã³ã³ããã¼ã©ã¼ç¨ãã£ã¬ã¯ããªï¼
â âââ modelï¼ã¢ãã«ç¨ãã£ã¬ã¯ããªï¼
â âââ viewï¼optional:ViewModelsç¨ãã£ã¬ã¯ããªï¼ä»å使ããªãã®ã§ç¡ãã¦ãè¯ããã¨ãããã¢ã¸ã¥ã¼ã«åãããã¨ViewModelsã¯ä½¿ããªãã£ããè¦ç¶ç¶èª¿æ»ï¼
âââ configï¼è¨å®ãã¡ã¤ã«ç¨ãã£ã¬ã¯ããªï¼
âââ langï¼è¨èªãã¡ã¤ã«ç¨ãã£ã¬ã¯ããªï¼
âââ viewsï¼ãã¥ã¼ãã¡ã¤ã«ç¨ãã£ã¬ã¯ããªï¼
âââ articles (viewså ã®ãµããã£ã¬ã¯ããªãä»å使ç¨ã½ã¼ã¹stationwagonã®viewsæ§æã§ãããªã£ã¦ããçº)
âââ categories(ä¸ã«åã)
âââ users(ä¸ã«åã)
- ä¸è¨ã®ç·ã»èµ¤ã»éã®å¤ªåè²ã¤ãé¨åã®ãã£ã¬ã¯ããªæ§æãåºæ¬ã§ããã使ç¨ããªããã£ã¬ã¯ããªã¯ä¸è¦ã§ããï¼langãconfigãã£ã¬ã¯ããªçï¼
- ã¢ã¸ã¥ã¼ã«åã¯è§£èª¬ç¨ã«sboxã¨ååãã¤ãã¾ããããã®å ´åURLã¯ttp://example.com/sbox/hogehogeï¼ã¢ã¯ã·ã§ã³åï¼ ã®æ§ã«ãªãã¾ããï¼Routingãå¼ã£ãå ´åã¯ãã®éãã§ã¯ãªããï¼
ã¾ããä»åã¯stationwagonã®ã½ã¼ã¹ç¾¤ãsboxã¨ãã1ã¢ã¸ã¥ã¼ã«ã«ã¾ããã¨éãè¾¼ãã¾ãããã
- articlesæ©è½ãmodules/mod1(ä»»æã®ååï¼ã¢ã¸ã¥ã¼ã«é ä¸ã«
- usersæ©è½ãmodules/mod2ã¢ã¸ã¥ã¼ã«é ä¸ã«
- categoriesãmodules/mod3ã¢ã¸ã¥ã¼ã«é ä¸ã«
ã¨ããåå²ã®ä»æ¹ã大ãã«ã¢ãªã§ãï¼ã¨ããããã£ã¡ãæ¬æµãï¼ã
æé 2ï¼app/modules/sbox(ä»»æã®ã¢ã¸ã¥ã¼ã«å)/以ä¸ã«ã½ã¼ã¹ãã¡ã¤ã«ã移åãã
移ååã»ç§»åå¾ã®ãã¡ã¤ã«æ§æã®å·®ç°ã¯ä¸è¿°ããç»åAã®éãã§ãã移åå¾ã¯ãããªæãç¹ã«ã³ã¡ã³ãç¡ããã¨ãããã移åããã°ã¨ãã·ã
root@star:/var/vhosts/sandbox/stationwagon/fuel/app/modules/sbox# tree
.
âââ classes
â âââ controller
â â âââ articles.php
â â âââ categories.php
â â âââ common.php
â â âââ users.php
â âââ model
â â âââ article.php
â â âââ category.php
â âââ view
âââ config
âââ lang
âââ views
âââ 404.php
âââ articles
â âââ add.php
â âââ edit.php
â âââ index.php
âââ categories
â âââ add.php
â âââ edit.php
â âââ index.php
âââ template.php
âââ users
âââ index.php
âââ login.php
âââ signup.php
ï¼404ãã¼ã¸ã®ãã¥ã¼ãããã¥ã¼ã®å¤æ ãã³ãã¬ã¼ãã®template.phpã¯ã¢ã¸ã¥ã¼ã«å¤ãããã¨ããã®ã¸ãã®ç´°ãã調æ´ã¯ãã¨ã§èãã¾ããããï¼
æé 3ï¼app/config/config.php å ã§module_pathsã®æå®ããããªã
'module_paths' => array( APPPATH.'modules'.DS ),
160-180è¡ç®ä»è¿ã«ããã¾ããã³ã¡ã³ãã¢ã¦ã//ãå¤ãã¦æå¹ã«ãããã¨ã§app/modules/ ãã¢ã¸ã¥ã¼ã«ç¨ãã£ã¬ã¯ããªã¨ãã¦ä½¿ããããã«ãªãã¾ãã
ï¼ããã¥ã¡ã³ãã® http://docs.fuelphp.com/general/modules.html#/module_config ï¼
æé 4ï¼ç§»åããã½ã¼ã¹å ã®ã½ã¼ã¹ãä¸é¨æ¸ãæãã¦ããã
ãã£ã¬ã¯ããªã移åãããã¨ã§åã½ã¼ã¹å ã®ä¸é¨æ¸ãæããå¿ è¦ã¨ãªãã¾ããï¼ããã¥ã¡ã³ãã® http://docs.fuelphp.com/general/modules.html#/module_namespace , http://docs.fuelphp.com/general/modules.html#/static_calls ï¼
åºæ¬åç´ä½æ¥ã§ããnamespaceã®çµ¡ã¿ããããCodeIgniterã®ã¢ã¸ã¥ã¼ã«åï¼ãã¡ã¤ã«ã移åããã°ã»ã¼ä½æ¥å®äºï¼ã®æã¨å¤å°åæãéãã®ã§ã¡ããã¨æ©ã¿ã¾ãããå ¬å¼ã®ããã¥ã¡ã³ãæ´åãé²ãã§ããã¨ãããããã¥ã¼ããªã¢ã«ãè¼ã£ã¦ããã¨æãã¾ãã
ï¼å®éã«æ¸ãæããç®æã¯stationwagonã®ã½ã¼ã¹ã®æ°ãè¦ã¦ããããã°ãããéãçµæ§å¤ãã®ã§ãå¤æ´ãã¿ã¼ã³ã®ã¿ä»¥ä¸ã«è¨è¿°ãã¾ããï¼
ã½ã¼ã¹æ¸ãæãã®ä¾
å®ä¾A ã¨ããã³ã³ããã¼ã©ã¼
root@star:/var/vhosts/sandbox/stationwagon# hg diff -r 61a788776698 fuel/app/modules/sbox/classes/controller/common.php diff -r 61a788776698 fuel/app/modules/sbox/classes/controller/common.php --- a/fuel/app/modules/sbox/classes/controller/common.php Tue Dec 06 18:30:18 2011 +0900 +++ b/fuel/app/modules/sbox/classes/controller/common.php Sat Dec 10 19:26:44 2011 +0900 @@ -1,6 +1,6 @@ <?php - -class Controller_Common extends Controller_Template { +namespace Sbox; +class Controller_Common extends \Controller_Template { public function before() { @@ -16,20 +16,21 @@ } // Check user access - $access = Auth::has_access(array( + $access = \Auth::has_access(array( $this->request->controller, $this->request->action )); if ($access != true) { - Response::redirect('users/login'); + + \Response::redirect('sbox/users/login'); } else { - if (Auth::check()) + if (\Auth::check()) { - $this->user_id = Auth::instance()->get_user_id(); + $this->user_id = \Auth::instance()->get_user_id(); $this->user_id = $this->user_id[1]; } } @@ -42,7 +43,7 @@ // Set a HTTP 404 output header $this->response->status = 404; - $this->template->content = View::factory('404', $data); + $this->template->content = \View::factory('404', $data); } }
å®ä¾B ã¨ããã¢ãã«
root@star:/var/vhosts/sandbox/stationwagon# hg diff -r 61a788776698 fuel/app/modules/sbox/classes/model/article.php diff -r 61a788776698 fuel/app/modules/sbox/classes/model/article.php --- a/fuel/app/modules/sbox/classes/model/article.php Tue Dec 06 18:30:18 2011 +0900 +++ b/fuel/app/modules/sbox/classes/model/article.php Sat Dec 10 20:58:44 2011 +0900 @@ -1,16 +1,16 @@ <?php +namespace Sbox; +class Model_Article extends \Orm\Model { -class Model_Article extends Orm\Model { (ä¸ç¥ï¼ @@ -22,7 +22,7 @@ public static function validate($factory) { - $val = Validation::factory($factory); + $val = \Validation::factory($factory); $val->add('category_id', 'Category');
ï¼ä¸è¨å®ä¾Aã®stationwagonã®å
±éã³ã³ããã¼ã©ã¼common.phpã¯ãfuel/core/classes/controllerã®Controller_Templateãç¶æ¿ããController_Commonã¨ããã¯ã©ã¹ã§ããã®ã¢ã¸ã¥ã¼ã«å
ã®ä»ã®ã³ã³ããã¼ã©ã¼ï¼articles.phpãªã©ï¼ã¯common.phpãç¶æ¿ãã¦ãã¾ããcommon.phpã«ã¯ã³ã³ããã¼ã©ã¼å
åã¢ã¯ã·ã§ã³ï¼URLï¼å¼ã³åºãç´åã®å
±éå¦çbefore()ãã404ãã¼ã¸è¡¨ç¤ºç¨ã¢ã¯ã·ã§ã³ action_404() ãæ¸ããã¦ãã¾ãã æ§ï¼factory()âæ°ï¼forge()ã¸ã®ã¡ã½ããåæ¸ãæãã¯ä»åã®ä¸»é¡ããã¯å¤ããã®ã§ãã®ã¾ã¾ï¼
ããã§ã¯è¦å¤æ´ç®æãæ¸ãã¦ããã¾ãã
å¤æ´1 : ã³ã³ããã¼ã©ã»ã¢ãã«ãã¡ã¤ã«ã§ã¯ã¢ã¸ã¥ã¼ã«å(ï¼ãã£ã¬ã¯ããªåï¼ã® namespace ãå¿ ã追å ãã
ã³ã³ããã¼ã©ã§ã¯ã namespace ã¢ã¸ã¥ã¼ã«å;ã®å®£è¨ãè¡ããï¼ããã§ã¯ namespace Sbox;ï¼åèï¼ http://docs.fuelphp.com/general/modules.html#/module_namespace
åå空éã®å½åè¦å PSR-0äºã
ã«ã¤ãã¦ã®æ·±ãè°è«ã«ã¤ãã¦ã¯ http://fuelphp.com/blog/2011/04/classnames-autoloading-namespaces çããåèãã ãããä»åã¯FuelPHPã®ä»æ§ã«ã¤ãã¦ã®ç§è¦ã¯çç¥ãã¾ããcore/classes/autoloader.php ã®alias_to_namespace($class, $namespace = '')ã®class_alias($class, $root_class);å¨ãã§global namespaceã«ã¨ã¤ãªã¢ã¹ãä½ã£ã¦ãããããªã®ã§ï½½ï½°ï¾ï¾ï½°ï¾ï½¶ï½°ã¯ãã®è¾ºã§ãå¼ã£ã¦ä¸æããã¨ããã°ã好ã¿ã®å½¢ã«ãªããã¨ã
å¤æ´2 : ã³ã³ããã¼ã©ã»ã¢ãã«å ã§ã³ã¢ã®ã¯ã©ã¹ãå¼ã³åºãã¦ããã¨ããã®é ã«\ã追å
ãã¤ã³ãï¼ã§ã¢ã¸ã¥ã¼ã«ã®namespaceã宣è¨ãããã¨ã«ä¼´ããã½ã¼ã¹å 微調æ´ãè¡ãªãã¾ããä¸è¨å®ä¾Aã¨å®ä¾Bã§ã¯
ã¨è³ãæã§\ãä»ãããã¦ããäºã解ããã¨æãã¾ãã
class Controller_Common extends Controller_Template {
â
class Controller_Common extends \Controller_Template {
class Model_Article extends Orm\Model {
â
class Model_Article extends \Orm\Model {
Auth::
â
\Auth::
Response::
â
\Response::
View::
â
\View::
Validation::
â
\Validation::
ã¾ããä»åã®ãµã³ãã«ã½ã¼ã¹ã§ã¯è©²å½ç®æã¯åå¨ãã¾ãããoilã³ãã³ãçã§èªåçæãããã¢ãã«ã®å ´åã¢ãã«ã®ã¯ã©ã¹åã
namespace Model; class Contact extends Orm\Model {
ã®æ§ã«namespace Model; ãclass ã¢ãã«å extendsâ¦ã¨ãªã£ã¦ããå ´åãããã¾ããï¼ã¨ããããã£ã¡ã®ã»ããããã¥ã¢ã«ã«è¼ã£ã¦ããæ¸ãæ¹ï¼ï¼åè http://docs.fuelphp.com/general/models.htmlï¼
ãã®å ´åã«ã¯
ã¨ããæ§ãªæ¸ãæããå¿ è¦ã¨ãªãã¾ãã
//namespace Model;
â
namespace Sbox\Model;
//class Contact extends Orm\Model {
â
class Contact extends \Orm\Model {
å°ãã³ã³ããã¼ã©ããå¼ã³åºãããå
ã®Viewãã¡ã¤ã«å
(ä¾ï¼sbox/views/articles/edit.php)ãªã©ã§
<?php echo Form::input( ã¨è¨è¿°ããã¦ããç®æã\Form::input(ã«ããå¿
è¦ã¯ããã¾ããã
å¤æ´3 : ã³ã³ããã¼ã©å ããã¥ã¼ãã¡ã¤ã«å ã§\Response::redirect('é£ã³å ');ããHtml::anchor('é£ã³å ');ã¨ãã¦ããç®æã®é£ã³å ãã¢ã¸ã¥ã¼ã«åä»ãã®é©åãªãã¹ã«æ¸ãæãã
ä¾ãã° categories/add ã ã£ããªã³ã¯å ã¯ä»åã¢ã¸ã¥ã¼ã«sboxã®ä¸ã«ã½ã¼ã¹ç¾¤ãè¨ç½®ããã®ã§ãsbox/categories/add ã¸ã®æ¸ãæããå¿ è¦ã¨ãªãã¾ããå¤æ´4 : ã¦ã¼ã¶ã¼èªè¨¼ã«SimpleAuthã使ã£ã¦ããå ´åã®config/simpleauth.phpã®rolesè¨å®æ¸ãæã
stationwagonã®ã½ã¼ã¹ã§ã¯ã¦ã¼ã¶ã¼èªè¨¼ã«FuelPHPæ¨æºæè¼ã® SimpleAuth - http://docs.fuelphp.com/packages/auth/simpleauth/intro.html ï¼2011/12/10ç¾å¨å ¬å¼ã®ããã¥ã¢ã«ãã¾ã ã¤ã³ããé¨åããç¡ãï¼ã使ã£ã¦ããã®ã§ããããã®rolesè¨å®ã«ãã¢ã¸ã¥ã¼ã«åã®è¿½å ãå¿ è¦ã¨ãªãã¾ããroot@star:/var/vhosts/sandbox/stationwagon# diff ../stationwagon-nohmvc/fuel/app/config/simpleauth.php fuel/app/config/simpleauth.php 59c59 < '\Controller_Users' => array( --- > '\Sbox\Controller_Users' => array( 64c64 < '\Controller_Users' => array( --- > '\Sbox\Controller_Users' => array( 71c71 < '\Controller_Users' => array( --- > '\Sbox\Controller_Users' => array( 74c74 < '\Controller_Articles' => array( --- > '\Sbox\Controller_Articles' => array( 80c80 < '\Controller_Categories' => array( --- > '\Sbox\Controller_Categories' => array(
â¦ã¨ããããã§èª¬æãæ¸ãã¨ã¿ã¤ãã³ãªãã§ãããããã¨ã¯åç´ã§ã
- ã³ã³ããã¼ã©ã¼ãã¢ãã«ã«ã¯ namespace ã¢ã¸ã¥ã¼ã«åãã¤ããã
- ã³ã³ããã¼ã©ã¼ãã¢ãã«ã®ãã¡ã¤ã«å ã§fuel/core/classes/é ä¸ã®ã³ã¢ã®ã¯ã©ã¹ãå¼ã³åºãã¦ããé¨åã«ã¯\ãé ã«ã¤ãã
- ãªãã¤ã¬ã¯ãå ããªã³ã¯å ãå¤ããã®ã§ã¢ã¸ã¥ã¼ã«å追è¨åãèæ ®ãã¦è¨æ£ããã
- èªè¨¼å¨ããªã©ã®è¨å®ãã¢ã¸ã¥ã¼ã«å追è¨åãèæ ®ãã¦æ¸ãæãã
ã¨ããæããã®ä½æ¥ã®ãã¢ã¨ãªãã¾ãã
III:ã¢ã¸ã¥ã¼ã«åæ¸ã®welcomeã¢ã¸ã¥ã¼ã«ããsboxã¢ã¸ã¥ã¼ã«ã®loginç»é¢ãå¼ã³åºãã¦ã¿ã
ããã¾ã§æ¸ãã®ã«ç²ããã®ã§å¾ã¯æ°æ§ã®ç»é¢ã·ã§ããã§â¦welcome/action_index (ttp://example.com/welcome/) ã«ã¦sbox/users/login/ ãã¢ã¸ã¥ã¼ã«ã¨ãã¦å¼ã³åºãã¦ããã½ã¼ã¹é¨å
welcomeç»é¢ã«Tumblrã£ã½ããã°ã¤ã³ãããã¯(sbox/users/login/)ãåºç¾ï¼
ï¼æ®éã®welcomeç»é¢ã¯ãããªæãï¼
ï¼sboxã¢ã¸ã¥ã¼ã«åä½ã§ã®sbox/users/login/ç»é¢ã¯ãããªæãï¼
HMVCã使ãé°å²æ°ããªãã¨ãªããããã¾ãã§ããããï¼
ï¼â»ãã®ã½ã¼ã¹ã§å®é¨ç¨ã«ã¯ãªãã¨ãªãå¥ã¢ã¸ã¥ã¼ã«ã®ã³ã³ããã¼ã©ã¼ãå¼ã³åºãã¦åããã¦ããã ãã§ãã®ã§ãå®éã®å®è£
ã§ã¯å¼ã³åºãããå´ã»å¼ã³åºãå´ã«ã¡ããã¨ããã¨ã©ã¼å¦çï¼ã¢ã¸ã¥ã¼ã«åä½ã§å¼ã°ããªãããã«ããæ¹ççãå¿
è¦ã«ãªããã¨æãã¾ããï¼
ï¼stationwagonã¯FuelPHPã®é°å²æ°ãå³ããç·´ç¿ã½ã¼ã¹ã¬ãã«ãªã®ã§å®åã¬ãã«ã«æã£ã¦è¡ãã«ã¯ãã£ã¨ä¾å¤å¦çã»å®è£
ã®è¿½å ãå¿
è¦ã§ããããã¾ã§ãå®å
¨ãªãã¼ã«ã«ç°å¢ã®ä¸ã§è©¦ãç å ´ã»å®é¨å ´ã¨ãã¦èãã¦ãã ããããã©ãã§ããå度æ¸ãã¦ããã¾ããï¼
IV:ä»åã¢ã¸ã¥ã¼ã«åå®é¨ã§ä½¿ã£ãã½ã¼ã¹
æ¹é åã½ã¼ã¹ã«ã¤ãã¦ã¯ãhttps://github.com/abdelm/stationwagon ããåå¾ãã¦ãã ãããæ¹é å¾ã½ã¼ã¹ã«ã¤ãã¦ã¯stationwagonã®ã©ã¤ã»ã³ã¹ãæè¨ããã¦ããªãçºå ¬éã¯æ§ãã¦ããã¾ãã
ä¸åº¦æ¸ãæãã¦åãããã«ãªãã¾ã§è©¦ãã¦ã¿ãã°FuelPHPã®æ§é ãè²ã
è¦ãã¦ããã®ã§ããã®æ©ä¼ã«ã¢ã¸ã¥ã¼ã«åã«ãã£ã¬ã³ã¸ãã¦ã¿ã¦ã¯å¦ä½ã§ãããï¼
ï¼ã¢ã¸ã¥ã¼ã«åä½æ¥éä¸ã§Auth::ã\Auth::ã«ãå¿ãã¦ããããªã¯ã©ã¹è¦ã¤ãããã¼ã¨ã¨æããã¦ãå³ï¼
ã¢ã¸ã¥ã¼ã«åæ¸ã¿ã§ã®ã½ã¼ã¹å®è£ ã¯ç¾å¨ http://contact.pizw.net/ ãhttp://contact.pizw.net/contact/srcwindow ã«ã¦ãåãåãããã©ã¼ã ä½æå®é¨ãã¼ãããã¸ã¯ã¸ã¯é²è¡ä¸ã§ãããã¡ãã®æ¹ã§ãªãã¨ãªãé°å²æ°ãæ´ãã§ããããã°ã¨æãã¾ãã
ææ¥ã¯ @ounziw ããã®ãPHP5.3 ã®åå空éå ¥éãã«ãªãã¾ãããããããããããã¾ãã
ã§ã¯ã§ã¯ï¼
éã¯ç±ããã¡ã«æã¦-ã³ããããã°ã©ãã¼ã®ããã®FuelPHPåä½å®é¨ãã®ï¼(ãµã³ãã«ã½ã¼ã¹ï¼ãç å ´ã¥ãã)
ã¿ãªãã¾ãä¹
ãã¶ãã§ããä»æ¥ã¯ä»æµè¡ãï¼ã®FuelPHP( http://fuelphp.com/ )ã«ã¤ãã¦èª¿ã¹ã¦ã¿ã¾ãã
ãã¼ãã³ã°ã«ã¹ãã¤ã¹ã®ããããã¬ã¼ã ã¯ã¼ã¯ã§ãã
çºç«è£
ç½®ãããçæã¨ããªãã®ã¸ã§ã¼ã¯ããã¸ã§ãã¼ï¼HAHAHAã
ï¼ä»æµè¡ãã¨ãããå¾2å¹´ä½ãããæµè¡ãããããªããããï¼
CodeIgniterã ã¨ã©ã¤ã»ã³ã¹äºã ãä»äºä¸ãã³ãã¯ã»ã¨ããã¢ãã³ãã¬ã¼ã ã¯ã¼ã¯å¼ããããã©ä»æ´ããã¼ãªFWãåå¼·ããã®ã¯å«ã ã¨ãããããæ°åãªæã®æä¸ä¸»åè£ã¨ãã¦ã©ããï¼
ï¼OSLã§ãå®è³ªå½±é¿ãªãã¨æãã®ã§ç§ã¯ããã»ã©æ°ã«ãã¦ã¯ããªãã§ããå人çã«ã¯ãã«ãªã¼ãã³ããOKããã å³å¯ã«èª¿æ»ããªãã¨ãããªãäºæ ã®ããæ¹ã«ã¯æ°ã®ãããè¦ç´ ã§ã¯ããã¾ããï¼
1.åå¼·ããã£ã¦ãã£ã¦ããµã³ãã«ç¡ãã¨ã¡ã³ãã¯ã»(;´Ð`)yââ
æ給250ä¸ã®ä¿ºçã«ã³ãã³ãã©ã¤ã³ããã½ã¼ã¹ããã¦ã³ãã¼ããã¦âãã¼ãã«ãã³ãã³ãã©ã¤ã³ã§ã¤ãã£ã¦âã½ã¼ã¹ãæ¸ãã¦âãã®éã«è²ã è¦ãã¦âã½ã¼ã¹è§£æã£ã¦åå¼ããé¢åããããï¼èª°ãéã£ã¦ãã ããï¼ã¨ããããã§ãæã£åãæ©ãDBé£æºããªããåããµã³ãã«ã½ã¼ã¹ãç¡ããæ¤ç´¢ãã¦ã¿ã¾ããã
https://github.com/abdelm/stationwagon
Stationwagon is full of examples and is updated regularly with the latest Fuel changes.
There are a lot of examples on these classes: - Orm - Auth - Pagination - Validation - and more!
俺訳ï¼
ã¹ãã¼ã·ã§ã³ã¯ã´ã³ã¯ãµã³ãã«ã³ã¼ãéã ã«ãã
FuelPHPã«æ°æ©è½ã追å ããããããã¤ãé 次å®è£ ãã¦ããã«ãã
ORM-èªè¨¼-ãã¼ã¸ãã¼ã·ã§ã³-ããªãã¼ã·ã§ã³ã¨ãã®ãµã³ãã«ãã¤ãã¼ã¤ããã§ãã
ã£ã¦äºã§åå¼·ç¨ã®ããã°ã·ã¹ãã 風ãµã³ãã«ã½ã¼ã¹ã¿ããã§ããï¼ãã®ã¾ã¾è¨ç½®ãã¦å
¬éï¼ã¿ãããªå®ç¨æ§ã¯ãªãã®ã§æ³¨æï¼
ããã¨è¦ã¯ãããªæãï¼
ããããã®ã¯ã¨ããããåãã½ã¼ã¹ãå£ããªããåå¼·ããã®ãä¸çªè¿éï¼ãã®ãããªä¿ºã«æé©ã
åºæ¬ã®ç»é²ã»æ´æ°ã»åé¤ã»ã¦ã¼ã¶ã¼è¿½å ã»ãã°ã¤ã³ã»ã«ãã´ãªã¼ä½æãªã©ã®åºæ¬åä½ç¢ºèªã®1ã¤ã®ãµã³ãã«ã¨ãã¦ã¯ã¾ãè¯ãã®ã§ã¯ï¼(ãã¬ã¼ã ã¯ã¼ã¯å
ã®ã½ã¼ã¹ã®é¢ä¿ã確èªããåæ©ã®åæ©ã®æç¿ãç¨éã§ããä¾å¤å¦ççããããã¦å
¥ã£ã¦ãªãã®ã§ãã®ã¾ã¾å
¬éãã¦ä½¿ãç®çã§ã¯æ±ºãã¦ãªãã§ããã注æãã)
ãã¨ã¯ããã¥ã¡ã³ãã§è¦ããããµã³ãã«ã³ã¼ããããã¤ã«ãºããºãè²¼ã£ã¦å®é¨ãã¦ããã°ãç å ´ã«ã¯ãªããã¨ã
ï¼ãã®ãµã³ãã«ã½ã¼ã¹ã®ã³ã¼ãã®å質ã¯ç¥ãã¾ãããåå¼·ç¨ãµã³ãããã¯ã¹ãä¸ããä½ãã®ãç縮ããç¨éã§ãããããããï¼
2.ãã¦ã³ãã¼ããã¤ã³ã¹ãã¼ã«
0.ç°å¢åå空é使ã£ã¦ãã®ã§PHP5.3以éãããªãã¨åããªãã¢ã«ã
Ubuntu 11.04-Apache/2.2.17-PHP 5.3.5-MySQL5.1.54ã®ä¸è¬çãªéçºç°å¢ã§ãããã以å¤ã¯ã·ã©ã
ãã¨mod_rewriteã¨ããã®è¾ºã¯é©å½ã«ãã£ã¦ãããããé¯å¼ãã®ã§ã«ã¼ãç°å¢å¿ é ãæå ã®ç°å¢ã§ããã¹ã
1.ãã¦ã³ãã¼ã
git cloneã¨ãã¹ãã¼ããªæ¹æ³ãããªãã¦ãZipã§ã¯ã¬ã§ãï½
https://github.com/abdelm/stationwagon/downloads
2.ã¤ã³ã¹ãã¼ã«
é©å½ãªãã£ã¬ã¯ããªã«å±é
(ããã§ã¯/var/vhosts/stationwagon.pizw.com/stationwagon/ )
pizw.comã¯ä¿ºææãã¡ã¤ã³ãWindowsã®hostsãã¡ã¤ã«(C:\Windows\System32\drivers\etc\hosts ããLinuxã®/etc/hostsã«192.168.xx.xx stationwagon.pizw.com çã ã¨ãã¦åå解決ããã¦ã¾ãã
ï¼ãã¼ã«ã«ãããã¯ã¼ã¯ç°å¢ã§ç¹ããããããã°stationwagon.example.comã§ã127.0.0.1ã§ããªãã§ããããï¼
3.DBä½æ&ãã¼ãã«ã¤ã³ãã¼ã
database.sqlã¯å±éããã½ã¼ã¹ç´ä¸ã«ããã¾ãã
# mysqladmin create stationwagon # mysql -uroot stationwagon < database.sql # mysql -uroot stationwagon Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3380 Server version: 5.1.54-1ubuntu4-log (Ubuntu) Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved. This software comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to modify and redistribute it under the GPL v2 license Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show tables; +------------------------+ | Tables_in_stationwagon | +------------------------+ | sw_articles | | sw_categories | | sw_users | +------------------------+ 3 rows in set (0.00 sec) mysql>
ãªãããã¼ãã«ã3ã¤ã§ãã¦ã¾ããã
mysqlã®root権éããããªãã¦ã¼ã¶ã¼ä½ã£ã¦å¥½ãã«æ±ã
4./etc/apache2/sites-available ã®è¨å®
/etc/apache2/sites-available/stationwagon.pizw.com(ä»»æã®æ¥ç¶å
ãã¼ãã£ã«ãã¹ãé¯å) ã®ä½æ
<VirtualHost *:80> ServerName stationwagon.pizw.com ServerAdmin webmaster@localhost DocumentRoot /var/vhosts/stationwagon.pizw.com/stationwagon/public/ <Directory /var/vhosts/stationwagon.pizw.com/stationwagon/public/> Options -Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </Directory> ErrorLog /var/log/apache2/stationwagon-error.log LogLevel warn CustomLog /var/log/apache2/stationwagon-access.log combined </VirtualHost>
/var/vhosts/stationwagon.pizw.com/stationwagon ã®ã¨ããã¯é©å½ã«æ¸ãæãã¦ã¡ã
5./etc/apache2/sites-enabled ã®è¨å®
# cd /etc/apache2/sites-enabled # ln -s ../sites-available/stationwagon.pizw.comï¼ãã£ãä½ã£ããã¡ã¤ã«ï¼ ./ # /etc/init.d/apache2 graceful
ãã¦ããã§ã¤ãªããããã«ãªã£ãã
6.FuelPHPã½ã¼ã¹å
ã®ä¸é¨è¨å®ãã¡ã¤ã«æ¸ãæã
ä¸é¨è¨å®ã®èª¿æ´
fuel/app/config/development/db.php å
'table_prefix' => 'sw_', ã以ä¸ã®æ§ã«è¿½å ã
<?php /** * The development database settings. */ return array( 'default' => array( 'type' => 'pdo', 'connection' => array( 'dsn' => 'mysql:host=localhost;dbname=stationwagon', 'username' => 'root', 'password' => '', ), 'identifier' => '`', 'table_prefix' => 'sw_', 'charset' => 'utf8', 'caching' => false, 'profiling' => false, ), ); ?>
ä½ããããã¼ãã«åã sw_articles,sw_categories,sw_users ã ãã©ã½ã¼ã¹ã§ã¯sw_ç¡ãã®(articles,categories,users)åæã§æ¸ããã¦ããã®ã§äºã
ã¡ãªã¿ã«ä¸æãåããªãã¨ãéçºã¢ã¼ãã§ã¯ï¼
ãããªç¾éºãªdebug_backtraceç»é¢ãåºãã
ãªãªã¼ã¹ã¢ã¼ãã§ã¯ãããªã¨ã©ã¼ç»é¢ï¼
.htaccessã« SetEnv FUEL_ENV production ã追è¨ããã¨ãªãªã¼ã¹ã¢ã¼ãã§åã親åè¨è¨ã
ãã®ã¸ããCodeIgniterã ã¨ãªãããªã¬ãªã¬ããã¯ãã¦ãéå»ã®è¨æ¶ãâ¦
ãã¨ã
ã¨äºç¨®é¡åããããªè¨å®ãã¡ã¤ã«ãããããã©ãéçºã¢ã¼ãã ã¨1ãåå¨ããã¨2ã§ä»è¾¼ãã æ¨æºè¨å®ããªã¼ãã©ã¤ããããããã ãå°å³ã ããªã«ãã«å¬ããã( fuel/app/config/production/db.php ãåå¨ããã¨ãã¡ããåªå )
3.åä½ç¢ºèª
CMS風ã®ãµã¤ã³ã¢ããã»ãã°ã¤ã³ã»ç»é²ã»æ´æ°ã»åé¤ã»ä¸è¦§æ©è½ã»ã«ãã´ãªæ©è½ãããããã§ããï¼ãªããä½æãããããã°ãå¤é¨ããè¦ãæ©è½ãç¡ãæ°ãããï½ã¾ã ä¸èº«è§£æãã¦ãªãã®ã§è©³ç´°ããããããï¼ãµã¤ã³ã¢ãã(users/signup)å¨ãã®åãã¯ãã®è¾º( https://github.com/abdelm/stationwagon/tree/develop/fuel/app/classes/controller/users.php )ãå´©ãã°è§£ããï¼
Auth::ã®æå確èªã«ä½¿ããï¼(users/login)ï¼
ããªãã¼ã·ã§ã³ãDBã¸ã®INSERT-UPDATE-DELETEå¨ãã¯ãã®è¾º(https://github.com/abdelm/stationwagon/tree/develop/fuel/app/classes/controller/articles.php )ãï¼
DBããã®SELECTãä¸è¦§ä½æå¨ãã¯ãã®è¾º(articles,categories) (https://github.com/abdelm/stationwagon/tree/develop/fuel/app/classes/controller/categories.php )ãï¼
ãã¨ã¯ããã¥ã¡ã³ã (http://docs.fuelphp.com/ )ã§ãè¦ãªããã½ã¼ã¹ã解æãã¦è²ã 試ããªããéãã§è¡ãã¾ããã¼ã
4.ãã¡ã¼ã¹ãã¤ã³ãã¬ãã·ã§ã³
ããã¥ã¡ã³ãã¨ã³ã¢ã®ã½ã¼ã¹ãçºãã¤ã¤ãé°å²æ°ã§ã¢ããèªã£ã¦ã¾ãããCodeIgniterã§ç©è¶³ããªãã¨æãã¦ãé¨åãæåããè£å®ããã¦ããæãã§ããä»å¾ä»äºã§ã使ç¨ã§ãããã¡ã³ã¡ã³ã試ãå®é¨ãã¦ããäºå®ã§ãã
æ°ã«å ¥ã£ãã®ã¯
- éçã¯ã©ã¹ã¡ã½ããã®å¼ã³åºããã¡ã¤ã³ï¼$this->load->ããããããªãã¦ããï¼ã
- HMVC,CSRF,ORM,MongoDB,Auth,RESTæ©è½å ¥ã
- åå空é使ç¨ã§æ´çæ´é 好ãã«åªãã
- vendoré ä¸ã«å¤é¨ã½ã¼ã¹è¨ç½®ã§ãããï¼ããã«Zend Frameworkãçªã£è¾¼ãã§ãã¡ã©åã§ãããï¼ï¼
- Sessionæ¹å¼ãã¯ããã¼ã»ãã¡ã¤ã«ã»DBã»memcachedçããè²ã é¸ã¹ã
- ç´°ãããã©validatorã®æ¸ãæ¹ãç´æç ä¾ï¼ $val->add('password', 'Your password')->add_rule('required')
- æ°ãããã¬ã¼ã ã¯ã¼ã¯ã¯æ¢åãã¬ã¼ã ã¯ã¼ã¯ããã½ã¼ã¹ã®åãåããè³¢ããªã£ã¦ããã便å©ããããããããã
MVCãã¬ã¼ã ã¯ã¼ã¯ã£ã¦çªãè©°ãã¦ããã¨ã©ããä¼¼ããããªæãã«åæãã¦ãããã©ã
å¾ããåºã¦ãããã®ã®é
åã¯
ãæ¨æºã§ã¢ã¬ã欲ããããªâä½ããããããæåããèæ
®ãããç¶æ
ãã§çµã¿è¾¼ã¾ãã¦ãäºã ã¨æãã
æåã®é ããæ¨æºã§ããã¹ããã®ã®æ¦å¿µã¯çå®ã«å¤åãã¦ãããã¾ãã«ããã°ã¤ã¤ã¼ã§ãã(ï½·ï¾ï½¯ã
使ãæ £ããCodeIgniterã¯å®çã§ã¨ã¦ãè¯ãããã©ãæ°ããçæã§åºæ¿ã«çããªããä»äºããã®ãããããããï¼
ã§ã¯ã§ã¯ï¼
åè:
PHP ãã¬ã¼ã ã¯ã¼ã¯ã®ãFuelPHPããã¹ã´ããã - A Day in Serenity @ Kenji :
http://d.hatena.ne.jp/Kenji_s/20111117/1321519058