2011年1月9日日曜日

さくらインターネット VPS980へのLAMP環境のセットアップ

さくらインターネットでは、昨年夏に仮想専用サーバーVPS980が提供されるようになりました。



既にいくつものサイトで紹介されていますが、異なる使い方をしているVPS980を複数契約しているため、少しずつ違いますが、今回はMagentoをセットアップするためのLAMP環境のセットアップ方法をまとめておきます。





1. 基本設定


1.1 接続情報の受け取り

さくらインターネットのサイトで、VPS980を申し込むと、次のメールが順次届きます。


  • お申込受付完了のお知らせ [会員番号]
  • [さくらのVPS] 仮登録完了のお知らせ
  • クレジットカードによるお支払いについてのお知らせ(カード払いの場合)


「[さくらのVPS] 仮登録完了のお知らせ」のメールに記載のIPアドレス、ユーザー名(root)とパスワードで、VPSへ接続します。


1.2 コントロールパネルへのログイン


 前項の接続情報を使い、コントロールパネルへログインします。


さくらインターネット コントロールパネル

はじめてログインしたVPSコントロールパネル(サーバーは停止状態)
 
「起動」ボタンを押し、サーバーを起動します。

1.2 はじめに設定すること

参考資料 CentOSをサーバーとして活用するための基本的な設定

さくらのVPSを借りたら真っ先にやるべきssh設定(2001/09/24追加)

1,000円を切る低価格で登場、「さくらのVPS」をチェック――サーバー構築編


1.2.1 rootのパスワード変更

まずは、さくらインターネットから渡された root のパスワードを変更します。
   # passwd
   Changing password for user root.
   New UNIX password:**********
   Retype new UNIX password:**********
   passwd: all authentication tokens updated successfully.
   #

1.2.2 sshのポート番号変更

sshのポート番号を22から10022に変更します。アタックをかける側は、まずデフォルトのポートを狙ってくるので、少ない作業の割には効果があると言われています。

   # vi /etc/ssh/sshd_config
   11 # default value.
   12
   13 #Port 22
   14 Port 10022 ← この行を追加
   15 #Protocol 2,1
   16 Protocol 2


1.2.3 作業用ユーザーの作成

Unix系OSでは、OS上での作業は作業用ユーザー(一般ユーザー)で行い、必要なときだけrootユーザーになり作業することが多くあります。
ここでは作業用ユーザーとして sysad を作成します。

   # groupadd engineer
   # useradd -d /home/sysad -m -g engineer sysad
   # passwd sysad
   Changing password for user admin.
   New UNIX password:*************
   Retype new UNIX password:*************
   passwd: all authentication tokens updated successfully.


1.2.4 作業用ユーザーがsudoコマンドを使用できるように設定

rootの権限でコマンドを実行するには、自らrootユーザーになる場合とsudoコマンドを使用する2つの方法があります。sudoは操作ログが残るため、原則sudoコマンドでroot権限が必要なコマンドを実行するようにしましょう。ユーザー sysadでsudoコマンドを実行出来るようにするためには、次の操作で設定ファイルを変更します。

   # visudo
   74 ##
   75 ## Allow root to run any commands anywhere
   76 root    ALL=(ALL)       ALL
   77 sysad   ALL=(ALL)       ALL ← この行を追加

ここで、一旦ログアウトし、作業用ユーザーでログインします。


1.2.5 rootでのssh接続を禁止

前項でユーザー sysad が sudo コマンドを使えるようにしたため、早速sudoコマンドを使用します。
   # su - sysad
   $ sudo vi /etc/ssh/sshd_config
   [sudo] password for admin: ************
   39 #LoginGraceTime 2m
   40 #PermitRootLogin yes
   41 PermitRootLogin no ← この行を追加
   42 #StrictModes yes
   43 #MaxAuthTries 6
   $ sudo /sbin/service sshd restart
   Stopping sshd:                                [  OK  ]
   Starting sshd:                                [  OK  ]
   $


1.2.6 公開鍵認証でssh接続できるように変更

省略


1.2.7 プロトコルと接続元制限

省略


1.2.8 パッケージのアップデート

CentOSおよび周辺のパッケージは、ときどきアップデートしている。次のコマンドでパッケージを最新版に更新します。ときどき、実行しておきましょう。

   $ sudo yum update
   Loaded plugins: fastestmirror
   Determining fastest mirrors
   
   Total download size: 131 M
   Is this ok [y/N]: y ← 更新する場合は y を入力します

