最近開發項目,想用到php開發一個web服務,用於服務器監控管理頁面,所以開始學習php,學習之前先把自己的php開發環境弄好。
整個開發環境採用是nginx + mysql + php + xdebug + IDE(NetBeans) , 其中nginx + mysql + php 是部署在linux服務器,IDE(NetBeans)安裝在自己windows PC上。
1、nginx
nginx 是經常用到的服務器,安裝起來很簡單。下載nginx 源碼,還有我需要的一些開源庫 openssl,zlib,pcre,其中pcre是必須項。
openssl,zlib,pcre 這三個編譯步驟都是 切換到相應的源碼目錄, ./configure,然後是make
nginx 是 ./configure --prefix=/opt/nginx --sbin-path=/opt/nginx/sbin --conf-path=/opt/nginx/conf/nginx.conf --with-pcre=pcre的源碼目錄 --with-zlib=zlib源碼目錄 --with-http_ssl_module --with-openssl=openssl源碼目錄, make, make install,install 後執行程序會安裝到指定的--prefix=/opt/nginx 目錄下。
安裝好後整個nginx項目都會在/opt/nginx目錄下,我們打開 /opt/nginx/conf/nginx.conf 配置文件,配置nginx。配置如下:
server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 配置所有匹配到 正則表達式 \.php$ 成功的請求,轉發到 127.0.0.1:3344這個程序上, 3344 被php-fpm所監聽
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:3344; #這個php監聽端口可以任意指定
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; #/scripts$fastcgi_script_name;
include fastcgi_params;
}
}
這裏我們是使用 fastcgi 方式,使nginx 和 php 解釋器工作。
2、mysql
./configure --prefix=/usr/local ,這裏 --prefix=/usr/local 最好要指定, 一會編譯php時,我們需要指定mysql庫目錄。 然後make 和 make install。
安裝好後, 我建議修改 /etc/my.cnf 中datadir=/data0/mysql,將數據庫文件指定到大的磁盤空間上。
3、編譯php
./configure --prefix=/opt/php --with-mysql=/usr/local/ --enable-fastcgi --with-mysql-sock=/var/lib/mysql/mysql.sock --enable-fpm,然後是make 和 make install
緊接着我們將 php 源碼目錄中的 php-fpm.conf 和 php.ini-development(或者php.ini-production)copy 到 /opt/php/sbin 目錄下,文件名分別爲php-fpm.conf和php.ini
這裏我要說明一下 php.ini-development 是php解釋器用於開發時候的配置文件, php.ini-production就是正式上線後的配置文件。
php-fpm.conf 是 程序 php-fpm 需要用到的配置文件, php-fpm 是一個進程管理者,它會啓動若干個php-cgi,用於和nginx 進行 fastcgi 交互。
我們修改一下php-fpm.conf 中的listen = 127.0.0.1:3344使 php-fpm監聽3344端口,和 nginx配置一致。
4、xdebug
xdebug 是php的調試工具,可以打印出具體的出錯函數堆棧,具體的信息可以網上google,xebug源碼目錄本身沒有configure,需要 phpize幫助生成,將/opt/php/bin/phpize copy 到 xdebug源碼目錄, /opt/php 是我php的安裝目錄。生成 configure 後 ,執行./configure --with-php-config=/opt/php/bin/php-config,然後make 和 make install。
下面是修改 php.ini 配置文件,在文件加上下面三句話:
[Xdebug]
xdebug.remote_enable=on
zend_extension=/opt/php/lib/php/extensions/no-debug-non-zts-20121212/xdebug.so
xdebug.so 爲xdebug的安裝目錄
5、安裝NetBeans
下載後安裝NetBeans,成功後 我們按照這個網址https://netbeans.org/kb/docs/php/quickstart_zh_CN.html學習如何使用netbeans,但是和這個鏈接裏不同的是我們的php環境都部署在linux 服務器上,所以在新建項目的時候,在這個界面下: