CentOS7 配置 nginx php php-fpm

上一篇說到安裝 php

 裝完並無任何設置,這篇記錄一下設置。先設置 nginx 吧,nginx 網上多如繁星的設置但大都比較簡單,屬於基礎設置,所以此處只貼出設置後的結果,用紅色框表示一些本身改動或須要注意的地方php

nginx 的基礎設置很簡單,設置個三次以上都能直接記住了。本身配置了一下 關於400、500系列錯誤的默認顯示路徑,40四、403錯誤都顯示 40x.html,500 的幾個錯誤都顯示 50x.html 錯誤,這樣頁面就能本身定義了。好比如今 404 就會顯示這樣一個頁面html

啓動 nginx遇到了插曲

由於是編譯安裝,所以安裝後是這樣啓動的linux

cd /usr/local/nginx
sudo ./nginx

今天在寫配置時發現網上神句沒法啓動nginx

sudo systemctl start nginx

百度大法讓寫一個腳本,好吧,寫,打開 vimvim

#!/bin/sh 
# 
# nginx - this script starts and stops the nginx daemon 
# 
# chkconfig:   - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
#               proxy and IMAP/POP3 proxy server 
# processname: nginx 
# config:      /etc/nginx/nginx.conf 
# config:      /etc/sysconfig/nginx 
# pidfile:     /var/run/nginx.pid 

# Source function library. 
. /etc/rc.d/init.d/functions 

# Source networking configuration. 
. /etc/sysconfig/network 

# Check that networking is up. 
[ "$NETWORKING" = "no" ] && exit 0 

nginx="/usr/local/nginx/sbin/nginx" 
prog=$(basename $nginx) 

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 

lockfile=/var/lock/subsys/nginx 

start() { 
    [ -x $nginx ] || exit 5 
    [ -f $NGINX_CONF_FILE ] || exit 6 
    echo -n $"Starting $prog: " 
    daemon $nginx -c $NGINX_CONF_FILE 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && touch $lockfile 
    return $retval 
} 

stop() { 
    echo -n $"Stopping $prog: " 
    killproc $prog -QUIT 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && rm -f $lockfile 
    return $retval 
killall -9 nginx 
} 

restart() { 
    configtest || return $? 
    stop 
    sleep 1 
    start 
} 

reload() { 
    configtest || return $? 
    echo -n $"Reloading $prog: " 
    killproc $nginx -HUP 
RETVAL=$? 
    echo 
} 

force_reload() { 
    restart 
} 

configtest() { 
$nginx -t -c $NGINX_CONF_FILE 
} 

rh_status() { 
    status $prog 
} 

rh_status_q() { 
    rh_status >/dev/null 2>&1 
} 

case "$1" in 
    start) 
        rh_status_q && exit 0 
    $1 
        ;; 
    stop) 
        rh_status_q || exit 0 
        $1 
        ;; 
    restart|configtest) 
        $1 
        ;; 
    reload) 
        rh_status_q || exit 7 
        $1 
        ;; 
    force-reload) 
        force_reload 
        ;; 
    status) 
        rh_status 
        ;; 
    condrestart|try-restart) 
        rh_status_q || exit 0 
            ;; 
    *)    
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
        exit 2 

esac  #!/bin/sh 
# 
# nginx - this script starts and stops the nginx daemon 
# 
# chkconfig:   - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
#               proxy and IMAP/POP3 proxy server 
# processname: nginx 
# config:      /etc/nginx/nginx.conf 
# config:      /etc/sysconfig/nginx 
# pidfile:     /var/run/nginx.pid 

# Source function library. 
. /etc/rc.d/init.d/functions 

# Source networking configuration. 
. /etc/sysconfig/network 

# Check that networking is up. 
[ "$NETWORKING" = "no" ] && exit 0 

nginx="/usr/local/nginx/sbin/nginx" 
prog=$(basename $nginx) 

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" 

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 

lockfile=/var/lock/subsys/nginx 