2010年11月くらいは56 Mだったのですが、2011年1月には131Mが更新するまでになっていました。



1.2.9 iptablesによるファイアウォールの設定

    $ sudo system-config-securitylevel-tui

“Customize”を選択すると、次の画面が表示され、プロトコル別にファイアウォールの開け閉めが設定できます。次の例では、SSH、HTTP、HTTPS、SMTPのみファイアウォールを通すように設定しています。名前のないポートを開けるには、”Other ports”にポート番号を入力します。複数ある場合は、ポート番号をカンマで区切り入力します。



OKをクリックし、設定を終了します。

1.2.10 root宛のメールを他のユーザーに転送

CentOSは、プログラムの実行結果などを何かと root ユーザーへメールで通知します。そのたびに root ユーザーになるのは手間なため、 root 宛に来たメールは admin@example.co.jp へ転送しておくと楽でしょう。

   $ sudo vi /etc/aliases ← /etc/aliasesファイルを編集
   95 # Person who should get root's mail
   96 #root:      marc
   97 root:       admin@example.co.jp ← この行を追加

次のコマンドで設定を有効にします。(/etc/aliases の変更を適用します)

   $ sudo newaliases
   /etc/aliases: 77 aliases, longest 22 bytes, 791 bytes total

では、実際にメールが転送されるかテストします。

   $ mail root
   Subject: Test mail ← メールの件名
   This is a test mail from root on sakura.
       ← メールの本文。最後にピリオドだけの行を入力
   .
   Cc: ← 指定してもいいですが、何もせずEnter
   $

これで、admin@example.co.jp 宛にメールが届けば設定は終了です。

なお、さくらインターネット VPSの場合、お試し期間中は外向けの25番ポートは閉じているので、お試し期間中に上記設定を行っても、メールは送信されないので要注意です。


1.2.11 不要なデーモンの停止

一旦、省略。設定する場合は、上記資料を参照。
   $ su -
   # chkconfig acpid off
   # chkconfig auditd off
   # chkconfig autofs off
   # chkconfig avahi-daemon off
   # chkconfig bluetooth off
   # chkconfig cups off
   # chkconfig firstboot off
   # chkconfig gpm off
   # chkconfig haldaemon off
   # chkconfig hidd off
   # chkconfig isdn off   (そもそも存在しません)
   # chkconfig kudzu off
   # chkconfig lvm2-monitor off
   # chkconfig mcstrans off
   # chkconfig mdmonitor off
   # chkconfig messagebus off
   # chkconfig netfs off
   # chkconfig nfslock off
   # chkconfig pcscd off
   # chkconfig portmap off
   # chkconfig rawdevices off
   # chkconfig restorecond off
   # chkconfig rpcgssd off
   # chkconfig rpcidmapd off
   # chkconfig smartd off
   # chkconfig xfs off
   # chkconfig yum-updatesd off
   # exit


1.2.6 不要なコンソールを停止

初期状態で停止していました。VPSの当初提供時から変更されているのかもしれません。

   $ sudo vi /etc/inittab
   44 # Run gettys in standard runlevels
   45 1:2345:respawn:/sbin/mingetty tty1
   46 # 2:2345:respawn:/sbin/mingetty tty2
   47 # 3:2345:respawn:/sbin/mingetty tty3
   48 # 4:2345:respawn:/sbin/mingetty tty4
   49 # 5:2345:respawn:/sbin/mingetty tty5
   50 # 6:2345:respawn:/sbin/mingetty tty6


1.2.5 selinuxを無効に設定

初期状態で既に無効になっていました。VPSの当初提供時から変更されているのかもしれません。

   $ sudo vi /etc/sysconfig/selinux
   6 SELINUX=disabled

ここで、VPSを再起動。VPSの再起動は、コマンドラインから

   $ su -
   # shutdown -r now

当初、起動時でメモリを220MBくらい使っていたが、不要なデーモンを止めて、150MBまで減らせた。


1.2.6 ホスト名の確認

ここで、さくらインターネット VPSのホスト名を確認しておきます。VPSではあらかじめホスト名が割り当てられています。何に使うわけではありませんが、一度確認しておくのもよいでしょう。

   $ hostname
   hogehoge.sakura.ne.jp



