LoginSignup
8

More than 5 years have passed since last update.

posted at

updated at

CentOS6でnginxによるProxy/WWWサーバを複数台構成としたときのPHP、MySQL、同期方法(lsyncd+rsyncd)について

はじめに

過去に何度か設定したときのテキスト主体のメモから起こしているため、最新情報と違いがある場合や、記載に誤りがある可能性はあります。
また、動作することを優先してしまっているので、セキュリティ的な観点等で指摘していただけると、とても嬉しいです。

基本設定

まずは、ごく基本的な設定。

その前にバージョンアップだ!

CentOS6デフォルトのOpenSSHは5.3で脆弱性を多く抱えているそうなので、これ以降の初期設定前に、「脆弱性が多いというOpenSSH5.3(CentOS6のデフォルト)を6.4にバージョンアップする手順」により、バージョンアップしておくと、2度手間にならず良いと思います。

ユーザ作成

sshdの設定を変更する前にrootログイン状態で、別のユーザを作成します。

shell
# adduser hoge
# passwd hoge

sudo

後回し(やっておくべきでしょう)

sshd

ポート変更、rootログイン禁止、空パスワード禁止、パスワードログイン禁止、PAM無効(が良いらしい)です。

shell
# cp /etc/ssh/sshd_config{,.org}

まずは設定ファイルのバックアップしたら、

shell
# vi /etc/ssh/sshd_config

で次の内容を設定する。

/etc/ssh/sshd_config
    Port [任意のポート番号]
    PermitRootLogin no
    PermitEmptyPasswords no
#    PasswordAuthentication no
    PasswordAuthentication yes
    UsePAM no

上記の任意のポート番号は、後述するセキュリティ設定(iptables)で解放します。

コメントアウトしているPasswordAuthenticationだけは、hogeユーザによる鍵認証ログインを確認してからnoにするのが安全かも知れません。
*さくらのVPSのようにブラウザ(Java)ベースのシリアルコンソールログインだと、コンソールへのテキスト張り付けができない(方法がわからない)ので

sshdを再起動して設定を反映します。

shell
# service sshd restart

hogeユーザで鍵認証ログインを確認したら、上記のPasswordAuthenticationnoにして、sshdを再起動します。

ホスト名設定と反映

shell
cp /etc/sysconfig/network{,.org}
/etc/sysconfig/network
HOSTNAME=[設定したいホスト名]
shell
/etc/rc.d/init.d/network restart

日本語環境

コンソールメッセージやテキストエディット等で日本語を使えるようにします。

言語設定ファイル

shell
# vi /etc/sysconfig/i18n

で以下の内容に変更します。

/etc/sysconfig/i18n
LANG="ja_JP.UTF-8"
SYSFONT="latarcyrheb-sun16"

テキストエディタ(vim)

shell
# vi ~/.vimrc

として以下の内容で新規作成します。

~/.vimrc
set encoding=utf-8
set fileencodings=utf-8,iso-2022-jp,sjis,euc-jp
set tabstop=4
set paste

セキュリティ

Firewall

iptablesで必要なポートのみ解放します。

shell
vi /etc/sysconfig/iptables

このファイルも存在しないはずなので新規作成です。
「さくらのVPS」導入解説 その10:iptablesの設定がわかりやすいです。

/etc/sysconfig/iptables
*filter
:INPUT   ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT  ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]

-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# SSH, HTTP, FTP1, FTP2, MySQL
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport [任意のポート番号] -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80    -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443    -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 20    -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21    -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3306  -j ACCEPT

-A RH-Firewall-1-INPUT -p tcp --dport 22001 -m state --state NEW -m recent --update --seconds 240 --hitcount 8 --rttl --name SSH -j DROP
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited

COMMIT

iptablesを再起動します。

shell
# /etc/rc.d/init.d/iptables restart

別ウィンドウから、hogeユーザでsshログインできることを確認します。

パッケージの最新化

まず、CentOSのパッケージ管理システムであるyumのデータベースを最新化します。

shell
yum update

ここまでで基本設定は終わりです。

ミドルウェアのインストールと設定

正直なところ咀嚼しきれていない箇所もありますが。

httpd、PHP、MySQLをまとめてインストール

shell
# yum -y install httpd mysql-server php php-mysql php-gd pcre-devel zlib-devel openssl-devel libxslt-devel GeoIP-devel gd-devel

PHP-GDやらopensslなどなどインストールしてます。

nginxのインストール

