1. 程式人生 > 其它 >|NO.Z.00058|——————————|^^ 部署 ^^|——|Linux&Centos6.x&LNMP.V02|

|NO.Z.00058|——————————|^^ 部署 ^^|——|Linux&Centos6.x&LNMP.V02|



[LinuxNetworkEnd:Linux&Centos6.x&LNMP.V02]                                          [Applications.LinuxNetworkEnd] [|LNMP|centos6.x|nginx啟動指令碼|]








一、原始碼軟體包安裝
### --- Nginx

~~~     Nginx是一款輕量級的web伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,
~~~     在BSD-like協議下發行,其特點是佔有記憶體少,併發能力強。
### --- 下載nginx原始碼包

[root@server21 ~]# mkdir /lnmp
[root@server21 ~]# cd /lnmp/
[root@server21 lnmp]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@server21 lnmp]# tar -zxvf nginx-1.12.2.tar.gz 
### --- 建立用於執行nginx的使用者

[root@server21 lnmp]# useradd -r -s /sbin/nologin nginx
### --- 解壓縮並安裝

[root@server21 lnmp]# cd nginx-1.12.2
[root@server21 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module && make && make install
[root@server21 nginx-1.12.2]# echo $?
0
[root@server21 nginx-1.12.2]# /usr/local/nginx/sbin/nginx 
[root@server21 nginx-1.12.2]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4385/nginx          
### --- 驗證:

~~~     http://20.20.20.21/
~~~     Welcome to nginx!
~~~     上傳編寫好的nginx啟動管理指令碼(見文字尾部)
二、安裝MySQL
### --- 安裝MySQL

~~~		下載:https://dev.mysql.com/downloads.mysql/
~~~		選擇:MySQL Community SERVER 5.5 >>
~~~		選擇:Select Version:按照自己要求選擇
~~~		Select Operating System:Source Code
~~~		Select OS Version:Generic Linux
### --- 格式:mysql-N.N.NN.tar.gz

[root@server21 nginx-1.12.2]# cd ..
[root@server21 lnmp]# wget http://downloads.mysql.com/archives/mysql-5.5/mysql-5.5.25a.tar.gz
~~~     #OR
[root@server21 ~]# https://downloads.mysql.com/archives/get/p/23/file/mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz
### --- 建立用於執行MySQL的使用者:

[root@server21 lnmp]# useradd -r -s /sbin/nologin mysql
### --- 解壓縮MySQL並安裝

[root@server21 mysql-5.5.25a]# tar -zxvf mysql-5.5.25a.tar.gz
[root@server21 mysql-5.5.25a]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
> -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci \
> -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 \
> -DENABLED_LOCAL_INFILE=1 -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
[root@server21 mysql-5.5.25a]# echo $?
0
[root@server21 mysql-5.5.25a]# make && make install
[root@server21 mysql-5.5.25a]# echo $?
0
[root@server21 mysql-5.5.25a]# ln -s /usr/local/mysql/bin/* /usr/local/bin
### --- 修改安裝後的目錄許可權

[root@server21 mysql-5.5.25a]# cd /usr/local/mysql
[root@server21 mysql]# chown -R root .
[root@server21 mysql]# chown -R mysql data/
### --- 生成MySQL配置檔案

[root@server21 mysql]# cp -a /lnmp/mysql-5.5.25a/support-files/my-medium.cnf /etc/my.cnf
cp: overwrite '/etc/my.cnf'? y
### --- 初始化。生成授權表

[root@server21 mysql]# cd /usr/local/mysql/                     // 一定要先切換到此目錄下,然後在執行下一步
[root@server21 mysql]# ./scripts/mysql_install_db --user=mysql  // 初始化成功標誌:兩個ok
### --- 生成MySQL的啟動和自啟動管理指令碼

[root@server21 mysql]# cd /lnmp/mysql-5.5.25a/support-files/    // 切換到MySQL的原始碼解壓縮目錄下的support-files
[root@server21 support-files]# cp -a mysql.server /etc/init.d/mysqld
[root@server21 support-files]# chmod +x /etc/init.d/mysqld
[root@server21 support-files]# chkconfig --add mysqld
[root@server21 support-files]# service mysqld status
 ERROR! MySQL is not running
[root@server21 support-files]# service mysqld start
Starting MySQL.. SUCCESS! 
[root@server21 support-files]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      14203/mysqld 
### --- 給MySQL的root使用者設定密碼

[root@server21 support-files]# mysqladmin -uroot password 123456
[root@server21 support-files]# mysql -uroot -p
Enter password: 123456
mysql> 
三、PHP
### --- 下載:http://www.php.net/

[root@server21 lnmp]# wget http://tw2.php.net/distributions/php-5.6.38.tar.gz
[root@server21 lnmp]# tar -zxvf php-5.6.38.tar.gz 
### --- 解壓縮PHP並安裝

[root@server21 php-5.6.38]# ./configure --prefix=/usr/local/php/ --with-config-file-path=/usr/local/php/etc/ \
> --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-soap --enable-mbstring=all --enable-sockets \
> --with-pdo-mysql=/usr/local/mysql --with-gd --without-pear --enable-fpm
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php5.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands
### --- 報錯資訊
If configure fails try --with-vpx-dir=<DIR>
If configure fails try --with-jpeg-dir=<DIR>
configure: error: png.h not found.

### --- 解決方案:
[root@server21 php-5.6.38]# yum install libpng
[root@server21 php-5.6.38]# yum install libpng-devel
### --- 報錯資訊:若遇到libpeng.so not found.報錯(老版本的PHP會出現此問題)
[root@server21 php-5.6.38]# make && make install

### --- 解決方案:
[root@server21 php-5.6.38]# ln -s /usr/lib64/libpng.so /usr/lib
### --- 生成PHP配置檔案
[root@server21 php-5.6.38]# cp -a /lnmp/php-5.6.38/php.ini-production /usr/local/php/etc/php.ini    #複製原始碼包內的配置檔案到安裝目錄下,並改名即可

### --- 建立軟連線,使用PHP相關命令是更方便
[root@server21 php-5.6.38]# ln -s /usr/local/php/bin/* /usr/local/bin/
[root@server21 php-5.6.38]# ln -s /usr/local/php/sbin/* /usr/local/sbin/
四、配置nginx連線PHP(重難點)
### --- 配置nginx連線PHP(重難點)

~~~     當由使用者通過nginx的80埠進行請求,nginx響應並判斷使用者的請求,
~~~     若是普通的HTML的頁面,nginx會直接返回結果,若是PHP的頁面,
~~~     nginx會主動向php-fpm發起請求,告訴它有一個PHP的頁面需要解析,你找PHP解析一下。
~~~     nginx連線PHP是藉助了PHP-fpm:9000埠來完成連線,
~~~     而Apache是通過模組來實現連線的。
### --- nginx連線PHP需要啟動php-fpm服務
~~~		啟動時開啟的程序數、最少空閒程序數、做多空閒程序數(預設值,未修改)
~~~		修改nginx啟動管理指令碼,將php-fpm的註釋取消掉即可

[root@server21 php-5.6.38]# cd /usr/local/php/etc/
[root@server21 etc]# cp -a php-fpm.conf.default php-fpm.conf // 生成php-fpm的配置檔案,並修改制定引數
[root@server21 etc]# vim php-fpm.conf                       // 修改指定條目的引數:
pid = run/php-fpm.pid
user = nginx                                                // 預設是nobody
group = nginx       
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
### --- 修改nginx的配置檔案,使其識別.php字尾的檔案

[root@server21 ~]# cp -a /usr/local/nginx/conf/nginx.conf  /usr/local/nginx/conf/nginx.conf.bac
[root@server21 ~]# vim /usr/local/nginx/conf/nginx.conf     // 取消下列的註釋,並修改include選項的字尾為fastcgi.conf,並注意每一行結尾的分號和大括號
user  nginx;                                                // 預設是nobody;預設是登出的,開啟即可
        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.conf;                    // 把fastcgi_params; 修改為fastcgi.conf     #預設是fastcgi_params;,建議修改為fastcfi.conf,不然會報錯。
        }       
