[乐意黎转载]CentOS6编译安装PHP7+Nginx

前端之家收集整理的这篇文章主要介绍了[乐意黎转载]CentOS6编译安装PHP7+Nginx前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文属于动手搭建PHP开发环境的一部分,更多点击链接查看。

本文以centos6为例。

安装PHP

下载

http://cn2.PHP.net/distributions/PHP-5.6.22.tar.bz2
http://cn2.PHP.net/distributions/PHP-7.0.7.tar.bz2

更新yum源

这里将Centos的yum源更换为国内的阿里云源。yum安装正常的可以跳过本步骤。

阿里云Linux安装镜像源地址:
http://mirrors.aliyun.com/

1、备份你的原镜像文件,以免出错后可以恢复:

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

2、下载新的CentOS-Base.repo 到/etc/yum.repos.d/

## CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo

## CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

## CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

3、生成缓存

yum clean all
yum makecache

安装依赖

yum install -y gcc gcc-c++ make cmake bison autoconf wget lrzsz
yum install -y libtool libtool-ltdl-devel 
yum install -y freetype-devel libjpeg.x86_64 libjpeg-devel libpng-devel gd-devel
yum install -y python-devel  patch  sudo 
yum install -y openssl* openssl openssl-devel ncurses-devel
yum install -y bzip* bzip2 unzip zlib-devel
yum install -y libevent*
yum install -y libxml* libxml2-devel
yum install -y libcurl* curl-devel 
yum install -y readline-devel

需要编译libmcrypt、mhash、mcrypt库

tar zxvf /libmcrypt-2.5.8.tar.gz \
&& cd /libmcrypt-2.5.8 && ./configure && make && make install && cd - / && rm -rf /libmcrypt* \
&& tar zxvf /mhash-0.9.9.9.tar.gz && cd mhash-0.9.9.9 && ./configure && make && make install && cd - / && rm -rf /mhash* \
&& tar zxvf /mcrypt-2.6.8.tar.gz && cd mcrypt-2.6.8 && LD_LIBRARY_PATH=/usr/local/lib ./configure && make && make install && cd - / && rm -rf /mcrypt*

开始安装

使用./configure --help查看编译支持的选项。如果写了不支持的选项,如PHP7里不支持--with-MysqL=MysqLnd提示

configure: WARNING: unrecognized options: --with-MysqL
wget http://cn2.PHP.net/distributions/PHP-7.0.7.tar.bz2
tar jxvf PHP-7.0.7.tar.bz2 
cd PHP-7.0.7

$ ./configure --prefix=/usr/local/PHP --with-config-file-scan-dir=/usr/local/PHP/etc/ --enable-inline-optimization --enable-opcache --enable-session --enable-fpm --with-MysqL=MysqLnd --with-MysqLi=MysqLnd --with-pdo-MysqL=MysqLnd --with-pdo-sqlite --with-sqlite3 --with-gettext --enable-mbregex --enable-mbstring --enable-xml --with-iconv --with-mcrypt --with-mhash --with-openssl --enable-bcmath --enable-soap --with-xmlrpc --with-libxml-dir --enable-pcntl --enable-shmop --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-sockets --with-curl --with-curlwrappers --with-zlib --enable-zip --with-bz2 --with-gd --enable-gd-native-ttf --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-readline
 
$ make
$ make install

可选项:

--with-fpm-user=www --with-fpm-group=www

这里面开启了很多扩展。如果这时候忘了开启,以后还能加上吗?答案是可以的。以后只需要进入源码的ext目录,例如忘了pdo_MysqL,进入ext/pdo_MysqL,使用PHPize工具,像安装普通扩展一样即可生成pdo_MysqL.so。

