1. 程式人生 > >centos7.2編譯安裝php7.2 nginx

centos7.2編譯安裝php7.2 nginx

    • 環境

    • 系統:Centos7.2 
    • 服務:Nginx

 

1:下載PHP7.0.2的安裝包解壓,編譯,安裝:

?

1

2

3

4

$ cd /usr/src/

$ wget http://cn2.php.net/distributions/php-7.0.2.tar.gz

$ tar -xzxvf php-7.0.2.tar.gz

$ cd php-7.0.2

1.1編譯前檢查

請檢查是否安裝了gcc ,沒有的話執行yum install gcc

檢查是否安裝了libxml2 ,沒有的話執行yum install libxml2

檢查是否安裝了libxml2-devel,沒有的話執行yum install libxml2-devel

注:因為改為用nginx了,所以編譯引數中的--with-apxs2=/usr/bin/apxs去掉了,如果要配置apache用,安裝PHP前,請先安裝apache。*

2:編譯引數配置

 

複製程式碼程式碼如下:

./configure --prefix=/usr/local/php7.2 --with-pdo-pgsql --with-zlib-dir --with-freetype-dir --enable-mbstring --with-libxml-dir=/usr --enable-soap --enable-calendar --with-curl  --with-gd --with-pgsql --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --enable-exif --enable-bcmath --with-mhash --enable-zip --with-pcre-regex --with-pdo-mysql --with-mysqli --with-jpeg-dir=/usr --with-png-dir=/usr  --with-openssl --with-fpm-user=www-data --with-fpm-group=www-data --with-libdir=/lib/x86_64-linux-gnu/ --enable-ftp --with-gettext --with-xmlrpc --with-xsl --enable-opcache --enable-fpm --with-iconv --with-xpm-dir=/usr

 

 

3:錯誤集合

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

報錯 Cannot find OpenSSL's <evp.h>

 執行 yum install openssl openssl-devel

 

報錯 Please reinstall the libcurl distribution

 執行 yum -y install curl-devel

 

錯誤 jpeglib.h not found

 執行 yum install libjpeg.x86_64 libpng.x86_64 freetype.x86_64 libjpeg-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 -y

 和執行 yum install libjpeg-devel

 

錯誤:checking for BZip2 in default path... not found configure: error: Please reinstall the BZip2 distribution 這是bzip2軟體包沒有安裝

 執行 yum install bzip2-devel.x86_64 -y

 

錯誤:configure: error: xpm.h not found.

 執行 yum install libXpm-devel

 

錯誤: Unable to locate gmp.h

 執行 yum install gmp-devel

 

錯誤:Unable to detect ICU prefix or /usr//bin/icu-config failed. Please verify ICU install prefix and make sure icu-config works

 執行 yum install -y icu libicu libicu-devel

 

錯誤:mcrypt.h not found. Please reinstall libmcrypt.

 執行 yum install php-mcrypt libmcrypt libmcrypt-devel

 

錯誤: configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path

 執行 yum install postgresql-devel

 

錯誤: configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution

 執行 yum install libxslt-devel

4:編譯

?

1

make clean && make && make install

5:PHP配置

5.1安裝完成後,我們要把原始碼包中的配置檔案複製到PHP安裝目錄下,原始碼包中有兩個配置 php.ini-development php.ini-production ,看名字就知道,一個是開發環境,一個是生產環境,我們這裡就複製開發環境的

?

1

cp php.ini-development /usr/local/php/lib/php.ini

5.2另外還需要設定環境變數 :修改/etc/profile檔案使其永久性生效,並對所有系統使用者生效,在檔案末尾加上如下兩行程式碼

?

1

2

PATH=$PATH:/usr/local/php/bin

export PATH

5.3 然後執行生效命令

?

1

source /etc/profile

5.4檢視PHP版本資訊

?

1

php -v

6:配置PHP-fpm

?

1

2

3

4

5

6

7

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

 

cp /usr/src/php-7.0.2/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

 

chmod +x /etc/init.d/php-fpm

6.1 啟動php-fpm:

?

1

/etc/init.d/php-fpm start

6.2 如果出現錯誤:ERROR: [pool www] cannot get uid for user 'www-data'

則新建www-data 使用者組:

?

1

2

groupadd www-data

useradd -g www-data www-data

6.3 重啟php-fpm

?

1

/etc/init.d/php-fpm restart

安裝所需環境

Nginx 是 C語言 開發,建議在 Linux 上執行,當然,也可以安裝 Windows 版本,本篇則使用 CentOS 7 作為安裝環境。

 

一. gcc 安裝
安裝 nginx 需要先將官網下載的原始碼進行編譯,編譯依賴 gcc 環境,如果沒有 gcc 環境,則需要安裝:

yum install gcc-c++

二. PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 相容的正則表示式庫。nginx 的 http 模組使用 pcre 來解析正則表示式,所以需要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也需要此庫。命令:

yum install -y pcre pcre-devel

三. zlib 安裝
zlib 庫提供了很多種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,所以需要在 Centos 上安裝 zlib 庫。

yum install -y zlib zlib-devel

四. OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼演算法、常用的金鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程式供測試或其它目的使用。
nginx 不僅支援 http 協議,還支援 https(即在ssl協議上傳輸http),所以需要在 Centos 安裝 OpenSSL 庫。

yum install -y openssl openssl-devel

官網下載

1.直接下載.tar.gz安裝包,地址:https://nginx.org/en/download.html

 

2.使用wget命令下載(推薦)。確保系統已經安裝了wget,如果沒有安裝,執行 yum install wget 安裝。

wget -c https://nginx.org/download/nginx-1.12.0.tar.gz

 

我下載的是1.12.0版本,這個是目前的穩定版。

解壓

依然是直接命令:

tar -zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0

配置

其實在 nginx-1.12.0 版本中你就不需要去配置相關東西,預設就可以了。當然,如果你要自己配置目錄也是可以的。
1.使用預設配置

./configure

2.自定義配置(不推薦)

./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:將臨時檔案目錄指定為/var/temp/nginx,需要在/var下建立temp及nginx目錄

編譯安裝

make
make install

查詢安裝路徑:

whereis nginx

啟動、停止nginx

cd /usr/local/nginx/sbin/
./nginx 
./nginx -s stop
./nginx -s quit
./nginx -s reload

啟動時報80埠被佔用:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
 

 解決辦法:1、安裝net-tool 包:yum install net-tools

 

./nginx -s quit:此方式停止步驟是待nginx程序處理任務完畢進行停止。
./nginx -s stop:此方式相當於先查出nginx程序id再使用kill命令強制殺掉程序。

查詢nginx程序:

ps aux|grep nginx

重啟 nginx

1.先停止再啟動(推薦):
對 nginx 進行重啟相當於先停止再啟動,即先執行停止命令再執行啟動命令。如下:

./nginx -s quit
./nginx

2.重新載入配置檔案:
當 ngin x的配置檔案 nginx.conf 修改後,要想讓配置生效需要重啟 nginx,使用-s reload不用先停止 ngin x再啟動 nginx 即可將配置資訊在 nginx 中生效,如下:
./nginx -s reload

啟動成功後,在瀏覽器可以看到這樣的頁面:

nginx-welcome.png

開機自啟動

即在rc.local增加啟動程式碼就可以了。

vi /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx
設定執行許可權:

chmod 755 rc.local

nginx-rclocal.png