2. 利用ソフトのインストール  当初、ソースファイルから構築しようとしたものの、PHP、MySQLの64bitの設定から、うまくいかなかったため、次の記事を参考に、yumによりソフトウェアをインストールしました。




    1,000円を切る低価格で登場、「さくらのVPS」をチェック――サーバー構築編     http://sourceforge.jp/magazine/10/10/04/0020218




2.1 Apache 2.1.1 インストール


 次のコマンドで、インストールします。


   $ sudo yum install httpd
   [sudo] password for admin:**********
      → rootのパスワードを入力
    :    Transaction Summary
   ==========================================================    Install 1 Package(s)
   Upgrade 0 Package(s)
   Total download size: 1.2 M
   Is this ok [y/N]: y → y を入力


   (以降、yumによるインストールでは同様)
 Apacheを起動し、正常に実行されるか確認します。


   $ sudo /sbin/service hyttpd start

 OSの起動時、Apacheが起動するように設定します。


   $ sudo /sbin/chkconfig httpd on



2.1.2 設定


(1) Webアプリケーションを置くディレクトリを作成


   $ sudo mkdir /srv/www/
   $ chmod 1777 /srv/www/



(2) 設定ファイルを変更 yumでインストールしたら、設定ファイルは、
 Apache 1.3の頃のように、すべて httpd.conf に記述するようになっていた。


   $ sudo vi /etc/httpd/conf/httpd.conf
   DirectoryIndex index.html index.html.var index.php


   コメントアウトを消して有効化する
   AddHandler cgi-script .cgi


   安全のため
   ServerTokens Prod
   ServerSignature Off
   ...


   実際には1行で
   LogFormat "%V %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" vcombined
   ...
   <Directory /srv/www>
     AllowOverride All
     Order allow,deny
     Allow from all
   </Directory> 
   ...


   ドメイン名=フォルダ名で振り分け可能にする
   <VirtualHost *:80>
     ServerName CatchAll
     ServerAdmin sysad@example.co.jp
     ErrorLog /var/log/httpd/error.log
     CustomLog /var/log/httpd/access.log vcombined


     AllowEncodedSlashes On
     VirtualDocumentRoot /srv/www/%0
     VirtualScriptAlias /srv/www/%0
   </VirtualHost>


2.1.3 確認
(1) 起動確認


 ここまで設定したところで、次のコマンドによりApacheを起動


   $ sudo /sbin/service httpd restart
   $ ps -ef | grep httpd


 Apacheが無事起動していたら、OS自体を再起動し、起動することを確認する。

   $ sudo /sbin/shutdown -r now




(2) 設定、アクセスの確認
 www.example.co.jpのホームページ用ディレクトリを作成し、確認します。

   $ su - 
   # cd /srv/www 
   # mkdir www.example.co.jp 
   # cd www.example.co.jp 
   # vi index.html 
   <html> 
   <head>
   <title>This is a www.example.co.jp test page.</title>
   </head> 
   <body>This is a www.example.co.jp test page.</body> 
   </html>  


続いて、ローカルPCのhostsファイルを編集し、www.example.co.jpがさくらインターネットのVPSを指すように設定します。Macでなく、Windowsの場合、hostsファイルは C:\Windows\system32\drives\etc\hosts にあります。hostsファイルに次の行を追加します。
もちろん、自分以外が見れるようにするには、ネームサーバーへの登録が必要で、hostsファイルへの登録は、あくまでテスト目的です。


   127.0.0.1     localhost 
   # ::1 localhost 
   VPS980のIPアドレス www.example.co.jp → この行を追加  


Webブラウザから http://www.example.co.jp/ へアクセスし、上記で作成したテストファイルが表示されたら、Apacheおよび仮想ホストを含めた設定ファイルの確認は終了です。
hostsファイルを編集したことを忘れ「サイトがおかしい」と勘違いしないよう、上記で編集した hosts ファイルは元に戻しておきましょう。


2.2 MySQL 5.1 


MySQLをインストールする前に、MySQLの実行ユーザーを作成するなど、インストールの準備を行います。


以下、すべて1行で入力します。
   $ sudo yum install mysql-server 
   $ sudo vi /etc/my.cnf 
    7 old_passwords=1 
    8 character-set-server=utf8 
    9 
   10 # Disabling symbolic-links is recommended to prevent assorted security risks; 
   11 # to do so, uncomment this line: 
   12 # symbolic-links=0 
   13 
   14 [mysqld_safe] 
   15 log-error=/var/log/mysqld.log 
   16 pid-file=/var/run/mysqld/mysqld.pid 
   17 
   18 [mysqldump] 
   19 default-character-set=bynary 
   20 
   21 [client] 
   22 default-character-set=utf8