关于:--enable-safe-mode
开启的话PHP可以执行一下系统函数,建议关闭(可搜索受此函数影响的PHP函数

#如果只需要配置某一个目录可以执行则 设置为on并指定 safe_mode_exec_dir=string目录来执行系统函数
#本特性已自 PHP 5.3.0 起废弃并将自 PHP 5.4.0 起移除。
safe_mode = off

PHP7编译不用加这个配置。

编译比较耗内存和cpu。等待半小时左右,编译完成:

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/PHP/lib/PHP/extensions/no-debug-non-zts-20151012/
Installing PHP CLI binary:        /usr/local/PHP/bin/
Installing PHP CLI man page:      /usr/local/PHP/PHP/man/man1/
Installing PHP FPM binary:        /usr/local/PHP/sbin/
Installing PHP FPM config:        /usr/local/PHP/etc/
Installing PHP FPM man page:      /usr/local/PHP/PHP/man/man8/
Installing PHP FPM status page:   /usr/local/PHP/PHP/PHP/fpm/
Installing PHPdbg binary:         /usr/local/PHP/bin/
Installing PHPdbg man page:       /usr/local/PHP/PHP/man/man1/
Installing PHP CGI binary:        /usr/local/PHP/bin/
Installing PHP CGI man page:      /usr/local/PHP/PHP/man/man1/
Installing build environment:     /usr/local/PHP/lib/PHP/build/
Installing header files:           /usr/local/PHP/include/PHP/
Installing helper programs:       /usr/local/PHP/bin/
  program: PHPize
  program: PHP-config
Installing man pages:             /usr/local/PHP/PHP/man/man1/
  page: PHPize.1
  page: PHP-config.1
Installing PEAR environment:      /usr/local/PHP/lib/PHP/
[PEAR] Archive_Tar    - installed: 1.4.0
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.3.0
[PEAR] PEAR           - installed: 1.10.1
Wrote PEAR system config file at: /usr/local/PHP/etc/pear.conf
You may want to add: /usr/local/PHP/lib/PHP to your PHP.ini include_path
/PHP-7.0.7/build/shtool install -c ext/phar/phar.phar /usr/local/PHP/bin
ln -s -f phar.phar /usr/bin/phar
Installing PDO headers:           /usr/include/PHP/ext/pdo/

[root@e8ed9b00e80c PHP-7.0.7]# /usr/bin/PHP -m
[PHP Modules]
bcmath
bz2
Core
ctype
curl
date
dom
fileinfo
filter
gd
gettext
hash
iconv
json
libxml
mbstring
mcrypt
MysqLi
MysqLnd
openssl
pcntl
pcre
PDO
pdo_MysqL
pdo_sqlite
Phar
posix
readline
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
xml
xmlreader
xmlrpc
xmlwriter
zip
zlib

[Zend Modules]

配置文件

需要从安装包里复制PHP.ini、PHP-fpm.conf到安装目录:

$ cp PHP-7.0.7/PHP.ini* /usr/local/PHP/etc/

$ cd /usr/local/PHP/etc/

$ cp PHP.ini-production PHP.ini
$ cp PHP-fpm.conf.default  PHP-fpm.conf

$ cp PHP-fpm.d/www.conf.default PHP-fpm.d/www.conf

$ ls
pear.conf  PHP-fpm.conf.default  PHP.ini-development  PHP.ini-production

配置PHP.ini

# 不显示错误,默认
display_errors = Off

# 在关闭display_errors后开启PHP错误日志(路径在PHP-fpm.conf中配置),默认
log_errors = On

# 字符集,默认
default_charset = "UTF-8"

# 文件上传大小,默认 
upload_max_filesize = 2M

# 设置PHP的扩展库路径,,默认被注释了。
extension_dir = "/usr/local/PHP7/lib/PHP/extensions/no-debug-non-zts-20151012/"
# 如果不设置extension_dir,也可以直接写绝对位置:
# extension=/usr/local/PHP/lib/PHP/extensions/no-debug-non-zts-20151012/redis.so


# 设置PHP的时区
date.timezone = PRC

# 开启opcache,默认是0
[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1

配置PHP-fpm.conf

; 去掉里分号,方便以后重启。建议修改
; Default Value: none
; 下面的值最终目录是/usr/local/PHP/var/run/PHP-fpm.pid
; 开启后可以平滑重启PHP-fpm
pid = run/PHP-fpm.pid

; 设置错误日志的路径,可以默认值
; Note: the default prefix is /usr/local/PHP/var
; Default Value: log/PHP-fpm.log,即/usr/local/PHP/var/log/PHP-fpm.log
error_log = /var/log/PHP-fpm/error.log

; Log等级,可以默认值
; Possible Values: alert,error,warning,notice,debug
; Default Value: notice
log_level = notice

; 后台运行,默认yes,可以默认值
; Default Value: yes
;daemonize = yes

; 引入www.conf文件中的配置,可以默认值
include=/usr/local/PHP/etc/PHP-fpm.d/*.conf

配置www.conf(在PHP-fpm.d目录下)

www.conf这是PHP-fpm进程服务的扩展配置文件

; 设置用户用户组,默认都是nobody。可以默认值
user = Nginx
group = Nginx

; 设置PHP监听
; 下面是默认值,不建议使用。可以默认值
; listen = 127.0.0.1:9000
; 根据Nginx.conf中的配置fastcgi_pass unix:/var/run/PHP-fpm/PHP-fpm.sock;
listen = /var/run/PHP-fpm/PHP-fpm.sock

######开启慢日志。可以默认值
slowlog = /var/log/PHP-fpm/$pool-slow.log
request_slowlog_timeout = 10s

保存配置文件后,检验配置是否正确的方法为:

/usr/local/PHP/sbin/PHP-fpm -t

如果出现诸如test is successful字样,说明配置没有问题。另外该命令也可以让我们知道PHP-fpm的配置文件在哪。

建立软连接:

ln -sf /usr/local/PHP/sbin/PHP-fpm /usr/bin/
ln -sf /usr/local/PHP/bin/PHP /usr/bin/
ln -sf /usr/local/PHP/bin/PHPize /usr/bin/
ln -sf /usr/local/PHP/bin/PHP-config /usr/bin/
ln -sf /usr/local/PHP/bin/PHP-cig /usr/bin/

或者将PHP编译生成bin目录添加到当前Linux系统的环境变量中:

echo -e '\nexport PATH=/usr/local/PHP/bin:/usr/local/PHP/sbin:$PATH\n' >> /etc/profile && source /etc/profile

启动PHP-fpm

/usr/local/PHP/sbin/PHP-fpm

如果提示没有www用户(www.conf里填写了www而不是nobody),则新增:

useradd www
chown -R www:www /www

检测是否启动:

ps aux |grep PHP-fpm # 另外该命令也可以让我们知道fpm的配置文件在哪。
netstat -ant |grep 9000

查看PHP-fpm进程数:

ps aux | grep -c PHP-fpm

PHP-fpm操作汇总:

/usr/local/PHP/sbin/PHP-fpm         # PHP-fpm启动
kill -INT `cat /usr/local/PHP/var/run/PHP-fpm.pid`      # PHP-fpm关闭
kill -USR2 `cat /usr/local/PHP/var/run/PHP-fpm.pid`         #PHP-fpm重启

重启方法二:

killall PHP-fpm
/usr/local/PHP/sbin/PHP-fpm &

如果无法平滑启动,那就终止进程id:

ps aux | grep PHP-fpm
kill -9  1210  #1210指PHP-fpm进程id

安装Nginx

Nginx news
http://nginx.org/

http://nginx.org/download/nginx-1.11.1.tar.gz

依赖:

# 为了支持rewrite功能,我们需要安装pcre
yum install pcre-devel

# 需要ssl的支持,如果不需要ssl支持,请跳过这一步
# yum install openssl*

# gzip 类库安装,按需安装
# yum install zlib zlib-devel

配置编译参数

$ tar -zxvf Nginx-1.11.1.tar.gz
$ cd Nginx-1.11.1
$ ./configure \
    --prefix=/usr/local/Nginx \
    --with-http_stub_status_module  \
    --with-http_ssl_module \
    --with-http_realip_module \
    --with-http_sub_module \
    --with-http_gzip_static_module \
    --with-pcre

配置ok:

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  Nginx path prefix: "/usr/local/Nginx"
  Nginx binary file: "/usr/local/Nginx/sbin/Nginx"
  Nginx modules path: "/usr/local/Nginx/modules"
  Nginx configuration "/usr/local/Nginx/conf"
  Nginx configuration "/usr/local/Nginx/conf/Nginx.conf"
  Nginx pid "/usr/local/Nginx/logs/Nginx.pid"
  Nginx error log "/usr/local/Nginx/logs/error.log"
  Nginx http access log "/usr/local/Nginx/logs/access.log"
  Nginx http client request body temporary files: "client_body_temp"
  Nginx http proxy temporary "proxy_temp"
  Nginx http fastcgi temporary "fastcgi_temp"
  Nginx http uwsgi temporary "uwsgi_temp"
  Nginx http scgi temporary "scgi_temp"

编译安装Nginx

make
make install

设置软连接:

ln -sf /usr/local/Nginx/sbin/Nginx /usr/sbin

检测Nginx:

Nginx -t

显示
Nginx: configuration file /usr/local/Nginx/conf/Nginx.conf test is successful

成功了。我们重新配置下Nginx.conf:

#user nobody;
worker_processes  1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/Nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    
    # 解决虚拟主机名字过长 http://www.jb51.net/article/26412.htm
    server_names_hash_bucket_size 128; 

    #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    # '$status $body_bytes_sent "$http_referer" '
    # '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log logs/access.log main;

    sendfile        on;
    #tcp_nopush on;

    #keepalive_timeout 0;
    keepalive_timeout  65;

    #gzip on;
    
    autoindex on;# 显示目录
    autoindex_exact_size on;# 显示文件大小
    autoindex_localtime on;# 显示文件时间
    
    include vhosts/*.conf;

}

配置localhost:
vhosts/localhost.conf

server {
    listen       80;
    server_name  localhost;

    #charset utf-8;

    #access_log logs/host.access.log main;

    location / {
        root   /www/www/;
        index  index.PHP index.html index.htm;
    }

    #error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.PHP$ {
    # proxy_pass http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.PHP$ {
        root           /www/www/;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.PHP;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

启动Nginx:

/usr/local/Nginx/sbin/Nginx

# 或者
Nginx

重启:

/usr/local/Nginx/sbin/Nginx -s reload

# 或者
Nginx -s reload

停止:

/usr/local/Nginx/sbin/Nginx -s stop

# 或者
Nginx -s stop

如果提示80端口被占用了,可以使用ps aunx | grep 80查看。一般是apache占用了。可以使用:

chkconfig --list
chkconfig Nginx on
service apache off

禁止apache启动并关闭apache服务。

安装扩展

安装swoole

Swoole: PHP的异步、并行、高性能网络通信引擎
http://www.swoole.com/

wget https://github.com/swoole/swoole-src/archive/swoole-1.8.5-stable.zip
unzip swoole-1.8.5-stable.zip
cd swoole-1.8.5-stable
PHPize
./configure
make && make install

安装redis

服务器端:
http://download.redis.io/releases/redis-3.2.0.tar.gz

$ wget http://download.redis.io/releases/redis-3.2.0.tar.gz
$ tar xzf redis-3.2.0.tar.gz
$ cd redis-3.2.0
$ make

默认编译完后在当前目录的src目录下。可以复制可执行文件到其他地方:

mkdir /usr/local/redis
cd src
cp  redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server redis-trib.rb /usr/local/redis

复制配置文件

$ cd redis-3.2.0
$ cp redis.conf /usr/local/redis/

或者安装的时候指定位置:

make PREFIX=/usr/local/redis install

将Redis的命令所在目录添加到系统参数PATH中:
修改profile文件

vi /etc/profile

在最后行追加:

export PATH="$PATH:/usr/local/redis/bin"

然后马上应用这个文件

. /etc/profile

这样就可以直接调用redis-cli的命令了

客户端:

2.0安装

wget https://github.com/nicolasff/PHPredis/archive/2.2.4.tar.gz
tar -zxvf 2.2.4
cd PHPredis-2.2.4/
PHPize
./configure 
make install

3.0安装

PHPredis/PHPredis: A PHP extension for Redis
https://github.com/phpredis/phpredis

需要先安装igbinary:

PECL :: Package :: igbinary
http://pecl.php.net/package/igbinary

wget http://pecl.PHP.net/get/igbinary-1.2.1.tgz
tar zxvf igbinary-1.2.1.tgz
cd igbinary-1.2.1
PHPize
./configure 
make install
wget https://github.com/PHPredis/PHPredis/archive/3.0.0-rc1.zip
unzip 3.0.0-rc1
cd PHPredis-3.0.0-rc1/

PHPize
./configure [--enable-redis-igbinary]
make install

安装memcache

更多资料

1、linux下为已经编译好的PHP环境添加MysqL扩展
https://ask.hellobi.com/blog/liangyong/69

附件

相关源码包
链接:http://pan.baidu.com/s/1kUQTlFD密码: ajzk

作者:飞鸿影~

出处:http://52fhy.cnblogs.com/

如果您觉得本文对您的学习有所帮助,可通过“微信”或“支付宝”打赏博主,或者点击页面下方推荐以支持博主。

版权申明:没有标明转载或特殊申明均为作者原创。本文采用以下协议进行授权,自由转载 - 非商用 - 非衍生 - 保持署名 | Creative Commons BY-NC-ND 3.0,转载请注明作者及出处。

原文链接:https://www.f2er.com/centos/377437.html

猜你在找的CentOS相关文章