Skip to content

Instantly share code, notes, and snippets.

@jingmian
Last active November 23, 2018 07:39
Show Gist options
  • Select an option

  • Save jingmian/a7d23fccc1dcc29a3e16402542470a09 to your computer and use it in GitHub Desktop.

Select an option

Save jingmian/a7d23fccc1dcc29a3e16402542470a09 to your computer and use it in GitHub Desktop.
lnmp安装多版本PHP
ubuntu下必须安装apt-get -y install libfreetype6-dev
//开始安装,首先要下载php-5.4.45.tar.gz源码包。
# tar xzvf php-5.4.45.tar.gz
# cd php-5.4.45
# ./configure --prefix=/usr/local/php54 --with-config-file-path=/usr/local/php54/etc --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-intl --with-xsl
# make ZEND_EXTRA_LIBS='-liconv'
# make install
# cp php.ini-production /usr/local/php/etc/php.ini
//php.ini 配置
post_max_size = 50M
upload_max_filesize = 50M
date.timezone = PRC
short_open_tag = On
cgi.fix_pathinfo=0
max_execution_time = 300
disable_functions=passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server
//php-fpm.conf 配置
# vim /usr/local/php/etc/php-fpm.conf
[global]
pid = /usr/local/php54/var/run/php-fpm.pid
error_log = /usr/local/php54/var/log/php-fpm.log
log_level = notice
[www]
listen = /tmp/php54-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1:9001
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 40
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 40
request_terminate_timeout = 100
request_slowlog_timeout = 0
#当request_slowlog_timeout 设为一个具体秒时request_slowlog_timeout =1,表示如果哪个脚本执行时间大于1秒,会记录这个脚本到慢日志文件中
#request_slowlog_timeout =0表示关闭慢日志输出。
slowlog = var/log/slow.log #这个是配合上面request_slowlog_timeout使用的
#慢日志的例子,慢日志会记录下进程号,脚本名称,具体哪个文件哪行代码的哪个函数执行时间过长。
//启动 php-fpm
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php54-fpm
# chmod +x /etc/init.d/php54-fpm
# /etc/init.d/php54-fpm start
//修改nginx配置,对需要的服务配置使用php-5.4.45
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php54-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
//配置php-5.4.45 的php54-fpm 开机自动启动
# chkconfig --add php54-fpm
# chkconfig php54-fpm on
chkconfig 功能说明:检查,设置系统的各种服务。
语法:chkconfig [–add][–del][–list][系统服务]
chkconfig [–level <等级代号>][系统服务][on/off/reset]
============================================nginx日志级别提高======================
在配置nginx.conf 的时候,有一项是指定错误日志的,默认情况下你不指定也没有关系,因为nginx很少有错误日志记录的。但有时出现问题时,是有必要记录一下错误日志的,方便我们排查问题。
error_log 级别分为 debug, info, notice, warn, error, crit 默认为crit, 该级别在日志名后边定义格式如下:
vim /usr/local/nginx/conf/nginx.conf
error_log /home/wwwlogs/nginx_error.log; #后面留空就行
=====================================================编译安装PHP 7=============================================================
# 安装mcrypt
yum install -y php-mcrypt libmcrypt libmcrypt-devel
# 升级bison
cd /var/soft/
wget http://ftp.gnu.org/gnu/bison/bison-2.6.4.tar.gz
tar -xvzf bison-2.6.4.tar.gz
cd bison-2.6.4
./configure
make && make install
# 升级re2c,解决You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.错误
cd /var/soft/
wget http://sourceforge.net/projects/re2c/files/re2c/0.13.5/re2c-0.13.5.tar.gz/download
tar zxf re2c-0.13.5.tar.gz && cd re2c-0.13.5
./configure
make && make install
编译PHP7
# 创建目录
mkdir -p /var/soft/php7
cd /var/soft/php7
# 下载源代码
git clone http://git.php.net/repository/php-src.git
cd php-src
./buildconf
#下载php7-1-18源码,如果已经安装相关的依赖环境后,其实可以直接从这里开始
# 配置参数
./configure --prefix=/usr/local/php7 \
--with-config-file-path=/usr/local/php7/etc \
--with-mcrypt=/usr/include \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gd \
--with-iconv \
--with-zlib \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache
#################################################################################
# 编译安装php时出现 /encodings.c:101: undefined reference to `libiconv_close’ 错误的解决方法
# 在装php时出现以下的错误:
# /root/Downloads/php-5.6.5/ext/xmlrpc/libxmlrpc/encodings.c:73: undefined reference to `libiconv_open'
# /root/Downloads/php-5.6.5/ext/xmlrpc/libxmlrpc/encodings.c:81: undefined reference to `libiconv'
# /root/Downloads/php-5.6.5/ext/xmlrpc/libxmlrpc/encodings.c:101: undefined reference to `libiconv_close'
# /root/Downloads/php-5.6.5/ext/xmlrpc/libxmlrpc/encodings.c:101: undefined reference to `libiconv_close'
# collect2: error: ld returned 1 exit status
# make: *** [sapi/cli/php] Error 1
# 根据php官网描述这是一个bug(https://bugs.php.net/bug.php?id=52611)。在configure以后要手动修改下Makefile文件,找到EXTRA_LIBS 在最后添加 -liconv 即可顺利通过
# (大约在Makefile文件的104)
# EXTRA_LIBS = -lcrypt -lz -lresolv -lcrypt -lrt -lmysqlclient -lmcrypt -lltdl -lpng -lz -ljpeg -lcurl -lz -lrt -lm -ldl -lnsl -lxml2 -lz -lm -ldl -lssl -lcrypto -lcurl -lxml2 -lz -lm -ldl -lfreetype -lmysqlclient -lz -lm -lssl -lcrypto -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lcrypt -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lxml2 -lz -lm -ldl -lssl -lcrypto -lcrypt -liconv
#################################################################################
# 编译安装
make && sudo make install
# 配置
cp php.ini-production /usr/local/php7/etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php7-fpm
chmod +x /etc/init.d/php7-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
#####注意,如果想把/etc/init.d/php7.1.17-fpm 命名对于的版本号,
vim /etc/init.d/php7.1.17-fpm
php_fpm_PID=${prefix}/var/run/php-fpm.pid 改为php_fpm_PID=${prefix}/var/run/php7.1.17-fpm.pid
还需要配合php-fpm.conf配置文件里面的
[global]
pid = /usr/local/php7.1.17/var/run/php7.1.17-fpm.pid
###################################################
//如果你想在linux能运行php命名,添加一个软链接,在/usr/bin目录下
php -> /usr/local/php7.15/bin/php
# vim /usr/local/php7.15/etc/php-fpm.conf
[global]
pid = /usr/local/php7.15/var/run/php-fpm.pid
error_log = /usr/local/php7.15/var/log/php-fpm.log
#错误级别(生产环境下应该提高错误级别,推荐error)
log_level = notice
# vim /usr/local/php7/etc/php-fpm.d/www.conf
[www]
listen = /tmp/php7-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1:9002
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 40
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 40
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow.log
# 配置opcache
//PHP7随服务器启动
cd /etc/init.d
sudo update-rc.d php7.1.17-fpm defaults 20
-----卸载启动脚本的方法
sudo update-rc.d -f php7.1.17-fpm remove
-----
vim /usr/local/php7/etc/php.ini
# 加入
zend_extension=/usr/local/php7/lib/php/extensions/no-debug-non-zts-20141001/opcache.so
# 启动
/etc/init.d/php7-fpm start
# 查看PHP版本
/usr/local/php7/bin/php -v
结果:
PHP 7.0.0-dev (cli) (built:www.111cn.net Mar 3 2015 10:02:26)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies
==================================================TP5,laravel框架可能提示500解决办法================================
在入口文件index.php,添加以下代码查看错误
error_reporting(E_ALL);
ini_set('display_errors', '1');
Warning: require(): open_basedir restriction in effect. File(/home/wwwroot/mz.kchangfu.cn/thinkphp/start.php) is not within the allowed path(s): (/home/wwwroot/mz.kchangfu.cn/public/:/tmp/:/proc/) in /home/wwwroot/mz.kchangfu.cn/public/index.php on line 22
Warning: require(/home/wwwroot/mz.kchangfu.cn/thinkphp/start.php): failed to open stream: Operation not permitted in /home/wwwroot/mz.kchangfu.cn/public/index.php on line 22
Fatal error: require(): Failed opening required '/home/wwwroot/mz.kchangfu.cn/public/../thinkphp/start.php' (include_path='.:/usr/local/php/lib/php') in /home/wwwroot/mz.kchangfu.cn/public/index.php on line 22
类似的错误,就是php不能引入其授权目录上级及其以上的文件
解决办法
在 fastcgi.conf;中加入如下代码 #:/home/wwwroot/default/laravel/
vim /usr/local/nginx/conf/fastcgi.conf
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/:/home/wwwroot/default/laravel/";
====================================扩展安装*.so=====================================
找到PHP的安装包,cd 进入安装包
cd php-xxx.xxx/ext/curl
phpize
如果报找不到phpize就补全路径,/usr/local/php/bin/phpize
如果出现以下错误:Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment vari
解决办法:
yum install m4
yum install autoconf
然后重新phpize
./configure -with-curl=/usr/local/curl -with-php-config=/usr/local/php7.15/bin/php-config
make && make install
php.ini
extension_dir=/usr/local/php7.15/lib/php/extensions/no-debug-non-zts-20160303/
==================================ssl https加密协议开启=================================
server {
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
listen 443 ssl;
ssl_certificate /usr/local/nginx/conf/ssl/xxx.com_crt;
ssl_certificate_key /usr/local/nginx/conf/ssl/xxx.com_key;
}
#强制https开启
#server {
#listen 80;
#server_name api.xxx.com;
#return 301 https://api.xxx.com$request_uri;
#}
=====================================开启错误日志=================================================
1. error_reporting = E_ALL ;将会向PHP报告发生的每个错误
2. display_errors = Off ;不显示 满足上条指令所定义规则的所有错误报告,就是不在浏览器上面显示出来
3. log_errors = On ;决定日志语句记录的位置
4. log_errors_max_len = 1024 ;设置每个日志项的最大长度
5. error_log = /home/wwwlogs/php_errors.log ;指定产生的错误报告写入操作系统的日志里
==========================================================mysql====================================================================
操作日志是mysql-bin.00 这些,都是二进制的
这么查看/usr/local/mysql/bin/mysqlbinlog /usr/local/mysql/var/mysql-bin.00001
//清空日志
mysql-bin.0000*删除,然后清空mysql-bin.index文件里的内容,再重启mysql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment