Centos7+nginx+php(php-fpm)基础web运行环境手工部署
准备工作
1.安装编译支持库
yum install -y gcc automake autoconf libtool make
yum install -y gcc gcc-c++
yum install -y openldap-devel libicu-devel libpng-devel libjpeg-devel curl-devel openssl-devel libxml2-devel libzip-devel
2.安装PCRE
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
tar -xzf pcre-8.40.tar.gz -C ./
cd pcre-8.40
./configure --prefix=/usr/local/pcre
make && make install
cd ..
3.安装zlib
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -xzf zlib-1.2.11.tar.gz -C ./
cd zlib-1.2.11
./configure --prefix=/usr/local/zlib
make && make install
cd ..
4.安装openssl
wget https://www.openssl.org/source/openssl-1.0.2k.tar.gz
tar -xzf openssl-1.0.2k.tar.gz -C ./
#注意,这里不需要进行安装,后面步骤省略。
安装nginx
官方下载 http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.12.0.tar.gz
tar -xzf nginx-1.12.0.tar.gz -C ./
cd nginx-1.12.0
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/mnt/tools/pcre-8.40/ \
--with-zlib=/mnt/tools/zlib-1.2.11/ \
--with-openssl=/mnt/tools/openssl-1.0.2k/ \
--with-http_stub_status_module \
--with-http_realip_module \
--with-stream
#注:cpre、zlib、openssl等依赖包的路径是解压的源码路径不是安装后的路径。
make
make install
安装PHP
安装依赖扩展
yum install -y libxml2-devel openssl-devel libcurl-devel libjpeg-devel libpng-devel libicu-devel openldap-devel
官网下载 http://php.net/releases/index.php
官网安装文档 http://php.net/manual/zh/install.unix.nginx.php
【可选】扩展freetype库安装
wget https://cytranet.dl.sourceforge.net/project/freetype/freetype2/2.9.1/freetype-2.9.1.tar.gz
tar -xzf freetype-2.9.1.tar.gz
cd freetype-2.9.1
./configure --prefix=/usr/local/freetype/2.9.1/ --libdir=/usr/local/freetype/2.9.1/lib64 --with-static --with-shared
make && make install
wget http://php.net/get/php-5.6.29.tar.gz/from/this/mirror -O php-5.6.29.tar.gz
tar -xzf php-5.6.29.tar.gz -C ./
cd php-5.6.29
./configure --prefix=/usr/local/php/php5.6.29/\
--with-config-file-path=/usr/local/php/php5.6.29/\
--with-libdir=lib64\
--enable-fpm\
--with-fpm-user=php-fpm\
--with-fpm-group=www\
--enable-mysqlnd\
--with-mysql=mysqlnd\
--with-mysqli=mysqlnd\
--with-pdo-mysql=mysqlnd\
--enable-opcache\
--enable-pcntl\
--enable-mbstring\
--enable-soap\
--enable-zip\
--enable-calendar\
--enable-bcmath\
--enable-exif\
--enable-ftp\
--enable-intl\
--enable-sockets\
--with-openssl\
--with-zlib\
--with-curl\
--with-gd\
--with-zlib-dir=/usr/lib\
--with-png-dir=/usr/lib\
--with-jpeg-dir=/usr/lib\
--with-gettext\
--with-mhash\
--with-ldap
make && make install
[可选]编译freetype将参数追加到configure 后面
--with-freetype-dir=/usr/local/freetype/2.9.1/\
--enable-gd-native-ttf\
配置文件
创建PHP配置文件
#创建配置文件
cd /usr/local/php/php-5.6.29/
cp php.ini-development /usr/local/php/php5.6.29/php.ini
cp /usr/local/php/php5.6.29/etc/php-fpm.conf.default /usr/local/php/php5.6.29/etc/php-fpm.conf
#复制php-fpm管理器脚本
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
修改配置文件
vi /usr/local/php/php5.6.29/php.ini
#需要着重提醒的是,如果文件不存在,则阻止 Nginx 将请求发送到后端的 PHP-FPM 模块,
#以避免遭受恶意脚本注入的攻击。
#定位到 cgi.fix_pathinfo= 并将其修改为如下所示:
cgi.fix_pathinfo=0
创建php-fpm运行用户,可通过配置查看当前配置用户及组名。
vi /usr/local/etc/php-fpm.conf
; 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 = php-fpm
group = www
#创建用户
useradd -s /sbin/nologin -M php-fpm
#创建组
groupadd www
通过php-fpm脚本,启动php服务(停止、重启、重载)。
service php-fpm start
service php-fpm restart
service php-fpm stop
service php-fpm reload
配置nginx,使其支持PHP应用。
vi /usr/local/nginx/nginx.conf
修改默认的 location 块,使其支持 .php 文件:
location / {
root html;
index index.php index.html index.htm;
}
下一步配置来保证对于 .php 文件的请求将被传送到后端的 PHP-FPM 模块, 取消默认的 PHP 配置块的注释,并修改为下面的内容:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
测试运行
重启 Nginx
/usr/local/nginx/nginx -s stop
/usr/local/nginx/nginx
#注意前面应该启动 php-fpm ,如:service php-fpm start
echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/index.php
访问 http://www.example.com 已经成功。
本文注意细节已经在过程步骤中说明,留意查看。如有疑问,请在文末留言,谢谢。
更多关于nginx、apache等web服务环境部署及生成环境部署优化,欢迎关注本博客文章。
续篇文章