shell
# cd /root
# wget http://dl.fedoraproject.org/pub/epel/6/SRPMS/nginx-1.0.15-2.el6.src.rpm
# rpm -ivh nginx-1.0.15-2.el6.src.rpm

# cd /root/rpmbuild/SOURCES
# wget http://nginx.org/download/nginx-1.3.4.tar.gz
# wget http://labs.frickle.com/files/ngx_cache_purge-1.6.tar.gz

# cd /root/rpmbuild/SPECS
# yum -y install perl-ExtUtils-Embed

# mv nginx.spec

nginx.specというファイルは、上記でリネーム保存して、以下の内容で作成します。
*ホントはどこかからダウンロードしたんだと思います。今後確認します。

nginx.spec
%define nginx_user      nginx
%define nginx_group     %{nginx_user}
%define nginx_home      %{_localstatedir}/lib/nginx
%define nginx_home_tmp  %{nginx_home}/tmp
%define nginx_logdir    %{_localstatedir}/log/nginx
%define nginx_confdir   %{_sysconfdir}/nginx
%define nginx_datadir   %{_datadir}/nginx
%define nginx_webroot   %{nginx_datadir}/html

Name:           nginx
Version:        1.3.4
Release:        1%{?dist}
Summary:        Robust, small and high performance HTTP and reverse proxy server
Group:          System Environment/Daemons   

# BSD License (two clause)
# http://www.freebsd.org/copyright/freebsd-license.html
License:        BSD
URL:            http://nginx.net/ 
BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)

BuildRequires:      pcre-devel,zlib-devel,openssl-devel,perl(ExtUtils::Embed)
BuildRequires:      libxslt-devel,GeoIP-devel,gd-devel
Requires:           perl(:MODULE_COMPAT_%(eval "`%{__perl} -V:version`"; echo $version))
# for aio
Requires:           kernel >= 2.6.18-181
# for /usr/sbin/useradd
Requires(pre):      shadow-utils
Requires(post):     chkconfig
# for /sbin/service
Requires(preun):    chkconfig, initscripts
Requires(postun):   initscripts
Provides:           webserver

Source0:    http://nginx.org/download/nginx-%{version}.tar.gz
Source1:    %{name}.init
Source2:    %{name}.logrotate
Source3:    virtual.conf
Source4:    ssl.conf
Source5:    %{name}.sysconfig
Source6:    nginx.conf
Source10:   http://labs.frickle.com/files/ngx_cache_purge-1.6.tar.gz
Source100:  index.html
Source101:  poweredby.png
Source102:  nginx-logo.png
Source103:  50x.html
Source104:  404.html

# removes -Werror in upstream build scripts.  -Werror conflicts with
# -D_FORTIFY_SOURCE=2 causing warnings to turn into errors.
Patch0:     nginx-auto-cc-gcc.patch

%description
Nginx [engine x] is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3
proxy server written by Igor Sysoev.

%prep
%setup -q

%patch0 -p0

%setup -T -D -a 10

%build
# nginx does not utilize a standard configure script.  It has its own
# and the standard configure options cause the nginx configure script
# to error out.  This is is also the reason for the DESTDIR environment
# variable.  The configure script(s) have been patched (Patch1 and
# Patch2) in order to support installing into a build environment.
export DESTDIR=%{buildroot}
./configure \
    --user=%{nginx_user} \
    --group=%{nginx_group} \
    --prefix=%{nginx_datadir} \
    --sbin-path=%{_sbindir}/%{name} \
    --conf-path=%{nginx_confdir}/%{name}.conf \
    --error-log-path=%{nginx_logdir}/error.log \
    --http-log-path=%{nginx_logdir}/access.log \
    --http-client-body-temp-path=%{nginx_home_tmp}/client_body \
    --http-proxy-temp-path=%{nginx_home_tmp}/proxy \
    --http-fastcgi-temp-path=%{nginx_home_tmp}/fastcgi \
    --http-uwsgi-temp-path=%{nginx_home_tmp}/uwsgi \
    --http-scgi-temp-path=%{nginx_home_tmp}/scgi \
    --pid-path=%{_localstatedir}/run/%{name}.pid \
    --lock-path=%{_localstatedir}/lock/subsys/%{name} \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_xslt_module \
    --with-http_image_filter_module \
    --with-http_geoip_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_degradation_module \
    --with-http_stub_status_module \
    --with-http_perl_module \
    --with-mail \
    --with-file-aio \
    --with-mail_ssl_module \
    --with-ipv6 \
    --with-cc-opt="%{optflags} $(pcre-config --cflags)" \
    --with-cc-opt="%{optflags} $(pcre-config --cflags)" \
    --add-module=%{_builddir}/nginx-%{version}/ngx_cache_purge-1.6