start() { 
    [ -x $nginx ] || exit 5 
    [ -f $NGINX_CONF_FILE ] || exit 6 
    echo -n $"Starting $prog: " 
    daemon $nginx -c $NGINX_CONF_FILE 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && touch $lockfile 
    return $retval 
} 

stop() { 
    echo -n $"Stopping $prog: " 
    killproc $prog -QUIT 
    retval=$? 
    echo 
    [ $retval -eq 0 ] && rm -f $lockfile 
    return $retval 
killall -9 nginx 
} 

restart() { 
    configtest || return $? 
    stop 
    sleep 1 
    start 
} 

reload() { 
    configtest || return $? 
    echo -n $"Reloading $prog: " 
    killproc $nginx -HUP 
RETVAL=$? 
    echo 
} 

force_reload() { 
    restart 
} 

configtest() { 
$nginx -t -c $NGINX_CONF_FILE 
} 

rh_status() { 
    status $prog 
} 

rh_status_q() { 
    rh_status >/dev/null 2>&1 
} 

case "$1" in 
    start) 
        rh_status_q && exit 0 
    $1 
        ;; 
    stop) 
        rh_status_q || exit 0 
        $1 
        ;; 
    restart|configtest) 
        $1 
        ;; 
    reload) 
        rh_status_q || exit 7 
        $1 
        ;; 
    force-reload) 
        force_reload 
        ;; 
    status) 
        rh_status 
        ;; 
    condrestart|try-restart) 
        rh_status_q || exit 0 
            ;; 
    *)    
      echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
        exit 2 

esac  

而後執行centos

:w /etc/init.d/nginx
:q

sudo chmod 755 /etc/init.d/nginx
sudo chkconfig --add nginx

完成後,能夠用如下命令對 nginx 進行操做(還有插曲)php-fpm

#啓動 nginx 服務
sudo service nginx start

#中止 nginx 服務
sudo service nginx stop

#無間斷服務重啓
sudo service nginx reload

我要描述的插曲就是 腳本編輯完以後,用命令彷佛沒法中止 nginx,執行完命令依舊能夠瀏覽,能夠經過 sudo kill -quit nginx 這樣的形式關停

先往下繼續吧,安裝 php-fpm 組件ui

sudo yum install php-fpm

編輯一下 /etc/php.ini (吐槽下 linux 下的配置文件在這個位置?)this

#自願是否先備份一下
sudo cp /etc/php.ini /etc/php.ini.backup

sudo vim /etc/php.ini

/cgi.fix_pathinfo
找到後去掉前面的分號 ;
:wq

配置 www.confcentos7

#自願備份一下 www.conf
sudo cp /etc/php.fpm.d/www.conf /etc/php-fpm.d/www.conf.backup

sudo vim /etc/php.fpm.d/www.conf

#將 user = xxxx 改成 user = 已經存在的有權限的用戶
#將 grouip = xxx 改成 group = 已經存在的有權限的組
#以上兩句是百度出來的,在虛擬機上我改成了當前用戶名

依次啓動 php-fpm 和 (重)啓動 nginx

sudo systemctl start php-fpm

#設置php-fpm開機啓動
#sudo systemctl enable php-fpm

sudo systemctl restart nginx

這裏的插曲在於我用 kill -quit nginx 退出以後再用 service start nginx 時遇到了錯誤提示

而後用 systemctl start nginx 啓動了,但一下子也會出現提示

暫時還不明因此,但 php 能夠運行了

 

一些記錄和連接:

  • 參考《nginx啓動失敗Failed to reload nginx.service: Unit not found》:http://blog.csdn.net/wangjinbao5566/article/details/72938783
  • 參考《centos下配置nginx支持php》:http://www.cnblogs.com/mitang/p/5524540.html
  • 參考《centos7安裝並配置nginx+php》:http://www.cnblogs.com/cglWorkBook/p/5431571.html
  • 參考《如何正確配置 Nginx 和 PHP》:http://blog.jobbole.com/50121/
相關文章
相關標籤/搜索