2.2.2 設定 最初は、権限テーブル等を作成します。

   $ sudo mysql_install_db 


MySQLを起動し、かつOS起動時MySQLが起動されるように設定します。


   $ sudo /sbin/service mysqld start 
   $ sudo /sbin/chkconfig mysqld on 


ここで、MySQLへ接続できるか確認します。


   $ mysql -u root 接続できたら終了します。 


   > exit 


さらに、MySQLのrootユーザーにパスワードを設定します。


   $ sudo mysql_secure_installation 



   $ sudo mysql_secure_installation
   NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
   SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!    In order to log into MySQL to secure it, we'll need the current    password for the root user. If you've just installed MySQL, and    you haven't set the root password yet, the password will be blank,    so you should just press enter here.    Enter current password for root (enter for none):       → Enterキーを押します    OK, successfully used password, moving on...    Setting the root password ensures that nobody can log into the MySQL    root user without the proper authorisation.    Set root password? [Y/n]       → Enterキーを押します    New password:**********       → rootに設定するパスワードを入力    Re-enter new password:**********       → 同じパスワードを入力    Password updated successfully!    Reloading privilege tables..    ... Success!    By default, a MySQL installation has an anonymous user, allowing anyone    to log into MySQL without having to have a user account created for    them. This is intended only for testing, and to make the installation    go a bit smoother. You should remove them before moving into a    production environment.    Remove anonymous users? [Y/n]       → Enterキーを押します    ... Success!    Normally, root should only be allowed to connect from 'localhost'. This    ensures that someone cannot guess at the root password from the network.    Disallow root login remotely? [Y/n]       → Enterキーを押します    ... Success!    By default, MySQL comes with a database named 'test' that anyone can    access. This is also intended only for testing, and should be removed    before moving into a production environment.    Remove test database and access to it? [Y/n]       → Enterキーを押します    - Dropping test database...    ... Success!    - Removing privileges on test database...    ... Success!    Reloading the privilege tables will ensure that all changes made so far    will take effect immediately.    Reload privilege tables now? [Y/n]       → Enterキーを押します    ... Success!    Cleaning up...    All done! If you've completed all of the above steps, your MySQL    installation should now be secure.    Thanks for using MySQL! 


3.3 PHP 5.3へアップデート


PHPは2011年1月9日時点で5.3.5が最新版ですが、yumでインストールしたのは、5.1.6でした。 PHP 5.3へのアップデート、PEAR、mbstringのインストール、設定追加は後述します。


(1) PHP 5.3へのアップデート
yumにリポジトリを追加します。


   $ sudo rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi    $ sudo rpm --import http://download.fedora.redhat.com/pub/epel/RPM-GPG-KEY-EPEL




リポジトリ設定のインストール


   $ sudo rpm -ivh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm    $ sudo rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm




リポジトリを一時的に有効にして、php5.3をインストール


   $ sudo yum install --enablerepo remi php php-pear php-mbstring    Transaction Check Error:     file /etc/my.cnf from install of mysql-libs-5.1.51-1.el5.remi.x86_64 conflicts with file from package mysql-5.0.77-4.el5_5.3.x86_64     file /usr/share/mysql/charsets/Index.xml from install of mysql-libs-5.1.51-1.el5.remi.x86_64 conflicts with file from package mysql-5.0.77-4.el5_5.3.x86_64
         :
         :
         :
    file /usr/share/mysql/charsets/cp1250.xml from install of file /usr/share/mysql/ukrainian/errmsg.sys from install of mysql-libs-5.1.51-1.el5.remi.x86_64 conflicts with file from package mysql-5.0.77-4.el5_5.3.x86_64




以上、大量のコンフリクトが発生しました。 そこで、まずは、MySQLのほうを先にアップデートするのがよいらしいので、MySQLをアップデート。


   $ sudo yum --enablerepo=remi,epel update mysql*


これで、PHPまで合わせて最新の 5.3にアップデートできました。 念のためPEARのアップデート、mbstringがインストールされていないので、mbstringをインストール。


   $ sudo yum update --enablerepo remi php-pear
   $ sudo yum install --enablerepo remi php-mbstring


Apacheを再起動して、PHPの更新を反映します。


   $ sudo /sbin/service httpd restart


phpinfo()を呼び出すファイルを作成し、バージョン等を確認します。


0 コメント:

コメントを投稿