介紹:php
久聞php7的速度以及性能那但是比php5系列的任何一版本都要快,具體性能有多好,建議仍是先嚐試下再說。若是你是升級或新安裝,那你首先須要考慮php7和程序是否存在兼容性,若是程序是基於php5開發的,那麼就須要考慮php7是否適合你當前的生產環境,今天我就實操並安裝用於生產中。html
先安裝php依賴包,不然在編譯安裝php7的過程中會出現各類報錯,安裝完成後便可進入下一個環節。mysql
安裝擴展包並更新系統內核:nginx
yum install epel-release -y
yum update
安裝php依賴組件(包含Nginx依賴):c++
yum -y install wget vim pcre pcre-devel openssl openssl-devel libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel
建立用戶和組,並下載php安裝包解壓:web
cd /tmp groupadd www useradd -g www www wget http://am1.php.net/distributions/php-7.2.1.tar.gz tar xvf php-7.2.1.tar.gz cd php-7.2.1
設置變量並開始源碼編譯:sql
cp -frp /usr/lib64/libldap* /usr/lib/
./configure --prefix=/usr/local/php-7.2.1 \ --with-config-file-path=/usr/local/php-7.2.1/etc \ --enable-fpm \ --with-fpm-user=www \ --with-fpm-group=www \ --enable-mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --enable-mysqlnd-compression-support \ --with-iconv-dir \ --with-freetype-dir \ --with-jpeg-dir \ --with-png-dir \ --with-zlib \ --with-libxml-dir \ --enable-xml \ --disable-rpath \ --enable-bcmath \ --enable-shmop \ --enable-sysvsem \ --enable-inline-optimization \ --with-curl \ --enable-mbregex \ --enable-mbstring \ --enable-intl \ --with-mcrypt \ --with-libmbfl \ --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-opcache \ --with-pear \ --enable-maintainer-zts \ --with-ldap=shared \
--without-gdbm \
--with-iconv=/usr/local/
問題:make: *** [sapi/cgi/php-cgi.exe] Error 1shell
最近遇到一個問題,在安裝PHP的時候,make步驟出現這樣的錯:ubuntu
ext/iconv/iconv.o: In function `_php_iconv_appendl': /home/r/php-5.3.3/ext/iconv/iconv.c:337: undefined reference to `_iconv' /home/r/php-5.3.3/ext/iconv/iconv.c:374: undefined reference to `_iconv' ext/iconv/iconv.o: In function `_php_iconv_strlen': /home/r/php-5.3.3/ext/iconv/iconv.c:615: undefined reference to `_iconv' ext/iconv/iconv.o: In function `_php_iconv_substr': /home/r/php-5.3.3/ext/iconv/iconv.c:736: undefined reference to `_iconv' ext/iconv/iconv.o: In function `php_iconv_string': /home/r/php-5.3.3/ext/iconv/iconv.c:501: undefined reference to `_iconv' ext/iconv/iconv.o:/var/src/php-5.3.3/ext/iconv/iconv.c:521: more undefined references to `_iconv' follow collect2: ld returned 1 exit status make: *** [sapi/cgi/php-cgi.exe] Error 1
這樣是make不成功的,這是由於機器上沒有安裝iconv,安裝後再編繹便可,個人解決步驟是這樣的:vim
先安裝iconv:
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz tar -zxvf libiconv-1.13.1.tar.gz cd libiconv-1.13.1 ./configure --prefix=/usr/local make && make install
接下來再編繹php的時候加上–with-iconv=/usr/local/ 便可:
--with-iconv=/usr/local/
若無報錯執行下一步安裝,若是編譯過程當中出現錯誤,根據報錯安裝依賴包,一般不會出現這種問題。
注意:–enable-gd-jis-conv 此參數會致使Zabbix中文字符亂碼,建議取消。
開始安裝:
make -j 4 && make install
完成安裝後配置php.ini文件:
cp php.ini-development /usr/local/php/etc/php.ini cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
修改 php.ini 相關參數:
vim /usr/local/php/etc/php.ini
expose_php = Off short_open_tag = ON max_execution_time = 300 max_input_time = 300 memory_limit = 128M post_max_size = 32M date.timezone = Asia/Shanghai mbstring.func_overload=2 extension = "/usr/local/php/lib/php/extensions/no-debug-zts-20170718/ldap.so" #OPcache 緩存 [opcache] zend_extension=/usr/local/php/lib/php/extensions/no-debug-zts-20170718/opcache.so opcache.memory_consumption=128 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=4000 opcache.revalidate_freq=60 opcache.fast_shutdown=1 opcache.enable_cli=1 #設置php安全函數 disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru #配置www.conf listen = /var/run/www/php-cgi.sock listen.owner = www listen.group = www listen.mode = 0660 listen.allowed_clients = 127.0.0.1 pm = dynamic listen.backlog = -1 pm.max_children = 180 pm.start_servers = 50 pm.min_spare_servers = 50 pm.max_spare_servers = 180 request_terminate_timeout = 120 request_slowlog_timeout = 50 slowlog = var/log/slow.log
配置www.conf
listen = /var/run/www/php-cgi.sock listen.owner = www listen.group = www listen.mode = 0660 listen.allowed_clients = 127.0.0.1 pm = dynamic listen.backlog = -1 pm.max_children = 180 pm.start_servers = 50 pm.min_spare_servers = 50 pm.max_spare_servers = 180 request_terminate_timeout = 120 request_slowlog_timeout = 50 slowlog = var/log/slow.log
建立php-cgi.sock存放目錄
mkdir /var/run/www/ chown -R www:www /var/run/www
配置php-fpm.conf
vim /usr/local/php/etc/php-fpm.conf
取下如下注釋並填寫完整路徑:
pid = /usr/local/php/var/run/php-fpm.pid
至此php7已經安裝完成。
說明:禁用php函數,若是程序須要這些函數,能夠取消禁止,新手建議忽略此步驟。
建立system系統單元文件php-fpm啓動腳本:
vim /usr/lib/systemd/system/php-fpm.service
添加以下變量內容:
[Unit] Description=The PHP FastCGI Process Manager After=syslog.target network.target [Service] Type=simple PIDFile=/usr/local/php/var/run/php-fpm.pid ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf ExecReload=/bin/kill -USR2 $MAINPID [Install] WantedBy=multi-user.target
啓動php-fpm服務並加入開機自啓動:
systemctl enable php-fpm.service
systemctl restart php-fpm.service
nginx自己不能處理PHP,它只是個web服務器,當接收到請求後,若是是php請求,則發給php解釋器處理,並把結果返回給客戶端。
nginx通常是把請求發fastcgi管理進程處理,fascgi管理進程選擇cgi子進程處理結果並返回被nginx
本文以php-fpm爲例介紹如何使nginx支持PHP
1、編譯安裝php-fpm
什麼是PHP-FPM
PHP-FPM是一個PHP FastCGI管理器,是隻用於PHP的,能夠在 http://php-fpm.org/download下載獲得.
PHP-FPM實際上是PHP源代碼的一個補丁,旨在將FastCGI進程管理整合進PHP包中。必須將它patch到你的PHP源代碼中,在編譯安裝PHP後纔可使用。
新版PHP已經集成php-fpm了,再也不是第三方的包了,推薦使用。PHP-FPM提供了更好的PHP進程管理方式,能夠有效控制內存和進程、能夠平滑重載PHP配置,比spawn-fcgi具備更多優勢,因此被PHP官方收錄了。在./configure的時候帶 –enable-fpm參數便可開啓PHP-FPM,其它參數都是配置php的,具體選項含義能夠查看這裏。
安裝前準備
centos下執行
yum -y install gcc automake autoconf libtool make yum -y install gcc gcc-c++ glibc yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel
新版php-fpm安裝(推薦安裝方式)
wget http://cn2.php.net/distributions/php-5.4.7.tar.gz tar zvxf php-5.4.7.tar.gz cd php-5.4.7
./configure --prefix=/usr/local/php-5.4.7 --enable-fpm --with-mcrypt \
--enable-mbstring --disable-pdo --with-curl --disable-debug --disable-rpath \
--enable-inline-optimization --with-bz2 --with-zlib --enable-sockets \
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \
--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \
--with-gd --with-jpeg-dir
make all install
舊版手動打補丁php-fpm安裝(舊版程序已經沒有了,你們新版的吧,這裏作個展現)
wget http://cn2.php.net/get/php-5.2.17.tar.gz
wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
tar zvxf php-5.2.17.tar.gz
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
cd php-5.2.17
./configure --prefix=/usr/local/php -with-config-file-path=/usr/local/php/etc
-with-mysql=/usr/local/mysql
-with-mysqli=/usr/local/mysql/bin/mysql_config -with-openssl -enable-fpm -enable-mbstring
-with-freetype-dir -with-jpeg-dir -with-png-dir -with-zlib-dir -with-libxml-dir=/usr -enable-xml
-with-mhash -with-mcrypt -enable-pcntl -enable-sockets -with-bz2 -with-curl -with-curlwrappers
-enable-mbregex -with-gd -enable-gd-native-ttf -enable-zip -enable-soap -with-iconv -enable-bcmath
-enable-shmop -enable-sysvsem -enable-inline-optimization -with-ldap -with-ldap-sasl -enable-pdo
-with-pdo-mysql
make all install
以上兩種方式均可以安裝php-fpm,安裝後內容放在/usr/local/php目錄下
以上就完成了php-fpm的安裝。
下面是對php-fpm運行用戶進行設置
cd /usr/local/php cp etc/php-fpm.conf.default etc/php-fpm.conf vi etc/php-fpm.conf
修改
user = www-data
group = www-data
若是www-data用戶不存在,那麼先添加www-data用戶
groupadd www-data
useradd -g www-data www-data
2、編譯安裝nginx
而後按照http://www.nginx.cn/install 安裝nginx
3、修改nginx配置文件以支持php-fpm
nginx安裝完成後,修改nginx配置文件爲,nginx.conf
其中server段增長以下配置,注意標紅內容配置,不然會出現No input file specified.錯誤
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ .php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
4、建立測試php文件
建立php文件
在/usr/local/nginx/html下建立index.php文件,輸入以下內容
<?php
echo phpinfo();
?>
5、啓動服務
啓動php-fpm和nginx
/usr/local/php/sbin/php-fpm #手動打補丁的啓動方式/usr/local/php/sbin/php-fpm start sudo /usr/local/nginx/nginx
php-fpm關閉重啓見文章結尾
6、瀏覽器訪問
訪問http://你的服務器ip/index.php,皆能夠見到php信息了。
php --ini 查看php.ini的路徑
打開
short_open_tag = On
安裝php-fpm時可能遇到的錯誤:
1. php configure時出錯
configure: error: XML configuration could not be found
apt-get install libxml2 libxml2-dev (ubuntu下) yum -y install libxml2 libxml2-devel(centos下)
2. Please reinstall the BZip2 distribution
wget http://www.bzip.org/1.0.5/bzip2-1.0.5.tar.gz tar -zxvf bzip2-1.0.5.tar.gz cd bzip2-1.0.5 make make install
3. php的配置文件中有一行--with-mysql=/usr。
安裝的時候提示:
configure: error: Cannot find MySQL header files under yes.
Note that the MySQL client library is not bundled anymore.
這是因爲安裝mysql時沒有安裝mysql頭文件,或者是路徑指定不正確,php找不到mysql的頭文件引發的錯誤提示。
解決方法。
(1.) 查看你的系統有沒有安裝mysql header
find / -name mysql.h
若是有。請指定--with-mysql=/跟你的正常路徑。
若是沒有。請看下一步。
(2.)redhat安裝
rpm -ivh MySQL-devel-4.1.12-1.i386.rpm
(3.)ubuntu安裝
apt-get install libmysqlclient15-dev
(4.)最後一步php的配置選項添加--with-mysql=/usr便可!
4.No input file specified.
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
5. 若是php configure時缺庫,能夠先安裝庫(ubuntu下)
sudo apt-get install make bison flex gcc patch autoconf subversion locate
sudo apt-get install libxml2-dev libbz2-dev libpcre3-dev libssl-dev zlib1g-dev libmcrypt-dev libmhash-dev libmhash2 libcurl4-openssl-dev libpq-dev libpq5 libsyck0-dev
6. mcrypt.h not found. Please reinstall libmcrypt
apt-get install libmcrypt-dev
或者
cd /usr/local/src
wget http://softlayer.dl.sourceforge.net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd /usr/local/src/libmcrypt-2.5.8
./configure --prefix=/usr/local
make
make install
7. php-fpm 5.4.7 如何關閉 重啓?
php 5.4.7 下的php-fpm 再也不支持 php-fpm 之前具備的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,須要使用信號控制:
master進程能夠理解如下信號
INT, TERM 馬上終止 QUIT 平滑終止 USR1 從新打開日誌文件 USR2 平滑重載全部worker進程並從新載入配置和二進制模塊
示例:
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進程數:
ps aux | grep -c php-fpm
8.命令行下執行php,提示找不到命令
-bash: /usr/bin/php: No such file or directory
vi /etc/profile
在文件底部增長一行配置
export PATH=/usr/local/php/bin:$PATH
保存退出
source /etc/profile