1. 更新系统
yum update
2. 安装nginx
- 安装
yum install nginx
- 启动
service nginx start
- 检查
浏览器中访问你的ip地址,如127.0.0.1
,看到如下画面即为安装成功
3. 安装php7
阿里云的镜像目前还没有php7的源,需要编译安装:)
- 安装必需的工具和依赖
yum install -y gcc-c++ autoconf freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses curl openssl-devel gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel
- 下载php7,最新版本可访问 PHP download
wget http://cn2.php.net/distributions/php-7.0.11.tar.gz
tar -zxvf php-7.0.11.tar.gz
rm -f php-7.0.11.tar.gz
mv php-7.0.11/ /usr/src/
cd /usr/src/php-7.0.11
- 编译安装(编译可能需要较长时间)
'./configure' \
'--prefix=/usr/local/php' \
'--with-config-file-path=/usr/local/php/etc/' \
'--with-mysql=/usr/local/mysql/' \
'--with-libxml-dir=/usr/local/libxml2/' \
'--with-png-dir=/usr/local/libpng/' \
'--with-jpeg-dir=/usr/local/jpeg8/' \
'--with-freetype-dir=/usr/local/freetype/' \
'--with-gd=/usr/local/gd/' \
'--with-zlib-dir=/usr/local/zlib/' \
'--with-mcrypt=/usr/local/libmcrypt/' \
'--with-mysqli=/usr/local/mysql/bin/mysql_config' \
'--enable-soap' \
'--enable-mbstring=all' \
'--enable-sockets' \
'--enable-fpm'
make
make install
- 设置配置文件路径
cp php.ini-development /usr/local/php/php.ini
cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
cp sapi/fpm/php-fpm /usr/local/bin
4. 修改配置
(1) 修改php配置
vim /usr/local/php/php.ini
找到cgi.fix_pathinfo=
,修改为:
cgi.fix_pathinfo=0
以上修改可防止访问文件不存在时的脚本注入
(2) 修改用户
vim /usr/local/etc/php-fpm.d/www.conf.default
找到如下内容并修改为:
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = www-data
group = www-data
5.修改nginx配置
(1) 找到nginx配置文件
一般位于 /etc/nginx.conf
,找到并修改如下内容:
user = www-data
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root html;
index index.php index.html index.htm;
}
location ~* \.php$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
(2) 检查配置是否成功
nginx -t
若不报错,则配置修改成功
(3) 重启nginx
service nginx restart
6. 测试环境
进入nginx项目根目录,一般为/usr/share/nginx/html
,可在/etc/nginx.conf
中通过修改server
块下的root
属性实现自定义
cd /usr/share/nginx/html
echo '<?php phpinfo(); ?>' > info.php
访问浏览器,如127.0.0.1/info.php
,如显示php的信息页面则配置完成
PS
- 操作过程中遇到的问题可查询log日志
/var/log/nginx/error.log