Nginx+PHP+MySQL安裝參考
CentOS7環境
配置CentOS7網絡:
CentOS(最小安裝)默認是不打開網絡的
啟動網絡
vi打開:/etc/sysconfig/network-scripts/ifcfg-ens33 文件
將 “ONBOOT:no”屬性修改為:“ONBOOT:yes”
重啟網絡服務
# sudo service network restart?
查看IP
# ip addr
Nginx服務:
安裝依賴包
? ? 安裝:
? ? # yum -y install build-essential
? ??
? ? 安裝:更多依賴包
? ? # yum -y install gcc automake autoconf libtool make
? ? 安裝:g++
? ? # yum -y install gcc gcc-c++
安裝PCRE庫
選定源碼目錄 ==> ftp下載PCRE庫 ==> 安裝PCRE庫
# cd /usr/local/src
# wget ftp://ftp.pcre.org/pub/pcre/pcre-8.42.tar.gz
# tar -zxvf pcre-8.42.tar.gz
# cd ./pcre-8.42
# ./configure
# make && make install
安裝zlib源碼包
下載zlib源碼包 ==> 安裝zlib包
# cd /usr/local/src
# wget http://zlib.net/zlib-1.2.11.tar.gz
# tar -zxvf zlib-1.2.11.tar.gz
# cd zlib-1.2.11
# ./configure
# make && make install
安裝openssl源碼包
# cd /usr/local/src
# wget https://www.openssl.org/source/openssl-1.1.0k.tar.gz
# tar -zxvf openssl-1.1.0k.tar.gz
# cd ./openssl-1.1.0k
# mkdir /usr/local/openssl
# ./configure --prefix=/usr/local/openssl
# make && make install
安裝Nginx
# cd /usr/local/src
# wget http://nginx.org/download/nginx-1.16.1.tar.gz
# tar -zxvf nginx-1.16.1.tar.gz
# cd nginx-1.16.1
# groupadd -r nginx
# useradd -s /bin/false -M nginx -g nginx
# ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_mp4_module? \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre=/usr/local/src/pcre-8.42 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.1.0k \
# make
執行make 進行編譯,如果編譯成功的話會在objs中出現一個nginx文件
特別注意:
在已安裝的nginx上進行添加模塊的話執行到這里就行了,把objs中的nginx替換掉之前的安裝的nginx/sbin/中的nginx文件,
然后重啟nginx就行了,如果執行下一步的install,會導致之前安裝的nginx被覆蓋,比如之前配置好的nginx.conf文件
# make install
參數說明:
--prefix 用于指定nginx編譯后的安裝目錄
--add-module 為添加的第三方模塊,如添加fdfs的nginx模塊,則--add-module=/home/fastdfs-nginx-module/src
--with..._module 表示啟用的nginx模塊,如此處啟用了http_ssl_module模塊
可能出現的錯誤:
出現:./configure:? error: the HTTP rewrite module requires the PCRE library.
解決方法:yum -y install pcre-devel
出現:SSL modules require the OpenSSL library
解決方法:yum install openssl-devel?
或是下載源碼,指定對應路徑,如:
--with-pcre=/usr/local/src/pcre-8.41 指的是pcre-8.42 的源碼路徑
--with-zlib=/usr/local/src/zlib-1.2.11 指的是zlib-1.2.11 的源碼路徑
--with-openssl=/usr/local/src/openssl-1.1.0g 指的是openssl-1.1.0k 的源碼路徑
nginx編譯選項
make是用來編譯的,它從Makefile中讀取指令,然后編譯。
make install是用來安裝的,它也從Makefile中讀取指令,安裝到指定的位置。
configure命令是用來檢測你的安裝平臺的目標特征的。它定義了系統的各個方面,包括nginx的被允許使用的連接處理的方法,比如它會檢測你是不是有CC或GCC,
并不是需要CC或GCC,它是個shell腳本,執行結束時,它會創建一個Makefile文件。nginx的configure命令支持以下參數:
? ? --prefix=*path* 定義一個目錄,存放服務器上的文件 ,也就是nginx的安裝目錄。默認使用 /usr/local/nginx。
? ? --sbin-path=*path* 設置nginx的可執行文件的路徑,默認為 *prefix*/sbin/nginx.
? ? --conf-path=*path* 設置在nginx.conf配置文件的路徑。nginx允許使用不同的配置文件啟動,通過命令行中的-c選項。默認為*prefix*/conf/nginx.conf.
? ? --pid-path=*path* 設置nginx.pid文件,將存儲的主進程的進程號。安裝完成后,可以隨時改變的文件名 , 在nginx.conf配置文件中使用 PID指令。默認情況下,文件名 為``*prefix*/logs/nginx.pid.
? ? --error-log-path=*path* 設置主錯誤,警告,和診斷文件的名稱。安裝完成后,可以隨時改變的文件名 ,在nginx.conf配置文件中 使用 的error_log指令。默認情況下,文件名 為*prefix*/logs/error.log.
? ? --http-log-path=*path* 設置主請求的HTTP服務器的日志文件的名稱。安裝完成后,可以隨時改變的文件名 ,在nginx.conf配置文件中 使用 的access_log指令。默認情況下,文件名 為*prefix*/logs/access.log.
? ? --user=*name* 設置nginx工作進程的用戶。安裝完成后,可以隨時更改的名稱在nginx.conf配置文件中 使用的 user指令。默認的用戶名是nobody。
? ? --group=*name* 設置nginx工作進程的用戶組。安裝完成后,可以隨時更改的名稱在nginx.conf配置文件中 使用的 user指令。默認的為非特權用戶。
? ? --with-select_module --without-select_module 啟用或禁用構建一個模塊來允許服務器使用select()方法。該模塊將自動建立,如果平臺不支持的kqueue,epoll,rtsig或/dev/poll。
? ? --with-poll_module --without-poll_module 啟用或禁用構建一個模塊來允許服務器使用poll()方法。該模塊將自動建立,如果平臺不支持的kqueue,epoll,rtsig或/dev/poll。
? ? --without-http_gzip_module — 不編譯壓縮的HTTP服務器的響應模塊。編譯并運行此模塊需要zlib庫。
? ? --without-http_rewrite_module 不編譯重寫模塊。編譯并運行此模塊需要PCRE庫支持。
? ? --without-http_proxy_module — 不編譯http_proxy模塊。
? ? --with-http_ssl_module — 使用https協議模塊。默認情況下,該模塊沒有被構建。建立并運行此模塊的OpenSSL庫是必需的。
? ? --with-pcre=*path* — 設置PCRE庫的源碼路徑。PCRE庫的源碼(版本4.4 - 8.30)需要從PCRE網站下載并解壓。其余的工作是Nginx的./ configure和make來完成。正則表達式使用在location指令和 ngx_http_rewrite_module 模塊中。
? ? --with-pcre-jit —編譯PCRE包含“just-in-time compilation”(1.1.12中, pcre_jit指令)。
? ? --with-zlib=*path* —設置的zlib庫的源碼路徑。要下載從 zlib(版本1.1.3 - 1.2.5)的并解壓。其余的工作是Nginx的./ configure和make完成。ngx_http_gzip_module模塊需要使用zlib 。
? ? --with-cc-opt=*parameters* — 設置額外的參數將被添加到CFLAGS變量。例如,當你在FreeBSD上使用PCRE庫時需要使用:--with-cc-opt="-I /usr/local/include。.如需要需要增加 select()支持的文件數量:--with-cc-opt="-D FD_SETSIZE=2048".
? ? --with-ld-opt=*parameters* —設置附加的參數,將用于在鏈接期間。例如,當在FreeBSD下使用該系統的PCRE庫,應指定:--with-ld-opt="-L /usr/local/lib".
安裝完成后,按照安裝的參數,安裝的啟動目錄在/usr/local/nginx
[root@localhost nginx]# ls -l
總用量 76
drwxr-xr-x. 2 root root 4096 9月? ?8 09:46 conf
-rw-r--r--. 1 root root 1077 9月? ?8 10:34 fastcgi.conf
-rw-r--r--. 1 root root 1077 9月? ?8 10:34 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 9月? ?8 10:34 fastcgi_params
-rw-r--r--. 1 root root 1007 9月? ?8 10:34 fastcgi_params.default
drwxr-xr-x. 2 root root? ?40 9月? ?8 09:46 html
-rw-r--r--. 1 root root 2837 9月? ?8 10:34 koi-utf
-rw-r--r--. 1 root root 2223 9月? ?8 10:34 koi-win
drwxr-xr-x. 2 root root? ?41 9月? ?8 10:37 logs
-rw-r--r--. 1 root root 5231 9月? ?8 10:34 mime.types
-rw-r--r--. 1 root root 5231 9月? ?8 10:34 mime.types.default
-rw-r--r--. 1 root root 2656 9月? ?8 10:34 nginx.conf
-rw-r--r--. 1 root root 2656 9月? ?8 10:34 nginx.conf.default
-rw-r--r--. 1 root root? ? 6 9月? ?8 10:37 nginx.pid
drwxr-xr-x. 2 root root? ?36 9月? ?8 10:34 sbin
-rw-r--r--. 1 root root? 636 9月? ?8 10:34 scgi_params
-rw-r--r--. 1 root root? 636 9月? ?8 10:34 scgi_params.default
-rw-r--r--. 1 root root? 664 9月? ?8 10:34 uwsgi_params
-rw-r--r--. 1 root root? 664 9月? ?8 10:34 uwsgi_params.default
-rw-r--r--. 1 root root 3610 9月? ?8 10:34 win-utf
[root@localhost nginx]# pwd
/usr/local/nginx
啟動Nginx服務:
由于CentOS-7防火墻不開發端口,所以在本地測試中,可以選擇關閉防火墻或者允許開發80端口
CentOS防火墻
# systemctl status firewalld? ? ?==> 防火墻狀態
# systemctl start firewalld? ? ? ?==> 開啟防火墻
# systemctl stop firewalld? ? ? ? ==> 關閉防火墻
# systemctl restart firewalld? ? ?==> 重啟防火墻
# firewall-cmd --reload? ? ? ? ? ==> 防火墻重載
# firewall-cmd --permanent --zone=public --add-port=80/tcp
? ? permanent: 永久有效
? ? zone:作用域
? ? --add-port=80/tcp:添加-端口=端口/通信協議
開放端口或關閉防火墻后就可以啟動nginx服務
服務啟動
[root@localhost nginx]# netstat -ano | grep 80
tcp? ? ? ? 0? ? ? 0 0.0.0.0:80? ? ? ? ? ? ? 0.0.0.0:*? ? ? ? ? ? ? ?LISTEN? ? ? off (0.00/0/0)
unix? 3? ? ? [ ]? ? ? ? ?STREAM? ? ?CONNECTED? ? ?80900? ??
unix? 3? ? ? [ ]? ? ? ? ?STREAM? ? ?CONNECTED? ? ?80899? ??
[root@localhost nginx]# /usr/local/nginx/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
通過netstat查看端口網絡狀態,是否有服務占用80端口;通過調用nginx的啟動目錄實現nginx服務啟動
Nginx服務維護
為了避免每次開機手動啟動,可以使用命令腳本,注冊服務,開機自啟動等
創建nginx啟動命令腳本
#vi /etc/init.d/nginx
插入以下內容, 注意修改PATH和NAME字段, 匹配自己的安裝路徑 (這段是從網上copy的)
#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'
### BEGIN INIT INFO
# Provides:? ? ? ? ? nginx
# Required-Start:? ? $all
# Required-Stop:? ? ?$all
# Default-Start:? ? ?2 3 4 5
# Default-Stop:? ? ? 0 1 6
# Short-Description: starts the nginx web server
# Description:? ? ? ?starts nginx using start-stop-daemon
### END INIT INFO
# Author:? ?licess
# website:? http://lnmp.org
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
case "$1" in
? ? start)
? ? ? ? echo -n "Starting $NAME... "
? ? ? ? if netstat -tnpl | grep -q nginx;then
? ? ? ? ? ? echo "$NAME (pid `pidof $NAME`) already running."
? ? ? ? ? ? exit 1
? ? ? ? fi
? ? ? ? $NGINX_BIN -c $CONFIGFILE
? ? ? ? if [ "$?" != 0 ] ; then
? ? ? ? ? ? echo " failed"
? ? ? ? ? ? exit 1
? ? ? ? else
? ? ? ? ? ? echo " done"
? ? ? ? fi
? ? ? ? ;;
? ? stop)
? ? ? ? echo -n "Stoping $NAME... "
? ? ? ? if ! netstat -tnpl | grep -q nginx; then
? ? ? ? ? ? echo "$NAME is not running."
? ? ? ? ? ? exit 1
? ? ? ? fi
? ? ? ? $NGINX_BIN -s stop
? ? ? ? if [ "$?" != 0 ] ; then
? ? ? ? ? ? echo " failed. Use force-quit"
? ? ? ? ? ? exit 1
? ? ? ? else
? ? ? ? ? ? echo " done"
? ? ? ? fi
? ? ? ? ;;
? ? status)
? ? ? ? if netstat -tnpl | grep -q nginx; then
? ? ? ? ? ? PID=`pidof nginx`
? ? ? ? ? ? echo "$NAME (pid $PID) is running..."
? ? ? ? else
? ? ? ? ? ? echo "$NAME is stopped"
? ? ? ? ? ? exit 0
? ? ? ? fi
? ? ? ? ;;
? ? force-quit)
? ? ? ? echo -n "Terminating $NAME... "
? ? ? ? if ! netstat -tnpl | grep -q nginx; then
? ? ? ? ? ? echo "$NAME is not running."
? ? ? ? ? ? exit 1
? ? ? ? fi
? ? ? ? kill `pidof $NAME`
? ? ? ? if [ "$?" != 0 ] ; then
? ? ? ? ? ? echo " failed"
? ? ? ? ? ? exit 1
? ? ? ? else
? ? ? ? ? ? echo " done"
? ? ? ? fi
? ? ? ? ;;
? ? restart)
? ? ? ? $0 stop
? ? ? ? sleep 1
? ? ? ? $0 start
? ? ? ? ;;
? ? reload)
? ? ? ? echo -n "Reload service $NAME... "
? ? ? ? if netstat -tnpl | grep -q nginx; then
? ? ? ? ? ? $NGINX_BIN -s reload
? ? ? ? ? ? echo " done"
? ? ? ? else
? ? ? ? ? ? echo "$NAME is not running, can't reload."
? ? ? ? ? ? exit 1
? ? ? ? fi
? ? ? ? ;;
? ? configtest)
? ? ? ? echo -n "Test $NAME configure files... "
? ? ? ? $NGINX_BIN -t
? ? ? ? ;;
? ? *)
? ? ? ? echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
? ? ? ? exit 1
? ? ? ? ;;
esac
設置執行權限
chmod a+x /etc/init.d/nginx
注冊成服務
chkconfig --add nginx
設置開機啟動
chkconfig nginx on
重啟
shutdown -h 0 -r
查看nginx服務是否自動啟動
ss -apn|grep nginx
對nginx服務執行停止/啟動/重新讀取配置文件操作
#啟動nginx服務
systemctl start nginx.service
#停止nginx服務
systemctl stop nginx.service
#重啟nginx服務
systemctl restart nginx.service
#重新讀取nginx配置(這個最常用, 不用停止nginx服務就能使修改的配置生效)
systemctl reload nginx.service
如出現:nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
則需通過nginx –c ../conf/nginx.conf? ? 命令指定nginx的配置
關于nginx.conf配置文件
在安裝完nginx后會在conf目錄中產生一個nginx.conf的配置文件
里面有些默認配置,可根據自己的需求進行更改
示例:
#user? nobody;
worker_processes? 1;
events {
? ? ?use epoll;
? ? ? ? worker_connections? 51200;
}
http {
? ? include? ? ? ?mime.types;
? ? default_type? application/octet-stream;
? ? sendfile? ? ? ? on;
? ? keepalive_timeout? 65;
? ? proxy_connect_timeout 600s;
? ? proxy_read_timeout 600s;
? ? proxy_send_timeout 600s;
? ? proxy_buffer_size 64k;
? ? proxy_buffers 4 32k;
? ? proxy_busy_buffers_size 64k;
? ? proxy_temp_file_write_size 64k;
? ? proxy_ignore_client_abort on;
? ? client_max_body_size 200m;? #此參數在使用fdfs上傳可控制上傳文件的大小
#日志的輸出格式,如需打印請求的body參數信息,可在$body_bytes_sent后添加 $request_body?
log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '
? ? ? ? ? ? ? ? ?'$status $body_bytes_sent "$http_referer" '
? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"';
?
access_log? logs/access.log? main; #設置日志輸出的位置
access_log on;? #是否開啟日志,開啟on,關閉off
#負載均衡配置
upstream test.com {
ip_hash;
server 192.168.68.9:8080;
server 192.168.68.72:8080;
}
? ? server {
? ? ? ? listen? ? ? ?80; #監聽的端口,http默認監聽端口為80
? ? ? ? server_name? localhost; #監聽的主機名
? ? ? ? location / {
#設置請求的頭部中主機名為請求的主機名,而不是代理的nginx的主機名
? ? ? proxy_set_header Host $host:$server_port;
#代理的目標地址,如果要進行負載均衡,目標地址可添test.com
? ? ? ? ?proxy_pass http://192.168.68.9:8080;
? ? ? ? }
? ? ? ? error_page? ?500 502 503 504? /50x.html;
? ? ? ? location = /50x.html {
? ? ? ? ? ? root? ?html;
? ? ? ? }
? ? }
#https配置,https默認監聽端口為443
? ? server {
? ? ? ? listen? 443;
? ? ? ? server_name system.test.com;
? ? ? ? ssl on;
? ? ? ? ssl_certificate_key cert/system.key; #ssl key文件的位置,此處使用配置文件的相對路徑
? ? ? ? ssl_certificate cert/system.pem; #證書文件,此處為阿里云云盾證書生成的.pem也可修改擴展名為熟悉的.crt
? ? ? ? ssl_session_timeout 5m;
? ? ? ? ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
? ? ? ? ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
? ? ? ? ssl_prefer_server_ciphers on;
? ? ? ? location / {
? ? ? ? ? ? ?proxy_pass http://192.168.68.9:8080;
? ? ? ? }
? ? }
? ? #以下為我的fdfs文件配置,沒有使用fdfs可以不用配置
? ? server {
? ? ? ? listen? ? ? ?9300;? ? ? ??
? ? ? ? server_name? localhost;? ?
? ? #location /group1/M01 {
? ? ? ? ?#? ?root? ?/home/fdfs/storage2/data;
? ? ? ? ? #? ngx_fastdfs_module;? ? ? ? ?
? ? ? ? #}
? ? ? ??
? ? ? ? location /group1/M00 {
? ? ? ? ? ?root? ?/home/fdfs/storage1/data;
? ? ? ? ? ?ngx_fastdfs_module;
? ? ? ? }
? ? ? ? error_page? ?500 502 503 504? /50x.html;
? ? ? ? location = /50x.html {
? ? ? ? ? ? root? ?html;
? ? ? ? }
? ? }
#include vhost/*.conf;
}
關于https配置ssl
如果在安裝nginx的時候沒有安裝 --with-http_ssl_module模塊要先安裝該模塊
nginx –V 可查看已經安裝的模塊
添加一個https的server大概如下面這個樣子
server {
? ? ? ? ?listen 443;? ?##默認的監聽端口為443
? ? ? ? ?server_name? localhost;
? ? ? ? ?ssl on;
? ? ? ? ?ssl_certificate_key? xxx.key; ##私鑰
? ? ? ? ?ssl_certificate? xxx.crt; ##證書,證書中包含公鑰和私鑰加密后的簽名信息
?
? ? ? ? location / {
? ? ? ? ? ? root? ?html;
? ? ? ? ? ? index? index.html index.htm;
? ? ? ? ? ? ?proxy_pass http://xxx.xxx.xxx.xxx:xxx;
? ? ? ? }
? ? ? ? ?
? ? }
--------------------------------MySQL安裝---------------------------
--------------------------------PHP環境安裝------------------------------
編譯安裝php-fpm
PHP-FPM是一個PHP FastCGI管理器,是只用于PHP的
安裝依賴包
# yum -y install libmcrypt-devel mhash-devel libxslt-devel \
libjpeg libjpeg-devel libpng libpng-dvevl 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:
# cd /usr/local/src
# wget http://php.net/get/php-5.6.27.tar.gz/from/a/mirror
# tar -zxvf php-5.6.27.tar.gz?
# ./configure \
--prefix=/usr/local/php56 \
--with-config-file-path=/usr/local/php56/etc \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-opcache \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gettext \
--enable-mbstring \
--with-iconv \
--with-mcrypt \
--with-mhash \
--with-openssl \
--enable-bcmath \
--enable-soap \
--with-libxml-dir \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--with-curl \
--with-zlib \
--enable-zip \
--with-bz2 \
--with-readline \
--enable-gd-native-ttf \
--with-gd
#make
#make install
安裝的參數說明:
""" 安裝路徑 """
--prefix=/usr/local/php56 \
""" php.ini 配置文件路徑 """
--with-config-file-path=/usr/local/php56/etc \
""" 優化選項 """
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
""" 啟用 opcache,默認為 ZendOptimizer+(ZendOpcache) """
--enable-opcache \
""" FPM """
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
""" MySQL """
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
""" 國際化與字符編碼支持 """
--with-gettext \
--enable-mbstring \
--with-iconv \
""" 加密擴展 """
--with-mcrypt \
--with-mhash \
--with-openssl \
""" 數學擴展 """
--enable-bcmath \
""" Web 服務,soap 依賴 libxml """
--enable-soap \
--with-libxml-dir \
""" 進程,信號及內存 """
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
""" socket & curl """
--enable-sockets \
--with-curl \
""" 壓縮與歸檔 """
--with-zlib \
--enable-zip \
--with-bz2 \
""" GNU Readline 命令行快捷鍵綁定 """
--with-readline
""gd庫支持""
--enable-gd-native-ttf \
--with-gd
注:依賴包要同時安裝依賴包的本身,以及該依賴包的開發版,例如:zlib和zlib-devel。配置過程中可能會出現一些問題,
主要是依賴包沒有安裝開發板,或者安裝的依賴包版本過低導致的,更多編譯參數請使用 ./configure --help 查看。
執行configure時,發生報錯:
1. Please reinstall the libzip distribution?
刪除了舊版的libzip(“yum remove libzip”),之后下載libzip源碼包進行本地執行configure+make&&make install
libzip的安裝:
# cd /usr/local/src
# wget https://libzip.org/download/libzip-1.5.2.tar.gz
# tar -zxf libzip-1.2.0.tar.gz
# cd libzip-1.2.0
# ./configure
# make && make install
2. configure: error: mcrypt.h not found. Please reinstall libmcrypt
libmcrypt的安裝:
# cd /usr/local/src
# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
# tar -zxvf libmcrypt-2.5.7.tar.gz
# cd libmcrypt-2.5.7
# ./configure --prefix=/usr/local
# make && make install
3. Don't know how to define struct flock on this system, set --enable-opcache=no
查看--enble-opchahe的作用
#./configure --help|grep opcache--enable-opcache
Enable Zend OPcache support
可知這個是與 php 的加速模塊 zend相關的,不能省略
vim /etc/ld.so.conf.d/local.conf? ? ?# 編輯庫文件
/usr/local/lib
/usr/local/lib64
:wq? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 保存退出
ldconfig -v? ? ? ? ? ? ? ? ? ? ? ? ? # 使之生效
4. Please reinstall readline - I cannot find readline.h
yum -y install readline-devel
至此!PHP-fpm安裝的基本流程結束了,下一步就需要配置文件
用戶配置文件
? ? 為php提供配置文件:php.ini
# cp php.ini-production /usr/local/php/lib/php.ini
? ? 為php-fpm提供配置文件
# cd /usr/local/php
# cp etc/php.fpm.conf.default etc/php-fpm.conf
# vi etc/php-fpm.conf
vi打開php-fpm.conf文件:
將文件的尾部的索引;修改成實際的目錄
include=/usr/local/php/etc/php-fpm.d/*.conf
根據參數--with-fpm-user=www,添加用戶和組:
useradd www
groupadd -g www www
默認情況下,etc/php-fpm.d/目錄下有一個“www .conf.defalut”用戶配置文件
# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# vi /usr/local/php/etc/php-fpm.d/www.conf
修改“www.conf"文件中的user和group的value;添加用戶和組
user = www
group = www
? ? 啟動php-fpm服務
# /usr/local/php/sbin/php-fpm
# ps aux | grep php-fpm [驗證服務啟動]
# netstat -tln | grep 9000 [驗證網絡端口是否使用]
[root@localhost /]# ps aux | grep php-fpm
root? ? ? 41831? 0.0? 0.3 221264? 6220 ?? ? ? ? Ss? ?08:54? ?0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
mirror? ? 41832? 0.0? 0.2 221264? 5748 ?? ? ? ? S? ? 08:54? ?0:00 php-fpm: pool www
mirror? ? 41833? 0.0? 0.2 221264? 5748 ?? ? ? ? S? ? 08:54? ?0:00 php-fpm: pool www
root? ? ? 41835? 0.0? 0.0 110292? ?916 pts/0? ? R+? ?08:54? ?0:00 grep --color=auto php-fpm
[root@localhost /]# netstat -tln | grep 9000
tcp? ? ? ? 0? ? ? 0 127.0.0.1:9000? ? ? ? ? 0.0.0.0:*? ? ? ? ? ? ? ?LISTEN? ?
至此!php-fpm服務啟動成功!
Nginx+PHP環境配置
? ? 打開nginx.conf(nginx配置文件)
[root@localhost nginx]# vi ./nginx.conf
#user? nobody;
worker_processes? 1;
#error_log? logs/error.log;
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info;
#pid? ? ? ? logs/nginx.pid;
events {
? ? worker_connections? 1024;
}
http {
? ? include? ? ? ?mime.types;
? ? default_type? application/octet-stream;
? ? #log_format? main? '$remote_addr - $remote_user [$time_local] "$request" '
? ? #? ? ? ? ? ? ? ? ? '$status $body_bytes_sent "$http_referer" '
? ? #? ? ? ? ? ? ? ? ? '"$http_user_agent" "$http_x_forwarded_for"';
? ? #access_log? logs/access.log? main;
? ? sendfile? ? ? ? on;
? ? #tcp_nopush? ? ?on;
? ? #keepalive_timeout? 0;
? ? keepalive_timeout? 65;
? ? #gzip? on;
? ? server {
? ? ? ? listen? ? ? ?80;
? ? ? ? server_name? localhost;
? ? ? ? #charset koi8-r;
? ? ? ? #access_log? logs/host.access.log? main;
? ? ? ? location / {
? ? ? ? ? ? root? ?html;
? ? ? ? ? ? index? index.html index.htm;
? ? ? ? }
? ? ? ? #error_page? 404? ? ? ? ? ? ? /404.html;
? ? ? ? # redirect server error pages to the static page /50x.html
? ? ? ? #
? ? ? ? error_page? ?500 502 503 504? /50x.html;
? ? ? ? location = /50x.html {
? ? ? ? ? ? root? ?html;
? ? ? ? }
? ? ? ? # proxy the PHP scripts to Apache listening on 127.0.0.1:80
? ? ? ? #
? ? ? ? #location ~ \.php$ {
? ? ? ? #? ? proxy_pass? ?http://127.0.0.1;
? ? ? ? #}
? ? ? ? # 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? /scripts$fastcgi_script_name;
? ? ? ? #? ? include? ? ? ? fastcgi_params;
? ? ? ? #}
? ? ? ? # deny access to .htaccess files, if Apache's document root
? ? ? ? # concurs with nginx's one
? ? ? ? #
? ? ? ? #location ~ /\.ht {
? ? ? ? #? ? deny? all;
? ? ? ? #}
? ? }
? ? # another virtual host using mix of IP-, name-, and port-based configuration
? ? #
? ? #server {
? ? #? ? listen? ? ? ?8000;
? ? #? ? listen? ? ? ?somename:8080;
? ? #? ? server_name? somename? alias? another.alias;
? ? #? ? location / {
? ? #? ? ? ? root? ?html;
? ? #? ? ? ? index? index.html index.htm;
? ? #? ? }
? ? #}
? ? # HTTPS server
? ? #
? ? #server {
? ? #? ? listen? ? ? ?443 ssl;
? ? #? ? server_name? localhost;
? ? #? ? ssl_certificate? ? ? cert.pem;
? ? #? ? ssl_certificate_key? cert.key;
? ? #? ? ssl_session_cache? ? shared:SSL:1m;
? ? #? ? ssl_session_timeout? 5m;
? ? #? ? ssl_ciphers? HIGH:!aNULL:!MD5;
? ? #? ? ssl_prefer_server_ciphers? on;
? ? #? ? location / {
? ? #? ? ? ? root? ?html;
? ? #? ? ? ? index? index.html index.htm;
? ? #? ? }
? ? #}
}
修改server配置塊中的location和php后端請求配置塊
? server {
? ? ? ? listen? ? ? ?80;
? ? ? ? server_name? localhost;
? ? ? ? #charset koi8-r;
? ? ? ? #access_log? logs/host.access.log? main;
? ? ? ? location / {
? ? ? ? ? ? root? ?html;
? ? ? ? ? ? index? index.html index.htm index.php
? ? ? ? }
? ? ? ? #error_page? 404? ? ? ? ? ? ? /404.html;
? ? ? ? # redirect server error pages to the static page /50x.html
? ? ? ? #
? ? ? ? error_page? ?500 502 503 504? /50x.html;
? ? ? ? location = /50x.html {
? ? ? ? ? ? root? ?html;
? ? ? ? }
? ? ? ? # proxy the PHP scripts to Apache listening on 127.0.0.1:80
? ? ? ? #
? ? ? ? #location ~ \.php$ {
? ? ? ? #? ? proxy_pass? ?http://127.0.0.1;
? ? ? ? #}
? ? ? ? # 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? /scripts$fastcgi_script_name;
? ? ? ? ? ? include? ? ? ? fastcgi_params;
? ? ? ? }
? ? ? ? # deny access to .htaccess files, if Apache's document root
? ? ? ? # concurs with nginx's one
? ? ? ? #
? ? ? ? #location ~ /\.ht {
? ? ? ? #? ? deny? all;
? ? ? ? #}
? ? }
在location配置塊中添加index.php首頁
php請求和后端php-fpm模塊進行通信,需要配置location ~\ .php$配置塊
? root:配置php程序文件的根目錄
*** 修改配置文件的第一行:”user“屬性為我們之前配置的用戶**,表示nginx的權限
至此!我們的Nginx和php的環境完成簡單的配置!
大功告成
啟動步驟:
? ? 啟動Nginx服務
? ? # /usr/local/nginx/sbin/nginx
? ? 啟動php-fpm服務
? ? # /usr/local/php/sbin/php-fpm 或 systemctl restart php-fpm
? ? 啟動mysql服務
? ? # systemctl start mysqld
phpinfo():
在Nginx的目錄html中添加一個php文件:”index.php“
<?php
? ? phpinfo();
?>
測試數據庫連接:
編寫一個連接數據庫行為的php文件:”mysql.php“
php和mysql之間的連接操作依靠的是”mysqli“
<?php
? ? $conn = mysqli_connect("127.0.0.1","root","926498");
? ? if(! $conn ) {
? ? ? ? echo "連接失敗".mysqli_connect_error();
? ? } else {
? ? ? ? ?echo "連接成功";
? ? }
?>
至此!PHP+Nginx+MySQL環境完成了基本的搭建!