2014-07-21

Created page with "Category:Web Server (日本語) cs:LAMP de:LAMP Installation el:LAMP en:LAMP es:LAMP fr:Lamp it:LAMP pl:LAMP ru:LAMP sr:LAMP tr:L..."

New page

[[Category:Web Server (日本語)]]

[[cs:LAMP]]

[[de:LAMP Installation]]

[[el:LAMP]]

[[en:LAMP]]

[[es:LAMP]]

[[fr:Lamp]]

[[it:LAMP]]

[[pl:LAMP]]

[[ru:LAMP]]

[[sr:LAMP]]

[[tr:LAMP]]

[[zh-CN:LAMP]]

{{Related articles start (日本語)}}

{{Related2|MariaDB (日本語)|MariaDB}}

{{Related|PhpMyAdmin}}

{{Related|Adminer}}

{{Related|Xampp}}

{{Related|mod_perl}}

{{Related articles end}}

[[Wikipedia:ja:LAMP|LAMP]] は多くのウェブサーバーで使われているソフトウェアの共通の組み合わせを示しています: '''L'''inux, '''A'''pache, '''M'''ySQL/'''M'''ariaDB, '''P'''HP。この記事では Arch Linux 環境で [http://httpd.apache.org Apache HTTP Server] をセットアップする方法を記述しています。また、オプションとして Apache サーバーに [[PHP]] と [[MariaDB (日本語)|MariaDB]] をインストールして集約させる方法も載せています。

開発とテストのためのウェブサーバーが必要なだけの場合、[[Xampp]] がもっと簡単で良い選択肢になるでしょう。

== インストール ==

この文章では Apache, PHP, MariaDB をインストールすることを前提にしています。ただし必要に応じて Apache, PHP, MariaDB を別々にインストールして下の各セクションを参照することも可能です。

{{Pkg|apache}}, {{Pkg|php}}, {{Pkg|php-apache}}, {{Pkg|mariadb}} は[[公式リポジトリ]]から[[インストール]]できます。

== 設定 ==

=== Apache ===

セキュリティ上の理由で、root ユーザーによって Apache が (直接またはスタートアップスクリプトによって) 起動されるとすぐに {{ic|/etc/httpd/conf/httpd.conf}} で指定された UID/GID に切り替わるようになっています。デフォルトはユーザー '''http''' で、インストール中に自動で作成されます。

{{ic|httpd.conf}} や {{ic|extra/httpd-default.conf}} を任意で好きなように変更して [[systemd (日本語)#ユニットを使う]] を使って {{ic|httpd.service}} を起動してください。

これで Apache が実行されます。ウェブブラウザで http://localhost/ を開いてテストしてください。シンプルな Apache のテストページが表示されるはずです。

==== ユーザーディレクトリ ====

デフォルトではユーザーディレクトリは http://localhost/~yourusername/ から見れるようになっており {{ic|~/public_html}} の中身が表示されます (これは {{ic|/etc/httpd/conf/extra/httpd-userdir.conf}} で変更可能です)。

ウェブ上でユーザーディレクトリが見れないようにしたい場合は、{{ic|/etc/httpd/conf/httpd.conf}} の以下の行をコメントアウトしてください:

Include conf/extra/httpd-userdir.conf

Apache が取得できるようにホームディレクトリのパーミッションが正しく設定されているか確認する必要があります。ホームディレクトリと {{ic|~/public_html/}} は他者 ("rest of the world") にも実行可能でなければなりません。以下のコマンドでパーミッションを変更できます:

$ chmod o+x ~

$ chmod o+x ~/public_html

変更を適用するために {{ic|httpd.service}} を再起動して下さい。

==== SSL ====

SSL を使うには、{{pkg|openssl}} をインストールする必要があります。

自己署名証明書を作成 (キーのサイズや効力の日数は変更できます):

# cd /etc/httpd/conf

# openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out server.key

# chmod 600 server.key

# openssl req -new -key server.key -out server.csr

# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

そして {{ic|/etc/httpd/conf/httpd.conf}} の、以下の3行をアンコメント:

LoadModule ssl_module modules/mod_ssl.so

LoadModule socache_shmcb_module modules/mod_socache_shmcb.so

Include conf/extra/httpd-ssl.conf

{{ic|httpd.service}} を再起動して変更を適用してください。

==== バーチャルホスト ====

{{Note|バーチャルホストの SSL サポートのために別に <VirtualHost dommainame:443> セクションを追加する必要があります。[[#多数のバーチャルホストの管理]] のサンプルファイルを見て下さい。}}

複数のホストを使いたい場合は、{{ic|/etc/httpd/conf/httpd.conf}} の次の行をアンコメントしてください:

Include conf/extra/httpd-vhosts.conf

{{ic|/etc/httpd/conf/extra/httpd-vhosts.conf}} でバーチャルホストを設定します。デフォルトのファイルには初めて設定するときに役立つ詳細な説明の入ったサンプルが含まれています。

ローカルマシンでバーチャルホストをテストするには、バーチャルネームを {{ic|/etc/hosts}} ファイルに追加します:

127.0.0.1 domainname1.dom

127.0.0.1 domainname2.dom

{{ic|httpd.service}} を再起動して変更を適用してください。

===== 多数のバーチャルホストの管理 =====

If you have a huge amount of virtual hosts, you may want to easily disable and enable them. It is recommended to create one configuration file per virtual host and store them all in one folder, eg: {{ic|/etc/httpd/conf/vhosts}}.

First create the folder:

# mkdir /etc/httpd/conf/vhosts

Then place the single configuration files in it:

# nano /etc/httpd/conf/vhosts/domainname1.dom

# nano /etc/httpd/conf/vhosts/domainname2.dom

...

In the last step, {{ic|Include}} the single configurations in your {{ic|/etc/httpd/conf/httpd.conf}}:

#Enabled Vhosts:

Include conf/vhosts/domainname1.dom

Include conf/vhosts/domainname2.dom

You can enable and disable single virtual hosts by commenting or uncommenting them.

A very basic vhost file will look like this:

{{hc|/etc/httpd/conf/vhosts/domainname1.dom|<nowiki>

<VirtualHost domainname1.dom:80>

ServerAdmin webmaster@domainname1.dom

DocumentRoot "/home/user/http/domainname1.dom"

ServerName domainname1.dom

ServerAlias domainname1.dom

ErrorLog "/var/log/httpd/domainname1.dom-error_log"

CustomLog "/var/log/httpd/domainname1.dom-access_log" common

<Directory "/home/user/http/domainname1.dom">

Options Indexes FollowSymLinks

AllowOverride None

Require all granted

</Directory>

<Directory "/home/user/http/domainname1.dom/cgi-bin">

AllowOverride None

Options None

Require all granted

</Directory>

</VirtualHost>

<VirtualHost domainname1.dom:443>

ServerAdmin webmaster@domainname1.dom

DocumentRoot "/home/user/http/domainname1.dom"

ServerName domainname1.dom:443

ServerAlias domainname1.dom:443

ErrorLog "/var/log/httpd/domainname1.dom-error_log"

CustomLog "/var/log/httpd/domainname1.dom-access_log" common

<Directory "/home/user/http/domainname1.dom">

Options Indexes FollowSymLinks

AllowOverride None

Require all granted

</Directory>

<Directory "/home/user/http/domainname1.dom/cgi-bin">

AllowOverride None

Options None

Require all granted

</Directory>

SSLEngine on

SSLCertificateFile "/etc/httpd/conf/server.crt"

SSLCertificateKeyFile "/etc/httpd/conf/server.key"

<FilesMatch "\.(cgi|shtml|phtml|php)$">

SSLOptions +StdEnvVars

</FilesMatch>

<Directory "/srv/http/cgi-bin">

SSLOptions +StdEnvVars

</Directory>

BrowserMatch "MSIE [2-5]" \

nokeepalive ssl-unclean-shutdown \

downgrade-1.0 force-response-1.0

CustomLog "/var/log/httpd/ssl_request_log" \

"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"

</VirtualHost></nowiki>}}

==== 高度なオプション ====

These options in {{ic|/etc/httpd/conf/httpd.conf}} might be interesting for you:

Listen 80

:This is the port Apache will listen to. For Internet-access with router, you have to forward the port.

:If you want to setup Apache for local development you may want it to be only accessible from your computer. Then change this line to {{ic|Listen 127.0.0.1:80}}.

ServerAdmin you@example.com

:This is the admin's email address which can be found on e.g. error pages.

DocumentRoot "/srv/http"

:This is the directory where you should put your web pages.

:Change it, if you want to, but do not forget to also change {{ic|<Directory "/srv/http">}} to whatever you changed your {{ic|DocumentRoot}} to, or you will likely get a '''403 Error''' (lack of privileges) when you try to access the new document root. Do not forget to change the {{ic|Require all denied}} line, otherwise you will get a '''403 Error'''.

AllowOverride None

:This directive in {{ic|<Directory>}} sections causes Apache to completely ignore {{ic|.htaccess}} files. If you intend to use {{ic|mod_rewrite}} or other settings in {{ic|.htaccess}} files, you can allow which directives declared in that file can override server configuration. For more info refer to the [http://httpd.apache.org/docs/current/mod/core.html#allowoverride Apache documentation].

{{Tip|If you have issues with your configuration you can have Apache check the configuration with: {{ic|apachectl configtest}}}}

More settings can be found in {{ic|/etc/httpd/conf/extra/httpd-default.conf}}:

To turn off your server's signature:

ServerSignature Off

To hide server information like Apache and PHP versions:

ServerTokens Prod

==== トラブルシューティング ====

* '''Apache の状態とログ'''

: Apache デーモンの状態を表示: {{ic|systemctl status httpd}}

: Apache のログがある場所は: {{ic|/var/log/httpd/}}

* '''Error: PID file /run/httpd/httpd.pid not readable (yet?) after start.'''

: unique_id_module をコメントアウトしてください: {{ic|#LoadModule unique_id_module modules/mod_unique_id.so}}

* '''Apache を 2.2 から 2.4 にアップグレード'''

# If you use {{ic|php-apache}}, follow the introductory note to [[#PHP|Apache with PHP]] below.

# Access Control has changed. Convert all {{ic|Order}}, {{ic|Allow}}, {{ic|Deny}} and {{ic|Satisfy}} directives to the new {{ic|Require}} syntax.

#:[http://httpd.apache.org/docs/2.4/mod/mod_access_compat.html mod_access_compat] allows you to use the deprecated format during a transition phase.

# More information: [http://httpd.apache.org/docs/2.4/upgrading.html Upgrading to 2.4 from 2.2]

=== PHP ===

{{Note|1={{pkg|php-apache}} に含まれている {{ic|libphp5.so}} は {{ic|mod_mpm_event}} で動作しません ({{bug|39218}})。代わりに {{ic|mod_mpm_prefork}} を使って下さい。そうしないと以下のエラーが表示されます:

{{bc|1=Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe. You need to recompile PHP.

AH00013: Pre-configuration failed

httpd.service: control process exited, code=exited status=1}}

{{ic|mod_mpm_prefork}} を使うには、{{ic|/etc/httpd/conf/httpd.conf}} を開いて次の行を:

{{bc|LoadModule mpm_event_module modules/mod_mpm_event.so}}

以下のように置き換えてください:

{{bc|LoadModule mpm_prefork_module modules/mod_mpm_prefork.so}}

他の方法として、{{ic|mod_proxy_handler}} を使うこともできます (下の [[#php-fpm, mod_proxy_fcgi, mod_proxy_handler で php5 を使う]] を参照)。

}}

PHP を有効にするには、以下の行を {{ic|/etc/httpd/conf/httpd.conf}} に追加してください:

*次の行を {{ic|LoadModule}} リストの {{ic|LoadModule dir_module modules/mod_dir.so}} の後のどこかに記述:

LoadModule php5_module modules/libphp5.so

*次の行を {{ic|Include}} リストの最後に記述:

Include conf/extra/php5_module.conf

{{ic|DocumentRoot}} が {{ic|/srv/http}} ではない場合、次のように {{ic|/etc/php/php.ini}} の {{ic|open_basedir}} に追加してください:

open_basedir=/srv/http/:/home/:/tmp/:/usr/share/pear/:/path/to/documentroot

[[systemd (日本語)#ユニットを使う|systemd]] を使って {{ic|httpd.service}} を再起動して下さい。

PHP が正しく設定されたかどうかテストするには: Apache の {{ic|DocumentRoot}} ディレクトリ (例: {{ic|/srv/http/}} または {{ic|~/public_html}}) に以下の内容で {{ic|test.php}} という名前のファイルを作成します:

<?php phpinfo(); ?>

動作するか確かめるために次のページを見て下さい: http://localhost/test.php or http://localhost/~myname/test.php

高度な設定や拡張については、[[PHP]] を読んで下さい。

==== php-fpm, mod_proxy_fcgi, mod_proxy_handler で php5 を使う ====

{{Note|Unlike the widespread setup with ProxyPass, the proxy configuration with mod_proxy_handler and SetHandler respects other Apache directives like DirectoryIndex. This ensures a better compatibility with software designed for libphp5, mod_fastcgi and mod_fcgid.

If you still want to try ProxyPass, experiment with a line like this: {{bc|ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/srv/http/$1}}}}

* {{pkg|php-fpm}} と {{AUR|mod_proxy_handler}} をインストール

* {{ic|/etc/php/php-fpm.conf}} で {{ic|listen}} を以下のように設定:

; use ip/port instead of unix socket, mod_proxy_fcgi does not support it

listen = 127.0.0.1:9000

;listen = /run/php-fpm/php-fpm.sock

listen.allowed_clients = 127.0.0.1

* 以下を {{ic|/etc/httpd/conf/httpd.conf}} に追記:

LoadModule proxy_handler_module modules/mod_proxy_handler.so

<FilesMatch \.php$>

SetHandler "proxy:fcgi://127.0.0.1:9000/"

</FilesMatch>

<IfModule dir_module>

DirectoryIndex index.php index.html

</IfModule>

* 前に php モジュールを追加していた場合は、必要なくなるので削除:

LoadModule php5_module modules/libphp5.so

* {{ic|httpd}} と {{ic|php-fpm}} デーモンを[[Systemd (日本語)#ユニットを使う|再起動]]:

# systemctl restart httpd.service php-fpm.service

==== apache2-mpm-worker, mod_fcgid で php5 を使う ====

* {{ic|/etc/conf.d/apache}} の次の行をアンコメント:

HTTPD=/usr/bin/httpd.worker

* {{ic|/etc/httpd/conf/httpd.conf}} の次の行をアンコメント:

Include conf/extra/httpd-mpm.conf

* [[公式リポジトリ]]から {{pkg|mod_fcgid}} と {{Pkg|php-cgi}} パッケージを[[インストール]]。

* {{ic|/etc/httpd/conf/extra/php5_fcgid.conf}} を以下の内容で作成:

{{hc|/etc/httpd/conf/extra/php5_fcgid.conf|<nowiki>

# Required modules: fcgid_module

<IfModule fcgid_module>

AddHandler php-fcgid .php

AddType application/x-httpd-php .php

Action php-fcgid /fcgid-bin/php-fcgid-wrapper

ScriptAlias /fcgid-bin/ /srv/http/fcgid-bin/

SocketPath /var/run/httpd/fcgidsock

SharememPath /var/run/httpd/fcgid_shm

# If you don't allow bigger requests many applications may fail (such as WordPress login)

FcgidMaxRequestLen 536870912

# Path to php.ini – defaults to /etc/phpX/cgi

DefaultInitEnv PHPRC=/etc/php/

# Number of PHP childs that will be launched. Leave undefined to let PHP decide.

#DefaultInitEnv PHP_FCGI_CHILDREN 3

# Maximum requests before a process is stopped and a new one is launched

#DefaultInitEnv PHP_FCGI_MAX_REQUESTS 5000

<Location /fcgid-bin/>

SetHandler fcgid-script

Options +ExecCGI

</Location>

</IfModule></nowiki>

}}

* 必要なディレクトリを作成し PHP ラッパーにシンボリックリンクを作成:

# mkdir /srv/http/fcgid-bin

# ln -s /usr/bin/php-cgi /srv/http/fcgid-bin/php-fcgid-wrapper

* {{ic|/etc/httpd/conf/httpd.conf}} を編集:

#LoadModule php5_module modules/libphp5.so

LoadModule fcgid_module modules/mod_fcgid.so

Include conf/extra/php5_fcgid.conf

そして {{ic|httpd}} を[[Daemons (日本語)|再起動]]。

{{Note|1=As of Apache 2.4 you can now use [http://httpd.apache.org/docs/2.4/mod/mod_proxy_fcgi.html mod_proxy_fcgi] (part of the official distribution) with PHP-FPM (and the new event MPM). See this [http://wiki.apache.org/httpd/PHP-FPM configuration example].}}

=== MariaDB ===

[[MariaDB (日本語)|MariaDB]] で説明されているように MySQL/MariaDB を設定してください。

{{ic|/etc/php/php.ini}} の以下の行の [http://www.php.net/manual/ja/mysqlinfo.api.choosing.php どちらか一行] をアンコメント:

extension=pdo_mysql.so

extension=mysqli.so

{{Warning|PHP 5.5 から、{{ic|mysql.so}} は [http://php.net/manual/ja/migration55.deprecated.php 非推奨] になり、ログにエラーが出力されるようになっています。}}

ウェブスクリプトのために権限を抑えた MySQL ユーザーを追加できます。また、{{ic|/etc/mysql/my.cnf}} を編集して {{ic|skip-networking}} 行をアンコメントすることで MySQL サーバーがローカルホストからしかアクセスできないようにすることも可能です。変更を適用するには MySQL を再起動する必要があります。

[[systemd (日本語)#ユニットを使う|systemd]] を使って {{ic|httpd.service}} を再起動します。

{{Tip|データベースを利用するために [[phpMyAdmin]], [[Adminer]], {{AUR|mysql-workbench}} などのツールをインストールしても良いでしょう。}}

== 外部リンク ==

* [http://www.apache.org/ Apache 公式ウェブサイト]

* [http://www.php.net/ PHP 公式ウェブサイト]

* [https://mariadb.org/ MariaDB 公式ウェブサイト]

* [http://www.akadia.com/services/ssh_test_certificate.html 自己署名証明書の作成チュートリアル]

* [http://wiki.apache.org/httpd/CommonMisconfigurations Apache Wiki トラブルシューティング]

Show more