はてブに登録してくれている方がいらっしゃいました。
あ(・∀・)り(・∀・)が(・∀・)と(・∀・)う!ございます!
http://b.hatena.ne.jp/entry/7463386
月次アーカイブ: 2月 2008
【proftpd】 MySQLでアカウント管理する
【apache】 バーチャルホスト作成
久々のサーバ構築案件なので、忘れんようにメモ。
今回作成するホスト www.example.com はバーチャルホストとして作ります。
そして、 www.example.com のFTPアカウントはMySQLで管理します。
今後、FTPアカウントの管理やバーチャルホストのconfigはプログラムにやらせたいなー。
■■ webスペースの領域作成
FTPアカウントは今後MySQLに管理させます。ここのUID/GIDは、UNIXアカウントとは別物です。
FTPアカウントをMySQLに管理させる方法は次のエントリに書きます。乞うご期待!
[root@hoge ~]# mkdir /home/vhost
[root@hoge ~]# mkdir -p /home/vhost/www.example.com/{html,cgi-bin,logs,auth}
[root@hoge ~]# chown -R 1000:1000 /home/vhost/
■■ apache インストール
[root@hoge ~]# yum install httpd mod_ssl
(中略)
Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
httpd i386 2.0.52-38.ent.centos4.2 update 891 k
mod_ssl i386 1:2.0.52-38.ent.centos4.2 update 100 k
Installing for dependencies:
apr i386 0.9.4-24.9 base 93 k
apr-util i386 0.9.4-21 base 51 k
distcache i386 1.4.5-6 base 111 k
httpd-suexec i386 2.0.52-38.ent.centos4.2 update 30 k
Transaction Summary
=============================================================================
Install 6 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 1.2 M
Is this ok [y/N]: y
■■ php5のインストール
通常、CentOS4.x系では、yumコマンドでphpをインストールしようとすると、
PHP4系がインストールされます。しかし、もうサポート終わってるので
(セキュリティサポートは2008年8月まで)
centosplusリポジトリを一時的に有効にしてやってPHP5をインストールしてやります。
[root@hoge ~]# yum --enablerepo=centosplus install php
(中略)
Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
php i386 5.1.6-3.el4s1.8 centosplus 1.1 M
Installing for dependencies:
curl i386 7.12.1-11.el4 base 230 k
libidn i386 0.5.6-1 base 169 k
php-cli i386 5.1.6-3.el4s1.8 centosplus 2.0 M
php-common i386 5.1.6-3.el4s1.8 centosplus 136 k
Transaction Summary
=============================================================================
Install 5 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 3.7 M
Is this ok [y/N]: y
■ さらに、pearもcentosplus経由でインストールする
[root@hoge ~]# yum --enablerepo=centosplus install php-pear
(中略)
Dependencies Resolved
=============================================================================
Package Arch Version Repository Size
=============================================================================
Installing:
php-pear noarch 1:1.4.11-1.el4s1.1 centosplus 345 k
Transaction Summary
=============================================================================
Install 1 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 345 k
Is this ok [y/N]: y
■ pearが有効になっているか確認する
[root@hoge ~]# pear list
Installed packages, channel pear.php.net:
=========================================
Package Version State
Archive_Tar 1.3.1 stable
Console_Getopt 1.2 stable
PEAR 1.4.11 stable
XML_RPC 1.5.1 stable
■■ apacheの設定を修正する
■ httpd.confを修正する
[root@hoge ~]# cd /etc/httpd/conf
[root@ho
ge conf]# cp -p httpd.conf httpd.conf.orig
[root@hoge conf]# vi httpd.conf
#---- ServerTokensディレクティブでの表示を最小限にするため
#---- ここを
#---- ServerTokens OS
#----
#---- こうする
#---- #ServerTokens OS
#---- ServerTokens Prod
#---- サーバ管理者のメールアドレスを変更するため
#---- ここを
#---- ServerAdmin root@localhost
#----
#---- こうする
#---- #ServerAdmin root@localhost
#---- ServerAdmin webmaster@hoge.example.com
#---- すべてvirtualhostで管理するので、以下をコメントアウトする
#----
#---- ここを
#---- DocumentRoot "/var/www/html"
#---- こうする
#---- #DocumentRoot "/var/www/html"
#----
#---- ここを
#----
#---- Options FollowSymLinks
#---- AllowOverride None
#----
#----
#---- こうする
#---- #
#---- # Options FollowSymLinks
#---- # AllowOverride None
#---- #
#----
#---- ここを
#----
#----
#---- #
#---- # Possible values for the Options directive are "None", "All",
#---- # or any combination of:
#---- # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#---- #
#---- # Note that "MultiViews" must be named *explicitly* --- "Options All"
#---- # doesn't give it to you.
#---- #
#---- # The Options directive is both complicated and important. Please see
#---- # http://httpd.apache.org/docs-2.0/mod/core.html#options
#---- # for more information.
#---- #
#---- Options Indexes FollowSymLinks
#----
#---- #
#---- # AllowOverride controls what directives may be placed in .htaccess files.
#---- # It can be "All", "None", or any combination of the keywords:
#---- # Options FileInfo AuthConfig Limit
#---- #
#---- AllowOverride None
#----
#---- #
#---- # Controls who can get stuff from this server.
#---- #
#---- Order allow,deny
#---- Allow from all
#----
#----
#----
#---- こうする
#---- ##### comented by nullpopopo 2008/02/10
#---- #
#---- #
#---- ##
#---- ## Possible values for the Options directive are "None", "All",
#---- ## or any combination of:
#---- ## Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#---- ##
#---- ## Note that "MultiViews" must be named *explicitly* --- "Options All"
#---- ## doesn't give it to you.
#---- ##
#---- ## The Options directive is both complicated and important. Please see
#---- ## http://httpd.apache.org/docs-2.0/mod/core.html#options
#---- ## for more information.
#---- ##
#---- # Options Indexes FollowSymLinks
#---- #
#---- ##
#---- ## AllowOverride controls what directives may be placed in .htaccess files.
#---- ## It can be "All", "None", or any combination of the keywords:
#---- ## Options FileInfo AuthConfig Limit
#---- ##
#---- # AllowOverride None
#---- #
#---- ##
#---- ## Controls who can get stuff from this server.
#---- ##
#---- # Order allow,deny
#---- # Allow from all
#---- #
#---- #
#---- userdirを使わないので無効にする
#----
#---- ここを
#----
#---- #
#---- # UserDir is disabled by default since it can confirm the presence
#---- # of a username on the system (depending on home directory
#---- # permissions).
#---- #
#---- UserDir disable
#----
#---- #
#---- # To enable requests to /~user/ to serve the user's public_html
#---- # directory, remove the "UserDir disable" line above, and uncomment
#---- # the following line instead:
#---- #
#---- #UserDir public_html
#----
#----
#----
#---- こうする
#---- ##### comented by nullpopopo 2008/02/10
#---- #
#---- # #
#---- # # UserDir is disabled by default since it can confirm the presence
#---- # # of a username on the system (depending on home directory
#---- # # permissions).
#---- # #
#---- # UserDir disable
#---- #
#---- # #
#---- # # To enable requests to /~user/ to serve the user's public_html
#---- # # directory, remove the "UserDir disable" line above, and uncomment
#---- # # the following line instead:
#---- # #
#---- # #UserDir public_html
#---- #
#---- #
#----
#---- ServerSignatureをOffにしてApache が生成したドキュメント中の情報を抑制する
#----
#---- ここを
#---- ServerSignature On
#----
#---- こうする
#---- #ServerSignature On
#---- ServerSignature Off
#---- WebDAVを使わないので無効にする
#----
#---- ここを
#---- #
#---- # WebDAV module configuration section.
#---- #
#----
#---- # Location of the WebDAV lock database.
#---- DAVLockDB /var/lib/dav/lockdb
#----
#----
#---- こうする
#---- ##
#---- ## WebDAV module configuration section.
#---- ##
#---- #
#---- # # Location of the WebDAV lock database.
#---- # DAVLockDB /var/lib/dav/lockdb
#---- #
#---- /var/www/cgi-bin/ を使わないので無効にする
#----
#---- ここを
#---- #
#---- # ScriptAlias: This controls which directories contain server scripts.
#---- # ScriptAliases are essentially the same as Aliases, except that
#---- # documents in the realname directory are treated as applications and
#---- # run by the server when requested rather than as documents sent to the client.
#---- # The same rules about trailing "/" apply to ScriptAlias directives as to
#---- # Alias.
#---- #
#---- ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
#----
#---- #
#---- # "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
#---- # CGI directory exists, if you have that configured.
#---- #
#----
#---- AllowOverride None
#---- Options None
#---- Order allow,deny
#---- Allow from all
#----
#----
#---- こうする
#---- ##
#---- ## ScriptAlias: This controls which directories contain server scripts.
#---- ## ScriptAliases are essentially the same as Aliases, except that
#---- ## documents in the realname directory are treated as applications and
#---- ## run by the server when requested rather than as documents sent to the client.
#---- ## The same rules about trai
ling "/" apply to ScriptAlias directives as to
#---- ## Alias.
#---- ##
#---- #ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
#---- #
#---- ##
#---- ## "/var/www/cgi-bin" should be changed to whatever your ScriptAliased
#---- ## CGI directory exists, if you have that configured.
#---- ##
#---- #
#---- # AllowOverride None
#---- # Options None
#---- # Order allow,deny
#---- # Allow from all
#---- #
#---- 言語設定のUTF-8固定を無効にする
#----
#---- ここを
#---- AddDefaultCharset UTF-8
#----
#---- こうする
#---- #AddDefaultCharset UTF-8
#---- AddDefaultCharset Off
#---- 最終行に、バーチャルホスト用のディレクトリをincludeする設定を書く
#----
#---- Include conf/vconf.d/*.conf
ここまで終わったらhttpd.confを保存します。
■ vconf.d 作成
[root@hoge conf]# pwd
/etc/httpd/conf
[root@hoge conf]# mkdir vconf.d
■ vconf.d にダミー用ホストのconfigを書く
[root@hoge conf]# cd vconf.d
[root@hoge vconf.d]# vi www.example.com.conf
DocumentRoot /home/vhost/www.example.com/html
ServerName www.example.com
ErrorLog /home/vhost/www.example.com/logs/error.log
CustomLog /home/vhost/www.example.com/logs/access.log combined
Options Indexes FollowSymlinks MultiViews
AllowOverride Fileinfo AuthConfig
#AllowOverride All
order allow,deny
Allow from all
ScriptAlias /cgi-bin /home/vhost/www.example.com/cgi-bin
Options ExecCGI
AllowOverride Fileinfo AuthConfig
order allow,deny
Allow from all
■ ダミー用ホストのコンテンツディレクトリにテストファイルを置く
[root@hoge vconf.d]# cd /home/vhost/www.example.com/
[root@hoge www.example.com]# basename `pwd` > html/index.html
[root@hoge www.example.com]# vi html/phpinfo.php
phpinfo();
?>
[root@hoge www.example.com]# cd cgi-bin/
[root@hoge cgi-bin]# vi test.cgi
#!/usr/bin/perl
print "Content-type: text/htmlnn";
print "
[root@hoge cgi-bin]# chmod 755 test.cgi
[root@hoge cgi-bin]# cd ../
[root@hoge www.example.com]# chown -R webmaster. *
■■ apacheの設定確認と起動
■ 設定確認
[root@hoge www.example.com]# cd
[root@hoge ~]# httpd -t -D DUMP_VHOSTS
VirtualHost configuration:
192.168.0.110:80 www.example.com (/etc/httpd/conf/vconf.d/www.example.com.conf:1)
wildcard NameVirtualHosts and _default_ servers:
_default_:443 hoge.example.com (/etc/httpd/conf.d/ssl.conf:88)
Syntax OK
■ 起動
[root@hoge ~]# /etc/init.d/httpd start
Starting httpd: [ OK ]
■ 自動起動設定
[root@hoge ~]# chkconfig httpd --list
httpd 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@hoge ~]# chkconfig httpd on
[root@hoge ~]# chkconfig httpd --list
httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
■ エラーログ確認
[root@hoge ~]# cat /var/log/httpd/error_log
[Sun Feb 10 13:26:43 2008] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Sun Feb 10 13:26:44 2008] [notice] Digest: generating secret for digest authentication ...
[Sun Feb 10 13:26:44 2008] [notice] Digest: done
[Sun Feb 10 13:26:44 2008] [notice] LDAP: Built with OpenLDAP LDAP SDK
[Sun Feb 10 13:26:44 2008] [notice] LDAP: SSL support unavailable
[Sun Feb 10 13:26:45 2008] [notice] Apache configured -- resuming normal operations
■■ クライアントのHOSTSファイルで名前解決をして、ブラウザでアクセスする
[hostsへ以下のように書く]
192.168.0.2 www.example.com
[ブラウザで以下のURLにアクセスする]
http://www.example.com/
http://www.example.com/phpinfo.php
http://www.example.com/cgi-bin/test.cgi
【backup】rsyncでローカルのコンテンツをバックアップサーバに転送する
先日、珠玉のエロ画像をどうにかして死守しなければと思った (っ´∀`)っ ゃー です、こんばんは。
前回のエントリーで、sambaの共有ディレクトリ以外をローカルでバックアップしたのですが、やはり珠玉のエロ画像もバックアップしたいし、ローカルに取ったバックアップだけじゃ、そのハードディスクが死んでしまったら目もあてられないので、バックアップサーバを1台作ってそこにrsyncで転送することにしました。
rsyncで転送する利点としては、
1) 初回転送時だけ時間がかかるが、2回目以降は差分のみの転送なので動きが軽い
2) パスフレーズなしの鍵を使ったsshと組み合わせることで、安全かつ楽に転送ができる
でしょう。以下、intra1サーバ(エロ画像ディレクトリがある方。以下バックアップ元)
からバックアップサーバへバックアップを取る作業のメモ書きです。
■■ バックアップ元で鍵を作成する
ホームディレクトリの下に「.ssh」ディレクトリがなければ作成してください。
「.ssh」ディレクトリのパーミッションは700にしてください。
[root@intra1 ~]# cd .ssh/ [root@intra1 .ssh]# ssh-keygen -t rsa -f rsync Generating public/private rsa key pair. Enter passphrase (empty for no passphrase): ← エンターを空打ちします Enter same passphrase again: ← ここでもエンターを空打ちします Your identification has been saved in rsync. Your public key has been saved in rsync.pub. The key fingerprint is: 17:4c:63:65:b5:54:a5:d0:0f:ea:f1:61:0c:07:e5:03 root@intra1.example.com
■■ バックアップサーバへ鍵を送る
[root@intra1 .ssh]# scp rsync.pub root@backup:/root/.ssh/ The authenticity of host 'backup (192.168.0.200)' can't be established. RSA key fingerprint is f6:87:ab:5a:e4:7c:6c:ec:91:71:3f:28:9c:01:c6:0d. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'backup,192.168.0.200' (RSA) to the list of known hosts. root@backup's password: rsync.pub 100% 237 0.2KB/s 00:00
■■ バックアップサーバで鍵を登録する
[root@backup ~]# cd .ssh/ [root@backup .ssh]# cat rsync.pub > authorized_keys
■■ バックアップ元からバックアップサーバへログインしてみる
[root@intra1 .ssh]# ssh -i ./rsync backup
ここでパスフレーズなしでログインできれば、ひとまずはOK。
■■ バックアップサーバ側にて、鍵ログインで行えるコマンドを制限する
[root@backup .ssh]# vi authorized_keys
ssh-rsa AAAAB3NzaC1yc2E~~~
で始まる行を
command="ls" ssh-rsa AAAAB3~~~
に変更する。
■■ 再度バックアップ元からバックアップサーバへログインしてみる
[root@intra1 .ssh]# ssh -i ./rsync backup anaconda-ks.cfg backup bin install.log install.log.syslog Connection to backup closed.
バックアップサーバの/rootディレクトリでlsコマンドを実行した結果が
バックアップ元で表示されれば、ひとまずはOK。
■■ バックアップサーバにおいて、rsyncのみを許可する
バックアップ元からなんでもかんでもコマンドを実行できてしまったら、バックアップ元サーバが何らかの手段で乗っ取られた場合、即ちバックアップサーバも自在に操られる手段を提供してしまうことになります。よって、バックアップサーバではrsyncのみを許可することにしましょう。まずは、コマンドレベルでの制限を行うスクリプトを書いてやります。
[root@backup .ssh]# cd ~/bin/ [root@backup bin]# touch validate-rsync.sh [root@backup bin]# chmod 700 validate-rsync.sh [root@backup bin]# vi validate-rsync.sh
#!/bin/bash case "$SSH_ORIGINAL_COMMAND" in *&*) echo "Rejected" ;; *;*) echo "Rejected" ;; rsync --server*) $SSH_ORIGINAL_COMMAND ;; *) echo "Rejected" ;; esac
■ authorized_keys ファイルを修正する
[root@backup bin]# cd ~/.ssh/ [root@backup .ssh]# vi authorized_keys
ここを
command="ls" ssh-rsa AAAAB3N~~~
こうする
command="/root/bin/validate-rsync.sh" ssh-rsa AAAAB3N~~~
■ sshd_configを修正する
[root@backup .ssh]# cd /etc/ssh/ [root@backup ssh]# cp -p sshd_config sshd_config.orig [root@backup ssh]# vi sshd_config
ここを
#PermitRootLogin yes
こうする
#PermitRootLogin yes
PermitRootLogin forced-commands-only
■ sshdをreloadする
[root@backup ssh]# /etc/init.d/sshd reload Reloading sshd: [ OK ]
■■ 再度バックアップ元からバックアップサーバへログインを試みる
[root@intra1 ~]# ssh -i ./.ssh/rsync backup Rejected Connection to backup closed.
接続が拒絶されたので、OK。
■■ バックアップ元からバックアップサーバへrsyncの試験をする
■ バックアップサーバで適当なディレクトリを作る
[root@backup ~]# mkdir tmp [root@backup ~]# cd tmp/
■ バックアップ元からrsyncで転送をかける
[root@intra1 ~]# cd tmp/ [root@intra1 tmp]# touch hoge [root@intra1 ~]# rsync -auzv --delete -e 'ssh -i /root/.ssh/rsync' /root/tmp/ root@backup:/root/tmp/ building file list ... done ./ hoge sent 95 bytes received 40 bytes 270.00 bytes/sec total size is 0 speedup is 0.00
■ ファイルが転送されたことをバックアップサーバで確認
[root@backup tmp]# ll total 0 -rw-r--r-- 1 root root 0 Feb 3 03:48 hoge
■ バックアップ元でファイルの更新をしてからrsyncで転送をかける
[root@intra1 tmp]# echo hoge > hoge [root@intra1 tmp]# rsync -auzv --delete -e 'ssh -i /root/.ssh/rsync' /root/tmp/ root@backup:/root/tmp/ building file list ... done hoge sent 108 bytes received 40 bytes 98.67 bytes/sec total size is 5 speedup is 0.03
■ 更新されたファイルがバックアップサーバにあることを確認する
[root@backup tmp]# ll total 4 -rw-r--r-- 1 root root 5 Feb 3 03:51 hoge [root@backup tmp]# cat hoge hoge
■ バックアップ元で何もファイルの更新をせずにrsyncで転送をかける
[root@intra1 tmp]# rsync -auzv --delete -e 'ssh -i /root/.ssh/rsync' /root/tmp/ root@backup:/root/tmp/ building file list ... done sent 62 bytes received 2 0 bytes 54.67 bytes/sec total size is 5 speedup is 0.06
■ バックアップサーバにあるファイルに何も更新がかかっていないことを確認する
[root@backup tmp]# ll total 4 -rw-r--r-- 1 root root 5 Feb 3 03:51 hoge
■ バックアップ元でファイルを削除してからrsyncで転送をかける
[root@intra1 tmp]# rm -f hoge [root@intra1 tmp]# rsync -auzv --delete -e 'ssh -i /root/.ssh/rsync' /root/tmp/ root@backup:/root/tmp/ building file list ... done deleting hoge ./ sent 44 bytes received 20 bytes 128.00 bytes/sec total size is 0 speedup is 0.00
■ バックアップサーバでもファイルが削除されたことを確認する
[root@backup tmp]# ll total 0
■■ 本番運用準備
■ バックアップサーバ側での受け入れ態勢を作る
[root@backup ~]# mkdir -p /home/backup/intra1/{home,etc,root}
■ 初回バックアップを行う
下記コマンドの例では、ローカルのバックアック領域とISOイメージのディレクトリを除外して転送しています。
[root@intra1 ~]# rsync -auzv --delete --exclude=backup --exclude=shared/isos -e 'ssh -i /root/.ssh/rsync' /home/ root@backup:/home/backup/intra1/home/ [root@intra1 ~]# rsync -auzv --delete -e 'ssh -i /root/.ssh/rsync' /etc/ root@backup:/home/backup/intra1/etc/ [root@intra1 ~]# rsync -auzv --delete -e 'ssh -i /root/.ssh/rsync' /root/ root@backup:/home/backup/intra1/root/
■ バックアップ元でスクリプトを書く
[root@intra1 ~]# cd bin/backup_pg/ [root@intra1 backup_pg]# touch intra1_backup.sh [root@intra1 backup_pg]# chmod 755 intra1_backup.sh [root@intra1 backup_pg]# vi intra1_backup.sh
#!/bin/bash BPATH=root@backup:/home/backup/intra1 HR="============================================================" echo $HR echo echo "intra1 /home backup" echo rsync -auzv --delete --exclude=backup --exclude=shared/isos -e 'ssh -i /root/.ssh/rsync' /home/ $BPATH/home/ echo echo $HR echo echo "intra1 /etc backup" echo rsync -auzv --delete -e 'ssh -i /root/.ssh/rsync' /etc/ $BPATH/etc/ echo echo $HR echo echo "intra1 /root backup" echo rsync -auzv --delete -e 'ssh -i /root/.ssh/rsync' /root/ $BPATH/root/ echo echo $HR
■■ cronで自動実行の運用にのせる
[root@intra1 ~]# cd /etc/cron.d [root@intra1 cron.d]# vi intra1_backup
00 06 * * * root /root/bin/backup_pg/intra1_backup.sh | mail -s "[$HOSTNAME] `date` intra1_backup" root
以上で終了です。
ね、簡単でしょ?