make %{?_smp_mflags} 

mv ngx_cache_purge-1.6/CHANGES ngx_cache_purge-1.6/CHANGES.ngx_cache_purge
mv ngx_cache_purge-1.6/README.md ngx_cache_purge-1.6/README.md.ngx_cache_purge

%install
rm -rf %{buildroot}
make install DESTDIR=%{buildroot} INSTALLDIRS=vendor
find %{buildroot} -type f -name .packlist -exec rm -f {} \;
find %{buildroot} -type f -name perllocal.pod -exec rm -f {} \;
find %{buildroot} -type f -empty -exec rm -f {} \;
find %{buildroot} -type f -exec chmod 0644 {} \;
find %{buildroot} -type f -name '*.so' -exec chmod 0755 {} \;
chmod 0755 %{buildroot}%{_sbindir}/nginx
%{__install} -p -D -m 0755 %{SOURCE1} %{buildroot}%{_initrddir}/%{name}
%{__install} -p -D -m 0644 %{SOURCE2} %{buildroot}%{_sysconfdir}/logrotate.d/%{name}
%{__install} -p -D -m 0644 %{SOURCE5} %{buildroot}%{_sysconfdir}/sysconfig/%{name}
%{__install} -p -d -m 0755 %{buildroot}%{nginx_confdir}/conf.d
%{__install} -p -m 0644 %{SOURCE3} %{SOURCE4} %{buildroot}%{nginx_confdir}/conf.d
%{__install} -p -m 0644 %{SOURCE6} %{buildroot}%{nginx_confdir}
%{__install} -p -d -m 0755 %{buildroot}%{nginx_home_tmp}
%{__install} -p -d -m 0755 %{buildroot}%{nginx_logdir}
%{__install} -p -d -m 0755 %{buildroot}%{nginx_webroot}
%{__install} -p -m 0644 %{SOURCE100} %{SOURCE101} %{SOURCE102} %{SOURCE103} %{SOURCE104} %{buildroot}%{nginx_webroot}

# convert to UTF-8 all files that give warnings.
for textfile in CHANGES
do
    mv $textfile $textfile.old
    iconv --from-code ISO8859-1 --to-code UTF-8 --output $textfile $textfile.old
    rm -f $textfile.old
done

%clean
rm -rf %{buildroot}

%pre
if [ $1 == 1 ]; then
    %{_sbindir}/useradd -c "Nginx user" -s /bin/false -r -d %{nginx_home} %{nginx_user} 2>/dev/null || :
fi

%post
if [ $1 == 1 ]; then
    /sbin/chkconfig --add %{name}
fi

%preun
if [ $1 = 0 ]; then
    /sbin/service %{name} stop >/dev/null 2>&1
    /sbin/chkconfig --del %{name}
fi

%postun
if [ $1 == 2 ]; then
    /sbin/service %{name} upgrade || :
fi