### --- 測試:
### --- 重啟nginx服務,建立PHP測試檔案,訪問並檢視是否解析
### --- 配置啟動指令碼,見三、附件

[root@server21 ~]# vim /etc/init.d/nginx
vim /etc/init.d/nginx
[root@server21 ~]# chmod  +x /etc/init.d/nginx 
[root@server21 ~]# service nginx start
nginx service start success! 
[root@server21 ~]# service nginx restart 
nginx service stop success!
nginx service start success!   
[root@server21 ~]# netstat -antp
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      647/php-fpm         
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      14203/mysqld        
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      643/nginx  
### --- 寫入PHP的測試頁面
~~~		可以正常獲取PHP 5.6.38版本的資訊
~~~		訪問驗證:http://20.20.20.21/test.php 

[root@server21 ~]# vim /usr/local/nginx/html/test.php
 <?php
        phpinfo();
### --- 修改nginx配置檔案,使其預設自動載入PHP檔案

[root@server21 ~]# vim /usr/local/nginx/conf/nginx.conf
        location / {
            root   html;
            index  index.html index.php index.htm;          // 加入index.php的解析頁面
        }
### --- 驗證:
~~~		訪問http://20.20.20.21/預設訪問的是Apache的頁面,因為在配置檔案裡,
~~~		index.html優先順序高於index.php,有index.html就載入index.html,沒有就載入index.php
[root@server21 ~]# mv /usr/local/nginx/html/test.php /usr/local/nginx/html/index.php

