LoginSignup
22
23

More than 5 years have passed since last update.

ApacheでPHPのソースが表示される場合の対処

Last updated at Posted at 2016-03-25

[目的]
ApacheでPHPファイルを配置した際に、PHPが実行されずにソースが表示された場合の対処をする

こちら で構築した環境で、PHPファイルを配置した場合、PHPのソースがそのまま表示されてしまいます。

PHPファイルとして配置してもブラウザでソースのまま表示される
<?php
echo phpinfo();
?>

PHPのソースが表示される要因として、ApacheにPHPモジュールが追加されていないことが考えられる。

/etc/httpd/conf/httpd.conf にモジュールを追加しようとすると、libphp5.so が存在しないため、エラーになる。

/etc/httpd/conf/httpd.confに追記
LoadModule php5_module modules/libphp5.so
Apache再起動するとエラーになる
service httpd restart
Redirecting to /bin/systemctl restart  httpd.service
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: user
Password: 
==== AUTHENTICATION COMPLETE ===
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.

これは、libphp5.so が /etc/httpd/modules/ 配下に存在しないのが原因である。

そのため、libphp5.so を追加する。

yumでphpをインストール
sudo yum install php
読み込んだプラグイン:fastestmirror, remove-with-leaves, show-leaves
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.kddilabs.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ php.x86_64 0:5.4.16-36.el7_1 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

================================================================================
 Package       アーキテクチャー バージョン                 リポジトリー    容量
================================================================================
インストール中:
 php           x86_64           5.4.16-36.el7_1            base           1.4 M

トランザクションの要約
================================================================================
インストール  1 パッケージ

総ダウンロード容量: 1.4 M
インストール容量: 4.4 M
Is this ok [y/d/N]: y
Downloading packages:
php-5.4.16-36.el7_1.x86_64.rpm                             | 1.4 MB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : php-5.4.16-36.el7_1.x86_64                      1/1 
  検証中                  : php-5.4.16-36.el7_1.x86_64                      1/1 

インストール:
  php.x86_64 0:5.4.16-36.el7_1                                                  

完了しました!
New leaves:
  php.x86_64

これで、libphp5.so が配置される。

ls /etc/httpd/modules/libphp5.so 
/etc/httpd/modules/libphp5.so
Apache再起動で反映
service httpd restart
22
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
23