%files
%defattr(-,root,root,-)
%doc LICENSE CHANGES README
%doc ngx_cache_purge-1.6/CHANGES.ngx_cache_purge ngx_cache_purge-1.6/README.md.ngx_cache_purge
%{nginx_datadir}/
%{_sbindir}/%{name}
%{_mandir}/man3/%{name}.3pm.gz
%{_initrddir}/%{name}
%dir %{nginx_confdir}
%dir %{nginx_confdir}/conf.d
%dir %{nginx_logdir}
%config(noreplace) %{nginx_confdir}/conf.d/*.conf
%config(noreplace) %{nginx_confdir}/win-utf
%config(noreplace) %{nginx_confdir}/%{name}.conf.default
%config(noreplace) %{nginx_confdir}/mime.types.default
%config(noreplace) %{nginx_confdir}/fastcgi.conf
%config(noreplace) %{nginx_confdir}/fastcgi.conf.default
%config(noreplace) %{nginx_confdir}/fastcgi_params
%config(noreplace) %{nginx_confdir}/fastcgi_params.default
%config(noreplace) %{nginx_confdir}/scgi_params
%config(noreplace) %{nginx_confdir}/scgi_params.default
%config(noreplace) %{nginx_confdir}/uwsgi_params
%config(noreplace) %{nginx_confdir}/uwsgi_params.default
%config(noreplace) %{nginx_confdir}/koi-win
%config(noreplace) %{nginx_confdir}/koi-utf
%config(noreplace) %{nginx_confdir}/%{name}.conf
%config(noreplace) %{nginx_confdir}/mime.types
%config(noreplace) %{_sysconfdir}/logrotate.d/%{name}
%config(noreplace) %{_sysconfdir}/sysconfig/%{name}
%dir %{perl_vendorarch}/auto/%{name}
%{perl_vendorarch}/%{name}.pm
%{perl_vendorarch}/auto/%{name}/%{name}.so
%attr(-,%{nginx_user},%{nginx_group}) %dir %{nginx_home}
%attr(-,%{nginx_user},%{nginx_group}) %dir %{nginx_home_tmp}


%changelog
* Wed Apr 27 2011 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.8.54-1
- Update to new legacy stable 0.8.54

* Sun Oct 31 2010 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.8.53-1
- Update to new stable 0.8.53 since 0.6.x branch is no longer supported

* Sun Jun 20 2010 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.39-5
- fix bug #591543

* Mon Feb 15 2010 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.39-4
- change directory ownership of log dir to root:root

* Mon Feb 15 2010 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.39-3
- fix bug #554914 

* Fri Dec 04 2009 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.39-2
- fixes CVE-2009-3555

* Mon Sep 14 2009 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.39-1
- update to 0.6.39
- fixes CVE-2009-2629

* Sun Aug 02 2009 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.38-1
- update to 0.6.38

* Sat Apr 11 2009 Jeremy Hinegardner <jeremy at hinegardner dot org> 0.6.36-1
-  update to 0.6.36

* Thu Feb 19 2009 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.35-2
- rebuild

* Thu Feb 19 2009 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.35-1
- update to 0.6.35

* Tue Dec 30 2008 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.34-1
- update to 0.6.34
- Fix inclusion of /usr/share/nginx tree => no unowned directories [mschwendt]

* Sun Nov 23 2008 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.33-1
- update to 0.6.33

* Sun Jul 27 2008 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.32-1
- update to 0.6.32
- nginx now supports DESTDIR so removed the patches that enabled it

* Mon May 26 2008 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.31-3
- update init script
- remove 'default' listen parameter

* Tue May 13 2008 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.31-2
- added missing Source files

* Mon May 12 2008 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.6.31-1
- update to new upstream stable branch 0.6
- added 3rd party module nginx-upstream-fair
- add /etc/nginx/conf.d support [#443280]
- use /etc/sysconfig/nginx to determine nginx.conf [#442708]
- added default webpages
- add Requires for versioned perl (libperl.so) (via Tom "spot" Callaway)
- drop silly file Requires (via Tom "spot" Callaway)

* Sat Jan 19 2008 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.5.35-1
- update to 0.5.35

* Sun Dec 16 2007 Jeremy Hinegardner <jeremy at hinegardner dot org> - 0.5.34-1
- update to 0.5.34

* Mon Nov 12 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.33-2
- bump build number - source wasn't update

* Mon Nov 12 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.33-1
* update to 0.5.33

* Mon Sep 24 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.32-1
- updated to 0.5.32
- fixed rpmlint UTF-8 complaints.

* Sat Aug 18 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.31-3
- added --with-http_stub_status_module build option.
- added --with-http_sub_module build option.
- add in pcre-config --cflags

* Sat Aug 18 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.31-2
- remove BuildRequires: perl-devel

* Fri Aug 17 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.31-1
- Update to 0.5.31
- specify license is BSD

* Sat Aug 11 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.30-2
- Add BuildRequires: perl-devel - fixing rawhide build

* Mon Jul 30 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.30-1
- Update to 0.5.30

* Tue Jul 24 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.29-1
- Update to 0.5.29

* Wed Jul 18 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.28-1
- Update to 0.5.28

* Mon Jul 09 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.27-1
- Update to 0.5.27

* Mon Jun 18 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.26-1
- Update to 0.5.26

* Sat Apr 28 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.19-1
- Update to 0.5.19

* Mon Apr 02 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.17-1
- Update to 0.5.17

* Mon Mar 26 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.16-1
- Update to 0.5.16
- add ownership of /usr/share/nginx/html (#233950)

* Fri Mar 23 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.15-3
- fixed package review bugs (#235222) given by ruben@rubenkerkhof.com

* Thu Mar 22 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.15-2
- fixed package review bugs (#233522) given by kevin@tummy.com

* Thu Mar 22 2007 Jeremy Hinegardner <jeremy@hinegardner.org> - 0.5.15-1
- create patches to assist with building for Fedora
- initial packaging for Fedora

そして今度はパッケージをローカルで作成します。

shell
# rpmbuild -bb nginx.spec

nginxをアップグレードモードでインストールします。

shell
# rpm -Uvh /root/rpmbuild/RPMS/x86_64/nginx-1.3.4-1.el6.x86_64.rpm

FastCGI版PHPのインストール

shell
# wget http://sakuratan.biz/nginx/php-fastcgi
# mv php-fastcgi /etc/init.d/
# chmod 755 /etc/init.d/php-fastcgi
# mkdir /var/run/nginx
# chown nginx:nginx /var/run/nginx

# rpm -qa spawn-fcgi
# yum install -y spawn-fcgi
# /etc/init.d/php-fastcgi start

# wget http://sakuratan.biz/nginx/nginx.conf

nginx.confの設定は今後追加します。

nginxの設定確認と起動

shell
# /etc/init.d/nginx configtest
# /etc/init.d/nginx start

FastCGIとnginxのサーバブート時の自動起動設定

shell
# chkconfig --list php-fastcgi
# chkconfig php-fastcgi on
# chkconfig --list php-fastcgi

# chkconfig --list nginx
# chkconfig nginx on
# chkconfig --list nginx

MySQL

起動とサーバブート時の自動起動設定

shell
# /etc/init.d/mysqld start
# /sbin/chkconfig mysqld on

MySQL初期設定

shell
# mysql_secure_installation

ログインを確認。

shell
# mysql -uroot -p

*原則、上記までの一切の手順は、オリジンサーバ以外にも同じ設定が必要か(プロキシはMySQL不要など)は適宜判断してください。

同期(lsyncd+rsyncd)

同期用ユーザの作成

★後述する手順では、好ましくないがrootでlsyncしているため、syncuser作成は不要?

shell
useradd syncuser
passwd syncuser
usermod -G nginx syncuser

★後述する手順では、好ましくないがrootでlsyncしているため、syncuser作成は不要?

同期ユーザの公開鍵作成

★後述する手順では、好ましくないがrootでlsyncしているため、syncuser作成は不要?

同期元(オリジンサーバ)/同期先で鍵を作成します。

shell
# su syncuser
$ cd
$ mkdir .ssh
$ cd .ssh
$ ssh-keygen -t rsa
$ chmod 700 .

各サーバで作成した~/.ssh/id_rsa.pubを相互の/home/syncuser/.ssh/authorized_keysとして保存し、その後syncuserにて

shell
$ chmod 600 authorized_keys

とする。

rsyncdの起動

shell
yum -y install lsyncd

自動起動スクリプトを作成します。

shell
vi /etc/init.d/lsyncd
/etc/init.d/lsyncd
#!/bin/bash
#
# lsyncd
#
# chkconfig: - 99 20
# description: lsyncd auto start script

start() {
    /usr/bin/lsyncd /etc/lsyncd.conf
}

stop() {
    /bin/kill -9 `/sbin/pidof lsyncd`
    until [ -z $(/sbin/pidof lsyncd) ]; do :; done
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
        pid=`pidof lsyncd`
        if [ $? -eq 0 ]; then
            echo "lsyncd (pid $pid) is running..."
        else
            echo "lsyncd is not running"
        fi
        ;;
    *)
        echo "Usage: lsyncd {start|stop|restart|status}"
        exit 1
esac

exit $?

lsyncdの設定

shell
cp /usr/share/doc/lsyncd-2.0.4/examples/lrsync.lua /etc/lsyncd.conf
vi /etc/lsyncd.conf
/etc/lsyncd.conf
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync.
--
settings = {
        statusFile = "/tmp/lsyncd.stat",
        statusInterval = 1,
        logfile = "/var/log/lsyncd.log",
}

sync{
        default.rsync,
        source="/var/www/foo/html/",
        target="www1.example.com:/var/www/foo/html/",
        rsyncOps="-az",
        excludeFrom="/etc/rsync_exclude.lst",
}
sync{
        default.rsync,
        source="/var/www/foo/html/",
        target="www2.example.com:/var/www/foo/html/",
        rsyncOps="-az",
        excludeFrom="/etc/rsync_exclude.lst",
}

lsyncdの起動とサーバブート時自動起動

shell
# /sbin/chkconfig lsyncd on
# /etc/init.d/lsyncd start

注意事項

同期元サーバから、各サーバへは、CUIで1度SSHログインしておく。
known_hostsが作成されないため。
★このSSHログインは、好ましくないがrootのみで、syncuserは不要かも知れない?

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
8