2013-03-13 11:30 by 仁伯爵

これまでVirtualBoxでさくらインターネット用とカゴヤサーバー用の検証環境を作る試みを行ってきたわけだが、ここいらで仕切りなおしてVirtualBOXにCentOSをネットワークインストールして、nginx + PHP + PostgreSQLの環境を整える備忘録を書いておこうと思う。先に作った環境ではnginx + PHP + MySQLだったのだが、一通りいじくって満足してしまったのと、最近の流れではPostgreSQLの方へシフトする動きが流行りだというので、仕切りなおしてそっちを書くことにした。

1.CentOSのダウンロード

CentOSの6.4の64bitバージョンを使いたいと思う。
ここ(http://isoredirect.centos.org/centos/6/isos/x86_64/)から好きなサーバーを選んで、CentOS-6.4-x86_64-netinstall.isoをダウンロードする。今回ネットワークインストールを選んだのは、フルのインストールDVDを選ぶと4.1GBもあり、ダウンロードがとてもしんどいのと、virtualBoxで4.1Gのisoをマウントしてブートさせようとしたらうまくいかなかったためである。CentOS-6.4-x86_64-netinstall.isoなら230MBそこそこなのでダウンロードもすぐ終わるし、よろしかろうと思ったからだ。

2.VirtualBox仮想サーバーの新規作成

Oracle VM VirtualBoxマネージャの[新規]ボタンをクリックし仮想マシンを作成する。名前は任意。タイプはLinux、バージョンはRedHat(64bit)とする。CentOSはRedHatの互換ディストリビューションなのでこれでよい。あとはデフォルトでいい。作成できたら、できたアイコンを選択した状態で上部の[設定]ボタンをクリックし、以下の設定を行う。

[ストレージ]→[コントローラIDE]の[空]を選択し、右の属性欄にあるCDのアイコンをクリックしさっきダウンロードしたCentOS-6.4-x86_64-netinstall.isoをマウントする。

[ネットワーク]→[アダプタ1]の[割り当て]で”NAT”を選択する。
[ネットワーク]→[アダプター2]の[ネットワークアダプタを有効化]にチェックを入れて、[割り当て]で”ブリッジアダプター”を選択する。

これで、ホストOSと同じネットワークに参加できる状態の仮想マシンが用意でき、光学ドライブにインストールCDが挿入されている状態が出来上がった。このまま仮想マシンを起動させるとインストールメディアからブートされてCentOSのインストールが始まる。

3.CentOSのインストール

ここからは普通にCentOSをネットワークインストールしていけばいい。注意すべき点は、3つ

  • 仮想マシンのグラフィックがショボいので綺麗なグラフィックは使用できないが気にしない
  • ネットワークインストールでは、インストールメディア選択の画面で”URL”を選択する
  • ネットワーク設定の項目では、2つ目のインターフェースにホストOSと同じセグメントに参加できる設定をする。

そのほかは通常のCentOSのインストールと変わらないのでさくっと入れてしまおう。

4.こまごまとした設定

インストール直後からSSHで接続できるようになっているので、お好みターミナルエミュレータでログインする。virtualboxの窓から操作してもいいが、マウスでコピペできなかったりして不便。設定する項目のメモ

  • /etc/hostsでほかのテストサーバや前解決させたいIPとホスト名を追加
  • /etc/sysconfig/network-scripts/ifcfg-ethnでインターフェース設定
  • /etc/sysconfig/networkでホスト名設定
  • コマンド”adduser”で一般ユーザー作成
  • コマンド”setenforce 0″でselinuxの無効化、コマンド”getenforce”で確認
  • /etc/sysconfig/iptablesでiptableの80ポート許可設定(-A INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT)

その後、ネットワークとiptableの再起動。

  • /etc/init.d/iptables restart
  • /etc/init.d/network restart

今回のCentOS6.4をネットワークインストールした状態では、yum-plugin-fastestmirrorがすでに入っていたので省く。

yum-cronをインストールする。


# yum -y install yum-cron
~省略~
Complete!
#

必要に応じてyum-cronの設定を編集 
# vi /etc/sysconfig/yum-cron

yum-cronを起動
# /etc/rc.d/init.d/yum-cron start
夜間 yum 更新の有効化中:              [  OK  ]
#
#

yum-cronの自動起動設定
# chkconfig yum-cron on

確認
# chkconfig --list yum-cron
yum-cron        0:off   1:off   2:on    3:on    4:on    5:on    6:off

#
#

wgetコマンドのインストール


# yum -y install wget
~省略~
Complete!
#

epelパッケージリポジトリのインストール
ナイスなサーバーからepelの最新版のrpmパッケージをダウンロードしてインストールしちゃう。


wgetコマンドでダウンロード
# wget http://ftp-srv2.kddilabs.jp/Linux/packages/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
--2013-03-12 19:45:06--  http://ftp-srv2.kddilabs.jp/Linux/packages/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
ftp-srv2.kddilabs.jp をDNSに問いあわせています... 202.255.47.226
ftp-srv2.kddilabs.jp|202.255.47.226|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 14540 (14K) [application/octet-stream]
`epel-release-6-8.noarch.rpm' に保存中

100%[=============================================================================================================>] 14,540      --.-K/s 時間 0.02s

2013-03-12 19:45:06 (605 KB/s) - `epel-release-6-8.noarch.rpm' へ保存完了 [14540/14540]

#
#

rpmコマンドでインストール
#rpm -Uvh epel-release-6-8.noarch.rpm
警告: epel-release-6-8.noarch.rpm: ヘッダ V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
準備中...                ########################################### [100%]
   1:epel-release           ########################################### [100%]
#
#

リポジトリの無効化(明示的に指定したときのみ使うようにする。)
# vi /etc/yum.repos.d/epel.repo
~省略~
enabled=0 ← 0(無効)にする。
~省略~

remiパッケージリポジトリのインストール
さらにremiの最新版のpmパッケージもダウンロードしてインストールしちゃう。


wgetコマンドでダウンロード
# wget http://rpms.famillecollet.com/el5.x86_64/remi-release-5-9.el5.remi.noarch.rpm
--2013-03-12 20:09:18--  http://rpms.famillecollet.com/el5.x86_64/remi-release-5-9.el5.remi.noarch.rpm
rpms.famillecollet.com をDNSに問いあわせています... 88.191.74.232
rpms.famillecollet.com|88.191.74.232|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 5180 (5.1K) [application/x-rpm]
`remi-release-5-9.el5.remi.noarch.rpm' に保存中

100%[=======================================================================================================================>] 5,180       20.1K/s 時間 0.3s

2013-03-12 20:09:19 (20.1 KB/s) - `remi-release-5-9.el5.remi.noarch.rpm' へ保存完了 [5180/5180]

#
#

rpmコマンドでインストール
# rpm -Uvh remi-release-5-9.el5.remi.noarch.rpm
警告: remi-release-5-9.el5.remi.noarch.rpm: ヘッダ V3 DSA/SHA1 Signature, key ID 00f97f56: NOKEY
準備中...                ########################################### [100%]
   1:remi-release           ########################################### [100%]

#
#

リポジトリの無効化(明示的に指定したときのみ使うようにする。)
# vi /etc/yum.repos.d/remi.repo
~省略~
enabled=0 ← 0(無効)にする。
~省略~

PosggreSQLパッケージリポジトリのインストール
さらにPosggreSQ本家のジリポジトリも追加しちゃう。


wgetコマンドでダウンロード
# wget http://yum.postgresql.org/9.2/redhat/rhel-6-x86_64/pgdg-centos92-9.2-6.noarch.rpm
--2013-03-12 21:28:55--  http://yum.postgresql.org/9.2/redhat/rhel-6-x86_64/pgdg-centos92-9.2-6.noarch.rpm
yum.postgresql.org をDNSに問いあわせています... 98.129.198.114
yum.postgresql.org|98.129.198.114|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 5288 (5.2K) [application/x-redhat-package-manager]
`pgdg-centos92-9.2-6.noarch.rpm' に保存中

100%[=======================================================================================================================>] 5,288       23.2K/s 時間 0.2s

2013-03-12 21:28:56 (23.2 KB/s) - `pgdg-centos92-9.2-6.noarch.rpm' へ保存完了 [5288/5288]


#
#

rpmコマンドでインストール
# rpm -Uvh pgdg-centos92-9.2-6.noarch.rpm
警告: pgdg-centos92-9.2-6.noarch.rpm: ヘッダ V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
準備中...                ########################################### [100%]
   1:pgdg-centos92          ########################################### [100%]
#
#

リポジトリの無効化(明示的に指定したときのみ使うようにする。)
# vi /etc/yum.repos.d/pgdg-92-centos.repo
~省略~
enabled=0 ← 0(無効)にする。
~省略~

nginxリポジトリの追加
epelつかってもnginxは古い。最新のを使いたいので、yumコマンドで新しいnginxをインストールできるようにする。


リポジトリファイルを以下の内容で作る。
# /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

ここで、なぜリポジトリを追加するのかの説明を兼ねて、リポジトリごとにどう変わるのか見てみる。

nginxの場合


# yum info nginx
Loaded plugins: downloadonly, fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: centosz3.centos.org
Error: No matching Packages to list  
#
#
標準のリポジトリにはnginxは含まれていない。


# yum --enablerepo=epel info nginx
Loaded plugins: downloadonly, fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * epel: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: centosz3.centos.org
Available Packages
Name        : nginx
Arch        : x86_64
Version     : 1.0.15
Release     : 3.el6
Size        : 379 k
Repo        : epel
Summary     : A high performance web server and reverse proxy server
URL         : http://nginx.org/
License     : BSD
Description : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
            : IMAP protocols, with a strong focus on high concurrency, performance and low
            : memory usage.

#
#
epelリポジトリでのバージョンは1.0.15


# yum --enablerepo=nginx info nginx
Loaded plugins: downloadonly, fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: centosz3.centos.org
Available Packages
Name        : nginx
Arch        : x86_64
Version     : 1.2.7
Release     : 1.el6.ngx
Size        : 364 k
Repo        : nginx
Summary     : high performance web server
URL         : http://nginx.org/
License     : 2-clause BSD-like license
Description : nginx [engine x] is a HTTP and reverse proxy server, as well as
            : a mail proxy server

#
#
nginxのリポジトリでは1.2.7である。さすがオフィシャル。最新である。

因みに、postgreSQLは全部同じ8.4.13で本家は9.2、phpはremiが5.4、ほかが5.3となっていた。外部リポジトリを使うと、最新バージョンのパッケージが使えたりするが、バージョンの違いによる不整合が発生してしまう場合もあり、自己の責任の範囲内においてパラダイスを味わおうということである。

5.PostgreSQLのインストール

PostgreSQLとPostgreSQL Serverをインストールする。


# yum --enablerepo=pgdg92 install postgresql92 postgresql92-server
Loaded plugins: downloadonly, fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: centosz3.centos.org
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package postgresql92.x86_64 0:9.2.3-2PGDG.rhel6 will be installed
--> Processing Dependency: postgresql92-libs = 9.2.3-2PGDG.rhel6 for package: postgresql92-9.2.3-2PGDG.rhel6.x86_64
--> Processing Dependency: libpq.so.5()(64bit) for package: postgresql92-9.2.3-2PGDG.rhel6.x86_64
---> Package postgresql92-server.x86_64 0:9.2.3-2PGDG.rhel6 will be installed
--> Running transaction check
---> Package postgresql92-libs.x86_64 0:9.2.3-2PGDG.rhel6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================
 Package                                      Arch                            Version                                      Repository                       Size
=================================================================================================================================================================
Installing:
 postgresql92                                 x86_64                          9.2.3-2PGDG.rhel6                            pgdg92                          968 k
 postgresql92-server                          x86_64                          9.2.3-2PGDG.rhel6                            pgdg92                          3.8 M
Installing for dependencies:
 postgresql92-libs                            x86_64                          9.2.3-2PGDG.rhel6                            pgdg92                          185 k

Transaction Summary
=================================================================================================================================================================
Install       3 Package(s)

Total download size: 4.9 M
Installed size: 21 M
Is this ok [y/N]: y
Downloading Packages:
(1/3): postgresql92-9.2.3-2PGDG.rhel6.x86_64.rpm                                                                                          | 968 kB     00:05
(2/3): postgresql92-libs-9.2.3-2PGDG.rhel6.x86_64.rpm                                                                                     | 185 kB     00:01
(3/3): postgresql92-server-9.2.3-2PGDG.rhel6.x86_64.rpm                                                                                   | 3.8 MB     00:27
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                            144 kB/s | 4.9 MB     00:35
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
  Installing : postgresql92-libs-9.2.3-2PGDG.rhel6.x86_64                                                                                                    1/3
  Installing : postgresql92-9.2.3-2PGDG.rhel6.x86_64                                                                                                         2/3
  Installing : postgresql92-server-9.2.3-2PGDG.rhel6.x86_64                                                                                                  3/3
  Verifying  : postgresql92-server-9.2.3-2PGDG.rhel6.x86_64                                                                                                  1/3
  Verifying  : postgresql92-9.2.3-2PGDG.rhel6.x86_64                                                                                                         2/3
  Verifying  : postgresql92-libs-9.2.3-2PGDG.rhel6.x86_64                                                                                                    3/3

Installed:
  postgresql92.x86_64 0:9.2.3-2PGDG.rhel6                                     postgresql92-server.x86_64 0:9.2.3-2PGDG.rhel6

Dependency Installed:
  postgresql92-libs.x86_64 0:9.2.3-2PGDG.rhel6

Complete!

#
#

インストール後の作業


バージョン確認
# psql --version
psql (PostgreSQL) 9.2.3
#

データベース初期化
# service postgresql-9.2 initdb
データベースを初期化中:                                    [  OK  ]
#
#

起動!
# /etc/init.d/postgresql-9.2 start
postgresql-9.2 サービスを開始中:                           [  OK  ]
#
#

自動起動!
# chkconfig postgresql-9.2 on

自動起動設定確認!
# chkconfig --list postgresql-9.2
postgresql-9.2  0:off   1:off   2:on    3:on    4:on    5:on    6:off
#
#

6.PHPのインストール

PHPはremiリポジトリから5.4をインストールする。本当は5.5が良かったのだが今回はお預けである。なんだか必要そうなものは片っ端から入れてみる。


# yum --enablerepo=remi install php php-cli php-pgsql php-pear-MDB2-Driver-pgsql php-xml php-mbstring php-devel

 ~ 省略 ~ 

Installed:
  php.x86_64 0:5.4.12-1.el6.remi                  php-cli.x86_64 0:5.4.12-1.el6.remi                                php-devel.x86_64 0:5.4.12-1.el6.remi
  php-mbstring.x86_64 0:5.4.12-1.el6.remi         php-pear-MDB2-Driver-pgsql.noarch 0:1.5.0-0.7.b4.el6.remi         php-pgsql.x86_64 0:5.4.12-1.el6.remi
  php-xml.x86_64 0:5.4.12-1.el6.remi

Dependency Installed:
  apr.x86_64 0:1.3.9-5.el6_2                         apr-util.x86_64 0:1.3.9-3.el6_0.1                     apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1
  autoconf.noarch 0:2.63-5.1.el6                     automake.noarch 0:1.11.1-4.el6                        httpd.x86_64 0:2.2.15-26.el6.centos
  httpd-tools.x86_64 0:2.2.15-26.el6.centos          libedit.x86_64 0:2.11-4.20080712cvs.1.el6             libxslt.x86_64 0:1.1.26-2.el6_3.1
  mailcap.noarch 0:2.1.31-2.el6                      perl.x86_64 4:5.10.1-129.el6                          perl-Module-Pluggable.x86_64 1:3.90-129.el6
  perl-Pod-Escapes.x86_64 1:1.04-129.el6             perl-Pod-Simple.x86_64 1:3.13-129.el6                 perl-libs.x86_64 4:5.10.1-129.el6
  perl-version.x86_64 3:0.77-129.el6                 php-common.x86_64 0:5.4.12-1.el6.remi                 php-pdo.x86_64 0:5.4.12-1.el6.remi
  php-pear.noarch 1:1.9.4-12.el6.remi.1              php-pear-MDB2.noarch 0:2.5.0-0.8.b5.el6.remi

Complete!
#
#
#

なんだかたくさん依存関係でインストールされてしまった。つづいてremiリポジトリからアクセラレータ(一方通行さんではない)をインストールする。中間コードをキャッシュしてPHPの実行を軽くしてくれる。安いサーバーでWeb閲覧者の体感速度を上げるにはもってこいの代物である。


# yum --enablerepo=remi install php-xcache
Loaded plugins: downloadonly, fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * remi: mirror.awanti.com
 * updates: centosz3.centos.org
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package php-xcache.x86_64 0:3.0.1-1.el6.remi will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================
 Package                                Arch                               Version                                        Repository                        Size
=================================================================================================================================================================
Installing:
 php-xcache                             x86_64                             3.0.1-1.el6.remi                               remi                              95 k

Transaction Summary
=================================================================================================================================================================
Install       1 Package(s)

Total download size: 95 k
Installed size: 340 k
Is this ok [y/N]: y
Downloading Packages:
php-xcache-3.0.1-1.el6.remi.x86_64.rpm                                                                                                    |  95 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : php-xcache-3.0.1-1.el6.remi.x86_64                                                                                                            1/1
  Verifying  : php-xcache-3.0.1-1.el6.remi.x86_64                                                                                                            1/1

Installed:
  php-xcache.x86_64 0:3.0.1-1.el6.remi

Complete!

つづいてFast-CGIのプロセスマネージャーを入れてみる。


# yum --enablerepo=remi install php-fpm
Loaded plugins: downloadonly, fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * remi: mirror.awanti.com
 * updates: centosz3.centos.org
base                                                                                                                                      | 3.7 kB     00:00
extras                                                                                                                                    | 3.5 kB     00:00
updates                                                                                                                                   | 3.5 kB     00:00
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package php-fpm.x86_64 0:5.4.12-1.el6.remi will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================
 Package                              Arch                                Version                                        Repository                         Size
=================================================================================================================================================================
Installing:
 php-fpm                              x86_64                              5.4.12-1.el6.remi                              remi                              1.3 M

Transaction Summary
=================================================================================================================================================================
Install       1 Package(s)

Total download size: 1.3 M
Installed size: 3.9 M
Is this ok [y/N]: y
Downloading Packages:
php-fpm-5.4.12-1.el6.remi.x86_64.rpm                                                                                                      | 1.3 MB     00:05
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : php-fpm-5.4.12-1.el6.remi.x86_64                                                                                                              1/1
  Verifying  : php-fpm-5.4.12-1.el6.remi.x86_64                                                                                                              1/1

Installed:
  php-fpm.x86_64 0:5.4.12-1.el6.remi

Complete!
#

起動!
# /etc/init.d/php-fpm start
php-fpm を起動中:                                          [  OK  ]
#

自動起動!
# chkconfig php-fpm on
#

自動起動設定確認!
# chkconfig --list php-fpm
php-fpm         0:off   1:off   2:on    3:on    4:on    5:on    6:of
#
#

PHP関連はやりたいことによってあとから追加するかもしれないがとりあえず、このくらいでいいだろう。

7.nginxのインストール

いよいよnginxさんのインストールである。貧弱なサーバでも驚くべきパフォーマンスを発揮するというWebサーバ界隈では噂のあいつである。
*追記・・・nginxはApacheの様にあとからモジュールを読み込ませることはできない。モジュールの追加はコンパイル時に行っておく必要がある。色々モジュールを有効にしたnginxをインストールする場合はこちらの記事を参照のこと→「VirtualBoxで作るCentOS+nginxの検証環境Webサーバにngx_cache_purgeモジュールを追加する。


# yum --enablerepo=nginx install nginx
Loaded plugins: downloadonly, fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.riken.jp
 * extras: ftp.riken.jp
 * updates: centosz3.centos.org
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 0:1.2.7-1.el6.ngx will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================
 Package                            Arch                                Version                                         Repository                          Size
=================================================================================================================================================================
Installing:
 nginx                              x86_64                              1.2.7-1.el6.ngx                                 nginx                              364 k

Transaction Summary
=================================================================================================================================================================
Install       1 Package(s)

Total download size: 364 k
Installed size: 842 k
Is this ok [y/N]: y
Downloading Packages:
nginx-1.2.7-1.el6.ngx.x86_64.rpm                                                                                                          | 364 kB     00:05
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : nginx-1.2.7-1.el6.ngx.x86_64                                                                                                                  1/1
----------------------------------------------------------------------

Thanks for using NGINX!

Check out our community web site:
* http://nginx.org/en/support.html

If you have questions about commercial support for NGINX please visit:
* http://www.nginx.com/support.html

----------------------------------------------------------------------
  Verifying  : nginx-1.2.7-1.el6.ngx.x86_64                                                                                                                  1/1

Installed:
  nginx.x86_64 0:1.2.7-1.el6.ngx

Complete!
#

起動!
# /etc/init.d/nginx start
nginx を起動中:                                            [  OK  ]
#        

自動起動!
# chkconfig nginx on

自動起動設定確認!
# chkconfig --list nginx
nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off
#

これで、ホストOSブラウザからhttp://仮想サーバーのIPアドレス/とアクセスしてみると、nginxさんがいらっしゃーいと言ってくれるはずだ。こんな感じで↓

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.

やったぜ!

8.差し当たっての設定

まずは、php-fpmの実行ユーザをnginxに変更しておく。apacheさんにサヨナラするのだ。


# vi /etc/php-fpm.d/www.conf

~省略~
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
~省略~

続いて、nginxのとりあえずの設定をしておく。/var/www/htmlをドキュメントルートとして公開してみる。/etc/nginx/nginx.confが設定ファイルだがデフォルトの設定では/etc/nginx/conf.dに*.confという名前でファイルを置いとけば読み込むように記述されているので、公開するサイトごとに設定ファイルを作ればいいだろう。



# vi /etc/nginx/conf.d/test.conf
    server {
        listen       80;
        server_name  サーバ名;

        location / {
            root   /var/www/html;
            index  index.html index.htm index.php;
        }

        error_page  404              /404.html;
        location = /404.html {
            root   /usr/share/nginx/html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

#
#

設定ファイルの構文チェック
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
#
#

設定ファイルの読み込み
# nginx -s reload
#

この設定でとりあえずは、/var/www/htmlディレクトリ以下がwebサーバの公開ディレクトリになった。しれっとfast-cgiなんかも使えるようになっちゃってたりする。

さらに、PHPで日本語が文字化けしないように、デフォルトキャラクターセットをUTF-8にしちゃう。


# vi /etc/php.ini

~省略~
; PHP's default character set is set to empty.
; http://php.net/default-charset
default_charset = "UTF-8"

~省略~

これで、PHP内に日本語を書くときはUTF-8で書いとけば文字化けしないようになった。もうこれからはDBでも何でもかんでもUTF8に統一しとけばいい。

では、これで本当に動くのかどうかテストする。/var/www/htmlにtest.phpを置いて表示できるかチェックする。


# vi /var/www/html/test.php

<?php
  echo "<h1>テストだす。</h1>";
  phpinfo();
?>
#
# chown nginx.nginx test.php
#

テストPHPファイルを作ってオーナーをnginxに変更したらこれでアクセスできるはずだ。ブラウザから、http://仮想サーバ/test.php とすれば、phpの情報がずらーっと出てくるはずだ。”テストだす”も文字化けせずに出ていればいいね。

さらに細かな設定は各々環境ごとに合わせていくとして差し当たり動くようになったということで一区切りつけたいと思う。最後まで読んでいただいた心優しく聡明な貴兄らならばお気づきのことだろうが、じつはこの手順でリポジトリepelは追加しただけで一度も使われていないのだ!追加しなくてもよかった!!当初eAcceralatorをepelからインストールしようと思っていたのだが、remiのPHP5.4ではだめだとyumさんに怒られたので、結局remiのXCacheを使うことにしたのでepelの出番がなくなったのだ!何はともあれ、無事にテスト環境WEBサーバーが完成した。これで思う存分色々実験したりPHPの練習をしたり、HTML5の実験をしたりできる。コピーすればストレージ容量の許す限り増殖させることも可能だ。一昔前ならハードを買わなければならなかったことがこんなに簡単に情報空間で完結してしまうなんて、とても気分がいい。全部タダなのだ。すごいことだ。感激しつつ、これでどんなWebサービスが作れるのか色々実験してみたいと思う。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です


*


*

投げ銭はこちらへ
bitcoin : 1AFU37YroGt8ohmFz8nG1N2ockL56Z4hfQ

ADA Coin: DdzFFzCqrhskq33AqGL8XkJZ3bb1hpxJYTd2UrJFKVpXphWG8d1RuhQrKymmKU1zzjvGi7oU69PaJ7nXECRG4Kpvg27Pghf3hpRNhRMy
2025年2月
 12
3456789
10111213141516
17181920212223
2425262728  
カテゴリー