上一篇:搭建LNAMP環境(三)- 源碼安裝Apache2.4php
1.yum安裝編譯php須要的包html
yum -y install libxml2 libxml2-devel curl-devel libpng-devel freetype-devel libmcrypt-devel libjpeg-devel
2.建立php用戶組和用戶mysql
groupadd php
useradd -r -g php -s /sbin/nologin -M php
3.下載php源碼包,將源碼包放到/usr/local/src/目錄下
下載頁面:http://www.php.net/downloads.php
這裏用的是 php-7.0.13.tar.gz
下載地址:http://cn2.php.net/distributions/php-7.0.13.tar.gznginx
4.進入src/目錄web
cd /usr/local/src/
5.解壓源碼包sql
tar -zxf php-7.0.13.tar.gz
6.進入php源碼目錄,編譯安裝(配置參數:http://php.net/manual/zh/configure.about.php)apache
cd php-7.0.13/ ./configure \ --prefix=/usr/local/php \ --with-mcrypt=/usr/include \ --with-apxs2=/usr/local/apache/bin/apxs \ --with-config-file-path=/usr/local/php/etc \ --with-mysql-sock=/var/lib/mysql/mysql.sock \ --enable-fpm \ --enable-ftp \ --enable-xml \ --enable-zip \ --enable-soap \ --enable-pcntl \ --enable-shmop \ --enable-bcmath \ --enable-shared \ --enable-mysqlnd \ --enable-opcache \ --enable-session \ --enable-sockets \ --enable-sysvsem \ --enable-mbregex \ --enable-mbstring \ --enable-gd-native-ttf \ --enable-inline-optimization \ --with-gd \ --with-curl \ --with-zlib \ --with-mhash \ --with-iconv \ --with-xmlrpc \ --with-gettext \ --with-jpeg-dir \ --with-freetype-dir \ --with-fpm-user=nginx \ --with-fpm-group=nginx \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-openssl \ --without-gdbm \ --without-pear \ --disable-debug \ --disable-rpath \ --disable-fileinfo make && make install
7.源碼安裝包有兩個php.ini-xxx文件,一個是產品模式,一個是開發模式。根據需求選擇一個更名爲php.ini放到php/etc/目錄下vim
cp /usr/local/src/php-7.0.13/php.ini-development /usr/local/php/etc/php.ini
8.修改php目錄權限api
chown -R php:php /usr/local/php
9.將php命令加入環境變量,修改profile文件session
vim /etc/profile
10.修改成下面內容,保存退出
PATH=/usr/local/mysql/bin:/usr/local/php/bin:$PATH
11.使/etc/profile裏的配置當即生效
source /etc/profile
1.修改apache配置文件httpd.conf
vim /usr/local/apache/conf/httpd.conf
2.找到下面位置,加入紅色標記部分,保存退出
DirectoryIndex index.html index.php AddType application/x-compress .Z AddType application/x-gzip .gz .tgz Addtype application/x-httpd-php .php .phtml
3.重啓apache
service httpd restart
4.在apache的web目錄下添加php文件,如/usr/local/apache/htdocs/phpinfo.php
<?php phpinfo();
訪問URL,如:http://192.168.8.9:8088/phpinfo.php
頁面顯示正常,則配置成功
1.配置php-fpm
php-fpm.conf是 php-fpm 進程服務的配置文件
www.conf這是 php-fpm 進程服務的擴展配置文件
cd /usr/local/php/etc mv php-fpm.conf.default php-fpm.conf mv php-fpm.d/www.conf.default php-fpm.d/www.conf
2.將php-fpm服務腳本加入到init.d/目錄
cp /usr/local/src/php-7.0.13/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
3.爲php-fpm添加可執行權限
chmod +x /etc/init.d/php-fpm
4.將php-fpm加入系統服務
chkconfig --add php-fpm
5.修改服務的默認啓動等級
chkconfig php-fpm on
6.啓動php-fpm
service php-fpm start
7.修改nginx配置文件nginx.conf
vim /usr/local/nginx/conf/nginx.conf
8.把前面的#註釋符號去掉,把/script改成$document_root,保存退出
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; }
9.重啓nginx
service nginx restart
10.在nginx的web目錄下添加php文件,如/usr/local/nginx/html/phpinfo.php
<?php phpinfo();
訪問URL,如:http://192.168.8.9/phpinfo.php
頁面顯示正常,則配置成功
在web目錄下添加php文件,如/usr/local/apache/htdocs/mysql.php 或 /usr/local/nginx/html/mysql.php
<?php $host = 'localhost'; $user = 'root'; $pass = 'root123'; $database = 'mysql'; $sql = 'SELECT * FROM user LIMIT 1'; /*鏈接MySQLi*/ try { $con = mysqli_connect($host, $user, $pass, $database); $result = mysqli_query($con, $sql); //獲取數據 $data = mysqli_fetch_all($result, MYSQLI_ASSOC); print_r($data); //釋放結果集 mysqli_free_result($result); mysqli_close($con); } catch (Exception $e) { print "MySQLi Error: " . $e->getMessage() . "<br/>"; } echo "<hr>"; /*鏈接PDO*/ try { $dbh = new PDO("mysql:host=$host;dbname=$database", $user, $pass); foreach ($dbh->query($sql) as $row) { print_r($row); } $dbh = null; } catch (PDOException $e) { print "PDO Error: " . $e->getMessage() . "<br/>"; }
訪問URL,如:http://192.168.8.9/mysql.php
頁面顯示正常,則配置成功
PHP7安裝完畢!