### --- 驗證:
~~~		訪問http://20.20.20.21/顯示PHP的解析頁面PHP Version 5.6.38,
~~~		誰在前面就優先載入誰,若是都沒有,就顯示該目錄下的檔案列表
[root@server21 ~]# service nginx restart
[root@server21 ~]# rm -rf /usr/local/nginx/html/index.html // 把index.html的配置檔案刪除
五、附件:建議使用時先複製到文字檔案中檢視是否有字符集問題
~~~		# 有4個變數,2個是命令,一個是nginx的啟動命令,一個是php-fpm的啟動命令,
~~~		# 兩個pid檔案有kiss語句,當執行start/restart/stop同時操作nginx,php-fpm

###################################Nginx自動的管理指令碼##############################

[root@server21 ~]# vim /etc/init.d/nginx
#!/bin/bash
#Author: yanqi
#chkconfig: 2345 99 33
#description:nginx server control tools

ngxc="/usr/local/nginx/sbin/nginx"
pidf="usr/local/nginx/logs/nginx.pid"
ngxc_fpm="/usr/local/php/sbin/php-fpm"
pidf_fpm="/usr/local/php/var/run/php-fpm.pid"
case "$1" in
    start)
        $ngxc -t &> /dev/null
        if [ $? -eq 0 ];then
                $ngxc
                $ngxc_fpm
                echo "nginx service start success!"
        else
                $ngxc -t
        fi
        ;;
    stop)
        kill -s QUIT $(cat $pidf)
        kill -s QUIT $(cat $pidf_fpm)
                echo "nginx service stop success!"
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    reload)
        $ngxc -t &> /dev/mull
        if [ $? -eq 0 ];then
                kill -s HUP $(cat $pidf)
                kill -s HUP $(cat $pidf_fpm)
                echo "reload nginx config success!"
        else
                $ngxc -t
        fi
        ;;
    *)
        echo "please input stop|start|restart|reload."
        exit 1
esac








===============================END===============================


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                    ——W.S.Landor



來自為知筆記(Wiz)