TOP » Ubuntu のインストール » サーバーソフトウェアのインストール » LAMP/LAPP システムで Web アプリケーションサーバー構築

LAMP/LAPP システムで Web アプリケーションサーバー構築

LAMP とは Linux OS 上で Apache と MySQL、PHP のシステムの組み合わせたものをいい、LAPP とは上記の MySQL を PostgreSQL と置き換えたものをいいます。
PHP とは Hypertext Preprocessor の略で、Web サーバを拡張して動的にウェブページを生成することができるサーバーサイドスクリプト言語のことをいいます。
PHP 自体は C 言語で記述されています。
PHP は Apache HTTP Server(Web サーバー)上で動作してサーバー上のファイルが要求されるたびに PHP のプログラムを実行しその結果をウェブブラウザに送信します。

PHP のインストール

まずはじめに PHP をインストールしましょう。
PHP は apt-get を使ってインストールします。
以下のように root 権限でインストールを行います。

$ sudo apt-get install php5

Reading package lists… Done
Building dependency tree
Reading state information… Done
The following extra packages will be installed:
  apache2-mpm-prefork libapache2-mod-php5 libxml2 php5-common ucf
Suggested packages:
  php-pear
Recommended packages:
  xml-core debconf-utils
The following packages will be REMOVED:
  apache2-mpm-worker
The following NEW packages will be installed:
  apache2-mpm-prefork libapache2-mod-php5 libxml2 php5 php5-common ucf
0 upgraded, 6 newly installed, 1 to remove and 1 not upgraded.
Need to get 4002kB of archives.
After unpacking 8040kB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://jp.archive.ubuntu.com feisty/main libxml2 2.6.27.dfsg-1ubuntu3 [761kB]
Get:2 http://security.ubuntu.com feisty-security/main apache2-mpm-prefork 2.2.3-3.2ubuntu0.1 [429kB]
Get:3 http://jp.archive.ubuntu.com feisty/main ucf 2.0017ubuntu1 [57.0kB]
Get:4 http://security.ubuntu.com feisty-security/main php5-common 5.2.1-0ubuntu1.4 [223kB]
Get:5 http://security.ubuntu.com feisty-security/main libapache2-mod-php5 5.2.1-0ubuntu1.4 [2531kB]
Get:6 http://security.ubuntu.com feisty-security/main php5 5.2.1-0ubuntu1.4 [1078B]
Fetched 4002kB in 31s (125kB/s)
Preconfiguring packages …
dpkg: apache2-mpm-worker: dependency problems, but removing anyway as you request:
 apache2 depends on apache2-mpm-worker (>= 2.2.3-3.2ubuntu0.1) | apache2-mpm-prefork (>= 2.2.3-3.2ubuntu0.1) | apache2-mpm-event (>= 2.2.3-3.2ubuntu0.1); however:
  Package apache2-mpm-worker is to be removed.
  Package apache2-mpm-prefork is not installed.
  Package apache2-mpm-event is not installed.
(Reading database … 19455 files and directories currently installed.)
Removing apache2-mpm-worker …
 * Stopping web server (apache2)…                                      [ OK ]
Selecting previously deselected package apache2-mpm-prefork.
(Reading database … 19450 files and directories currently installed.)
Unpacking apache2-mpm-prefork (from …/apache2-mpm-prefork_2.2.3-3.2ubuntu0.1_i386.deb) …
Selecting previously deselected package libxml2.
Unpacking libxml2 (from …/libxml2_2.6.27.dfsg-1ubuntu3_i386.deb) …
Selecting previously deselected package php5-common.
Unpacking php5-common (from …/php5-common_5.2.1-0ubuntu1.4_i386.deb) …
Selecting previously deselected package ucf.
Unpacking ucf (from …/ucf_2.0017ubuntu1_all.deb) …
Moving old data out of the way
Selecting previously deselected package libapache2-mod-php5.
Unpacking libapache2-mod-php5 (from …/libapache2-mod-php5_5.2.1-0ubuntu1.4_i386.deb) …
Selecting previously deselected package php5.
Unpacking php5 (from …/php5_5.2.1-0ubuntu1.4_all.deb) …
Setting up apache2-mpm-prefork (2.2.3-3.2ubuntu0.1) …
 * Starting web server (apache2)…                                      [ OK ]

Setting up libxml2 (2.6.27.dfsg-1ubuntu3) …

Setting up php5-common (5.2.1-0ubuntu1.4) …
Setting up ucf (2.0017ubuntu1) …

Setting up libapache2-mod-php5 (5.2.1-0ubuntu1.4) …

Creating config file /etc/php5/apache2/php.ini with new version
 * Forcing reload of web server (apache2)…                             [ OK ]

Setting up php5 (5.2.1-0ubuntu1.4) …

MySQL/PostgreSQL との連携

すでにインストールされている MySQL、PostgreSQL との連携を行うためのモジュールをapt-get を使ってインストールします。
以下のように root 権限でインストールを行います。

$ sudo apt-get install php5-mysql php5-pgsql

Reading package lists… Done
Building dependency tree
Reading state information… Done
The following NEW packages will be installed:
  php5-mysql php5-pgsql
0 upgraded, 2 newly installed, 0 to remove and 1 not upgraded.
Need to get 117kB of archives.
After unpacking 438kB of additional disk space will be used.
Get:1 http://security.ubuntu.com feisty-security/main php5-mysql 5.2.1-0ubuntu1.4 [65.5kB]
Get:2 http://security.ubuntu.com feisty-security/main php5-pgsql 5.2.1-0ubuntu1.4 [51.3kB]
Fetched 117kB in 2s (44.3kB/s)
Selecting previously deselected package php5-mysql.
(Reading database … 19528 files and directories currently installed.)
Unpacking php5-mysql (from …/php5-mysql_5.2.1-0ubuntu1.4_i386.deb) …
Selecting previously deselected package php5-pgsql.
Unpacking php5-pgsql (from …/php5-pgsql_5.2.1-0ubuntu1.4_i386.deb) …
Setting up php5-mysql (5.2.1-0ubuntu1.4) …

Setting up php5-pgsql (5.2.1-0ubuntu1.4) …

LAMP/LAPP の動作確認

MySQL と PostgreSQL との連携を有効にするために Apache HTTP Server を再起動します。

$ sudo /etc/init.d/apache2 restart

 * Forcing reload of web server (apache2)…
                                                                         [ OK ]

再起動が終わると LAMP/LAPP 環境が正しく構築できているかを確認してみましょう。
はじめに公開ディレクトリに以下のような PHP ファイルを作成します。

$ sudo vi /var/ubuntud.com/public_html/info.php
/var/ubuntud.com/public_html/info.php
<?php phpinfo(); ?>

ファイルを作成したらブラウザを使ってアクセスしてみましょう。
http://192.168.0.2/info.php
以下のように PHP の実行環境などが示されたページが表示されます。

PHP の MySQL 環境
PHP の PostgreSQL 環境