[nginx] 在Amazon Linux上使用nginx和php-fpm来执行带基本认证的PHP脚本
在Amazon Linux 2015.09环境中搭建一个能够运行PHP脚本的环境,使用nginx和php-fpm。
1. 預先準備工作 (Yù zuò)
1.1. 创建Amazon Linux环境
-
- Amazon Linux環境の構築
キーペアの作成 (新規): http://qiita.com/tcsh/items/59303d9506ca7d13f744
インスタンスの作成 (Public): http://qiita.com/tcsh/items/ae8f1f0d706237327c5a
1.2. 搭建PHP环境
sudo yum install php54 -y
which php
/usr/bin/php
php -v
PHP 5.4.45 (cli) (built: Sep 11 2015 21:23:18)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
1.3. 安装nginx
- (参考) Amazon Linuxにnginxをインストールする (yumパッケージ): http://qiita.com/tcsh/items/001c05ce9b0e5a34abc7
which nginx
/usr/sbin/nginx
1.4. (任意)htpasswd命令
如果需要在Nginx上使用基本认证,这是必要的。
在Amazon Linux中,似乎已经默认安装了该功能。
which htpasswd
/usr/bin/htpasswd
如果不存在的话,我会安装它。
sudo yum install httpd-tools
2. 安装php-fpm
2.1. 安装
sudo yum install php54-fpm -y
(略)
インストール:
php54-fpm.x86_64 0:5.4.45-1.75.amzn1
完了しました!
2.2. 验证
which php-fpm
/usr/sbin/php-fpm
3. PHP-FPM的设置
将设置文件中标有”apache”的部分改为”nginx”。
;39行目付近
;user = apache
user = nginx
;41行目付近
;group = apache
group = nginx
4. 开启php-fpm
sudo service php-fpm start
Starting php-fpm: [ OK ]
sudo chkconfig php-fpm on \
&& chkconfig --list php-fpm
php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
5. 更改 nginx 的配置
5.1 修改nginx的配置
# 44行目付近
# index index.html index.htm;
index index.php index.html;
# 78行目付近
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
一旦更改了设置,将检查设置文件是否遵循正确的语法。
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
5.2 重新启动nginx
一旦重新启动nginx并确认它是否成功启动。
sudo service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
6. 设定基本认证 (顺序不限)
我们决定对名为’sysadm’的目录进行基本身份验证。
6.1. 创建验证文件用的目录
将认证文件放置在公共目录(/usr/share/nginx/html)的直接子目录之外的位置。
sudo mkdir /usr/share/nginx/credentials
6.2. 创建认证文件
AUTH_USER='sysadm'
htpasswd -c htpasswd ${AUTH_USER}
New password:
Re-type new password:
Adding password for user sysadm
将已创建的认证文件放置。
sudo mv htpasswd /usr/share/nginx/credentials/ \
&& ls /usr/share/nginx/credentials/htpasswd
创建进行基本认证的目录
sudo mkdir /usr/share/nginx/html/sysadm
6.4. 更改nginx的设置
把sysadm目录的配置添加到nginx中。如果要在’/sysadm’下执行PHP脚本,则似乎还需要单独设置php-fpm的配置。
# 57行目付近に追加
location ~ ^/sysadm/* {
auth_basic "Welcome sysadm.";
auth_basic_user_file /usr/share/nginx/credentials/htpasswd;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
更改設定後,將檢查設定文件是否符合正確的語法。
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
6.5 重新启动 Nginx
sudo service nginx restart
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
7. 验证 PHP 脚本的运行状况
7.1. 创建PHP脚本
cat << EOF > ~/info.php
<?php
phpinfo();
EOF
将编写好的PHP脚本放置在公开目录中。
sudo cp ~/info.php /usr/share/nginx/html/sysadm/ \
&& cat /usr/share/nginx/html/sysadm/info.php
sudo cp ~/info.php /usr/share/nginx/html/ \
&& cat /usr/share/nginx/html/info.php
7.2版本的PHP脚本运行测试
我试着从浏览器访问PHP脚本。
- ベーシック認証の設定をした場合: http://<ホスト>/sysadm/info.php
当弹出基本身份认证对话框时,在输入用户名和密码后,应该会显示以下画面。
- ベーシック認証の設定をしなかった場合: http://<ホスト>/info.php
应该会显示以下屏幕。

如果没有设置基本认证,那么请在确认操作后务必删除。
sudo rm /usr/share/nginx/html/info.php
&& cat /usr/share/nginx/html/info.php
cat: /usr/share/nginx/html/info.php: そのようなファイルやディレクトリはありません