【PHP】Hello PHP!

以前wordpress始めるときに少しはやったけどPCを忘れたのとPCを変えて、PHPの環境を新たに構築する必要が出たので改めてPHPを始める。

PHPやる理由としては、javaからサーバーのDBいじるためにRubyではダメなんじゃないかと思い始めたのも一つの理由です。今考えていることとして、javaでPOSTリクエストをサーバーのPHPに送る→PHP(Ruby)でDB操作する→結果を返す、という流れ。

PHPのローカルサーバーとしてApacheを使用します。すでにApacheをインストールしている人は、ApacheでPHP用にしなければならない設定があるのでそこだけ見れば良いと思います。

参考URL

PHPインストールと初期設定(Windows)

実行環境

Windows 10 (64bit)
Eclipse 4.6.0

PHPのDL・インストール

PHP_ Hypertext PreprocessorここからPHPをDLします。

右上の”Download”のリンクをたどります。私のレンタルしているサーバーではPHP5.6なので5.6をダウンロードします。

windowsなので”windows download”をクリックします。

64bit版なので”x64″のスレッドセーフをzipでDLします。

zipファイルを任意のディレクトリに解凍すればインストール完了です。私は「C:\php56-x64」に解凍しました。

PATHの設定

「Winキー+break」でシステムを表示します。

「システムの環境設定」をクリックします。

「環境変数」をクリックします。

システムの環境変数の「Path」をクリックして「編集」をクリックします。

「新規」をクリックして出てくるダイアログに、先程phpのzipファイルを解凍したディレクトリのパスを設定します。後はOK連打で出したダイアログを片付けます。

これでPATHの設定は完了です。

Apacheのインストール

参考URL:Apache入門

Apache VC14 binaries and modules downloadこのページからzipファイルをダウンロードします。現時点では最新版は2.4.23でした。

解凍して出てくる「Apache24」というファイルを任意のディレクトリに移してインストール完了となります。私はCドライブ直下に移動しました。ここでCドライブ直下に置くと後の設定が楽になります。

設定ファイルの修正

「C:\Apache24\conf\http.conf」ファイルをテキストエディタで開きます。

「ServerRoot “c:/Apache24″」で文字列検索して該当部分の””の中身をApacheのディレクトリに合わせます。私の場合はそのままでOKとなります。

次は「DocumentRoot “c:/Apache24/htdocs”」で文字列検索して該当部分、またその次の行の”C:/Apache24″の部分を自分でインストールしたApacheのディレクトリに合わせます。

また、「ScriptAlias /cgi-bin/ “c:/Apache24/cgi-bin/”」「<Directory “c:/Apache24/cgi-bin”>」で検索して該当部分を同じように変更します。

ポート番号の設定をします。58行目付近の「Listen 80」は任意で変更可能です。

218行目付近の「#ServerName www.example.com:80」は「ServerName localhost:80」に変更してコメントアウトを解除して有効にします。

Apacheの起動と終了

コマンドラインで「[Apacheルート]/bin/httpd.exe」を実行すると起動します。

コマンドラインでそのまま実行する方法と、「httpd -k start」でサービスで起動する方法があります。サービスとして起動した場合は「httpd -k stop」で終了できます。ただし、起動するために一度だけ「httpd -k install」でサービスとして登録する必要があります。

起動したらhttp://localhost/に「It works!」という文字が表示されるはずです。

PHP用の設定

参考URL:PHP利用のための設定 – Apache入門

Apacheにphp用の設定を追加します。

モジュールの登録

「[Apacheルート]/conf/http.conf」ファイルを開いて「Dynamic Shared Object (DSO) Support」で文字列検索します。

#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by
httpd -l’) do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
LoadModule access_compat_module modules/mod_access_compat.so
LoadModule actions_module modules/mod_actions.so


#LoadModule watchdog_module modules/mod_watchdog.so
#LoadModule xml2enc_module modules/mod_xml2enc.so

Apache2.4系なのでこの最後の部分に「LoadModule php5_module c:/php/php5apache2_4.dll」を追加します。”c:php”の部分は自分でPHPをインストールしたディレクトリのパスにして下さい。私の場合は「LoadModule php5_module c:/php56-x64/php5apache2_4.dll」になります。

php拡張子の登録

httpd.confファイルを開いて「AddType」で検索します。

<IfModule mime_module>

#
# AddType allows you to add to or override the MIME configuration
# file specified in TypesConfig for specific file types.
#
#AddType application/x-gzip .tgz
#
# AddEncoding allows you to have certain browsers uncompress
# information on the fly. Note: Not all browsers support this.
#
#AddEncoding x-compress .Z
#AddEncoding x-gzip .gz .tgz
#
# If the AddEncoding directives above are commented-out, then you
# probably should define those extensions to indicate media types:
#
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz



</IfModule>

のIfModuleタグ内の最後に以下の2行を追加します。

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

これで拡張子が「.php」のファイルを開いた時にphpスクリプトが実行されるようになりました。

php.iniの配置場所の指定(PHPIniDir)

httpd.confファイルの一番下の行に以下を追加します。

PHPIniDir “c:/php”

“C:/php”は各自のPHPをインストールしたディレクトリのパスに合わせてください。

