Linux CentOS7 安裝 Nginx PHP7 並設置開機啓動

所有操作需要在root用戶下

安裝路徑:/usr/local/php

先安裝如下依賴包:

yum install -y gcc gcc-c++  make zlib zlib-devel pcre pcre-devel  libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers libcurl libcurl-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel
 

下載PHP7.2.3安裝包

解壓php-7.2.3.tar.gz  然後進入目錄,編譯。命令如下:

$ tar -zxvf php-7.2.3.tar.gz

$ cd php-7.2.3

$ ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/ --with-config-file-scan-dir=/usr/local/php/etc/php.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --enable-xml --with-libxml-dir --with-xmlrpc --with-openssl --with-mcrypt --with-mhash --with-pcre-regex --with-sqlite3 --with-zlib --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --with-cdb --enable-dom --enable-exif --enable-fileinfo --enable-filter --with-pcre-dir --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext --with-gmp --with-mhash --enable-json --enable-mbstring --enable-mbregex --enable-mbregex-backtrack --with-libmbfl --with-onig --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib-dir --with-pdo-sqlite --with-readline --enable-session --enable-shmop --enable-simplexml --enable-sockets --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-wddx --with-libxml-dir --with-xsl --enable-zip --enable-mysqlnd-compression-support --with-pear --enable-opcache --enable-pcntl
 

在php-7.2.3中執行make

make完成後執行make install

 

在之前編譯的源碼包中,找到 php.ini-production,複製到/usr/local/php/etc下,並改名爲php.ini:

$ cp php.ini-production /usr/local/php/etc/php.ini
 

將php源碼編譯目錄下的 sapi/fpm/init.d.php-fpm 文件拷貝到系統配置 /etc/init.d 目錄下並重命名爲 php-fpm 

[root@localhost php-7.2.3]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 
[root@localhost php-7.2.3]# chmod +x /etc/init.d/php-fpm

添加 php-fpm 配置文件 
將php安裝目錄下的 /usr/local/php/etc/php-fpm.conf.default 文件拷貝同目錄下並重命名爲 php-fpm.conf 

[root@localhost php-7.2.3]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 

添加 www.conf 配置文件 
將php安裝目錄下的 /usr/local/php/etc/php-fpm.d/www.conf.default 文件拷貝同目錄下並重命名爲 www.conf 

[root@localhost php-7.2.3]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf 

添加php安裝目錄到系統環境變量 
創建並打開文件php.sh 

[root@localhost php-7.2.3]# vim /etc/profile.d/php.sh 

添加內容如下: 

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

保存並退出 

:wq! 

使用source立即生效剛剛添加的php環境變量 

[root@localhost php-7.2.3]# source /etc/profile.d/php.sh 
啓動php-fpm

[root@localhost php-7.2.3]# service php-fpm start
 看到9000端口也就成功了!!!

設置php開機啓動

修改系統配置目錄下的 php-fpm 文件可執行權限 
chmod +x /etc/init.d/php-fpm
> 將系統配置目錄下的 `php-fpm` 添加到 `系統服務`
chkconfig --add php-fpm
 
> 設置 `php-fpm` `系統服務` 爲開機啓動
chkconfig php-fpm on
 

安裝  Nginx

 

yum install gcc gcc-c++ automake pcre pcre-devel zlip zlib-devel openssl openssl-devel

依賴的簡單解釋:
gcc 安裝:安裝 nginx 需要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境。

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

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

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

2.準備好Nginx的tar包

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

image.png


(2)使用wget命令下載(推薦)。

 

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

3.解壓安裝文件

tar -zxvf nginx-1.10.1.tar.gz
tar -zxvf nginx-1.10.1.tar.gz -C /its  #解壓時指定解壓目錄

4.解壓之後進入安裝文件nginx-1.10.1進行編譯配置

./configure --prefix=/usr/local/nginx --with-http_ssl_module

 

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".

典型實例(下面爲了展示需要寫在多行,執行時內容需要在同一行)

 

1

2

3

4

5

6

7

./configure

    --sbin-path=/usr/local/nginx/nginx

    --conf-path=/usr/local/nginx/nginx.conf

    --pid-path=/usr/local/nginx/nginx.pid

    --with-http_ssl_module

 

 

.自定義配置(不推薦)

./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 \

 

linux 配置nginx 開機自啓動

2019.04.18 08:28:56字數 238閱讀 246

第一步

首先,在linux系統的/etc/init.d/目錄下創建nginx文件,使用如下命令:

vi /etc/init.d/nginx

nginx 腳本內容:

#!/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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    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
}
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

注意:
自定義編譯安裝的nginx,需要根據您的安裝路徑修改下面這兩項配置:
nginx=”/usr/sbin/nginx” 修改成nginx執行程序的路徑。
NGINX_CONF_FILE=”/etc/nginx/nginx.conf” 修改成配置文件的路徑。

第二步

保存腳本文件後設置文件的執行權限:
chmod a+x /etc/init.d/nginx
然後,就可以通過該腳本對nginx服務進行管理了:

/etc/init.d/nginx start
/etc/init.d/nginx stop

第三步

使用chkconfig進行管理,先將nginx服務加入chkconfig管理列表:
chkconfig --add /etc/init.d/nginx
加完這個之後,就可以使用service對nginx進行啓動,重啓等操作了。

service nginx start
service nginx stop

第四步

設置終端模式開機啓動:
chkconfig nginx on

驗證是否成功

reboot

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章