php.iniファイルの作成

phpルートディレクトリにある「php.ini-development」というファイルをコピーしてファイル名を「php.ini」に変更します。

拡張モジュールのディレクトリ設定

「php.ini」ファイルを開いて

; Directory in which the loadable extensions (modules) reside.
; http://php.net/extension-dir
; extension_dir = “./”
; On windows:
; extension_dir = “ext”

この辺りの文字列で検索します。この最後の行の「;」を削除して「extension_dir = “ext”」にします。

インクルードパスの設定

「php.ini」ファイルの

;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;

; UNIX: “/path1:/path2”
;include_path = “.:/php/includes”
;
; Windows: “\path1;\path2”
;include_path = “.;c:\php\includes”
;
; PHP’s default setting for include_path is “.;/path/to/php/pear”
; http://php.net/include-path

”Windows”の下の行を

include_path = “.;c:\php\includes;c:\php\pear”;

に変更します。”c:\php”の部分は自分でインストールしたphpのディレクトリに合わせて下さい。

日本語で利用するための設定

「php.ini」ファイルで以下の記述を変更します。

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

; If you wish to have an extension loaded automatically, use the following
; syntax:
;
; extension=modulename.extension
;
; For example, on Windows:
;


;extension=php_mbstring.dll


「extension=php_mbstring.dll」の行の1文字目のセミコロンを外して有効にします。

; language for internal character representation.
; This affects mb_send_mail() and mbstrig.detect_order.
; http://php.net/mbstring.language
; mbstring.language = Japanese

; Use of this INI entry is deprecated, use global internal_encoding instead.
; internal/script encoding.
; Some encoding cannot work as internal encoding. (e.g. SJIS, BIG5, ISO-2022-*)
; If empty, default_charset or internal_encoding or iconv.internal_encoding is used.
; The precedence is: default_charset < internal_encoding < iconv.internal_encoding
; mbstring.internal_encoding = 

また「mbstring.language = Japanese」で検索して該当する行のセミコロンも外します。

「mbstring.internal_encoding」で該当する行を「mbstring.internal_encoding = UTF-8」に変更します。セミコロンも外してね。

; Use of this INI entry is deprecated, use global input_encoding instead.
; http input encoding.
; mbstring.encoding_traslation = On is needed to use this setting.
; If empty, default_charset or input_encoding or mbstring.input is used.
; The precedence is: default_charset < intput_encoding < mbsting.http_input
; http://php.net/mbstring.http-input
; mbstring.http_input = 

; Use of this INI entry is deprecated, use global output_encoding instead.
; http output encoding.
; mb_output_handler must be registered as output buffer to function.
; If empty, default_charset or output_encoding or mbstring.http_output is used.
; The precedence is: default_charset < output_encoding < mbstring.http_output
; To use an output encoding conversion, mbstring’s output handler must be set
; otherwise output encoding conversion cannot be performed.
; http://php.net/mbstring.http-output
; mbstring.http_output = 

; enable automatic encoding translation according to
; mbstring.internal_encoding setting. Input chars are
; converted to internal encoding by setting this to On.
; Note: Do _not_ use automatic encoding translation for
; portable libs/applications.
; http://php.net/mbstring.encoding-translation
; mbstring.encoding_translation = Off

「mbstring.http_input」の行は「mbstring.http_input = pass」

「mbstring.http_outpu」の行は「mbstring.http_outpu = pass」

「; mbstring.encoding_translation = Off」の行は「mbstring.encoding_translation = Off」に変更します。

これらのすぐ下の方に改行で区切って記述している部分を以下のようにどんどん変更していきます。

; automatic encoding detection order.
; “auto” detect order is changed according to mbstring.language
; http://php.net/mbstring.detect-order
mbstring.detect_order = UTF-8,SJIS,EUC-JP,JIS,ASCII

; substitute_character used when character cannot be converted
; one from another
; http://php.net/mbstring.substitute-character
mbstring.substitute_character = none

; overload(replace) single byte functions by mbstring functions.
; mail(), ereg(), etc are overloaded by mb_send_mail(), mb_ereg(),
; etc. Possible values are 0,1,2,4 or combination of them.
; For example, 7 for overload everything.
; 0: No overload
; 1: Overload mail() function
; 2: Overload str*() functions
; 4: Overload ereg*() functions
; http://php.net/mbstring.func-overload
mbstring.func_overload = 0

; enable strict encoding detection.
; Default: Off
mbstring.strict_detection = Off

以上で完了です。

PHPの動作確認

まずApacheを起動しておきます。

任意のディレクトリ(私はC:\Apache24\htdocs)に「phpinfo.php」というファイルを作成します(ファイル名に意味はありません)。テキストエディタで開いて

と入力して保存します。

ブラウザを起動し「http://localhost/phpinfo.php」へアクセスして下さい。するとインストールされているPHPの情報が表示されます。

終わりに

ずいぶん長くなりました(´・ω・`)というかほとんどコピペ…

Apacheディレクトリとphpディレクトリを圧縮しておけば環境移行はパスの設定だけで済みそうですね。気づくの遅いけど。

シェアする

  • このエントリーをはてなブックマークに追加

フォローする