源碼編譯安裝LNMP

一、nginx概述

       LNMP=Linux Nginx Mysql PHP
Nginx ("engine x") 是一個高性能的 HTTP 和 反向代理 服務器。Nginx 是由 Igor Sysoev 爲俄羅斯訪問量第二的 Rambler.ru 站點開發的,第一個公開版本0.1.0發佈於2004年10月4日。其將源代碼以類BSD許可證的形式發佈,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。2011年6月1日,nginx 1.0.4發佈。


       Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。由俄羅斯的程序設計師Igor Sysoev所開發,供俄國大型的入口網站及搜索引擎Rambler(俄文:Рамблер)使用。其特點是佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度BWS、新浪、網易、騰訊等

Nginx官方網站

       RamBler:http://www.rambler.ru/                Nginx:http://nginx.org/
233935p03bwwb1nrn32j1v.jpg


在Nginx中,我們使用的是php-fpm來對php頁面解析,PHP-FPM其實是PHP源代碼的一個補丁,指在將FastCGI進程管理整合進PHP包中。必須將它patch到你的PHP源代碼中,再編譯安裝PHP後纔可以使用
       從PHP5.3.3開始,PHP中直接整合了PHP-FPM,所以從PHP5.3.3版本以後,不需要下載PHP-FPM補丁包了,下面是PHP-FPM官方發出來的通知:
       http://php-fpm.org/download

二、Nginx的工作原理
Nginx 本身只支持靜態頁面的處理,當客戶端訪問php頁面的時候,nginx會將php轉到php-fpm也處理,
php-fpm服務會把php頁面解析成html文件給nginx處理,nginx返回給客戶端處理
這裏需要結合Apache的工作,對PHP文件處理過程的區別
       1:Nginx是通過php-fpm這個服務來處理php文件
       2:Apache是通過libphp5.so這個模塊來處理php文件
Apache:

233935eojfcgjl9xs0g0y0.jpg


Nginx:
233936x8gjl3laeep8reph.jpg


       Apache的libphp5.so隨着apache服務器一起運行,而Nginx和php-fpm是各自獨立運行,所以在服務的運行過程中,Nginx和php-fpm都需要分別啓動!
修改Nginx配置文件,啓動nginx服務,修改php配置文件,啓動php-fpm服務。


三、安裝環境

系統環境:CentOS release 6.9 64bit  
軟件名稱:libmcrypt-2.5.8、mysql-5.6.26、nginx-1.9.4、pcre-8.37、php-5.6.13

軟件安裝位置:/usr/local/

數據存放位置:/usr/local/nginx/html、/usr/local/mysql/data

日誌存放位置:/usr/local/nginx/logs、/usr/local/mysql/logs


四、安裝lnmp

1、編譯安裝nginx

1)安裝開發工具包

yum -y groupinstall "Development Tools" "Development Libraries"


2)安裝依賴包

yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre* pcre-devel


3)在home創建lnmp文件夾,將對應軟件包上傳到這個文件夾

[root@centos-6 lnmp]# ll
total 50292
-rw-r--r-- 1 root root   875425 Jun 10 07:10 libmcrypt-2.5.8.tar.bz2
-rw-r--r-- 1 root root 33278710 Jun 10 07:10 mysql-5.6.26.tar.gz
-rw-r--r-- 1 root root   832104 Jun 10 07:10 nginx-1.8.0.tar.gz
-rw-r--r-- 1 root root   866423 Jun 10 07:11 nginx-1.9.4.tar.gz
-rw-r--r-- 1 root root  1557603 Jun 10 07:10 pcre-8.37.tar.bz2
-rw-r--r-- 1 root root 14077633 Jun 10 07:10 php-5.6.13.tar.bz2
[root@centos-6 lnmp]#


4)解壓pcre軟件包

解壓此安裝包即可,不需要安裝,Nginx需要指定pcre的源碼不是安裝後的路徑,此包的功能是支持地址重寫rewrite功能  pcre的依賴可以yum安裝pcre和pcre-devel解決!

tar jxvf pcre-8.37.tar.bz2 -C /usr/local/src/


5)編譯安裝nginx

[root@centos-6 lnmp]# tar zxvf nginx-1.9.4.tar.gz -C /usr/local/src/
[root@centos-6 lnmp]# cd /usr/local/src/nginx-1.9.4/[root@centos-6 nginx-1.9.4]# ./configure --prefix=/usr/local/nginx --with-http_dav_module
--with-http_stub_status_module --with-http_addition_module
--with-http_sub_module --with-http_flv_module --with-http_mp4_module
--with-pcre=/usr/local/src/pcre-8.37


6)功能模塊說明:

--with-http_dav_module                  #啓用支持(增加PUT,DELETE,MKCOL:創建集合,COPY和MOVE方法)
                                    默認關閉,需要編譯開啓
--with-http_stub_status_module    #啓用支持(獲取Nginx上次啓動以來的工作狀態)
--with-http_addition_module         #啓用支持(作爲一個輸出過濾器,支持不完全緩衝,分部分相應請求)
--with-http_sub_module                 #啓用支持(允許一些其他文本替換Nginx相應中的一些文本)
--with-http_flv_module                   #啓用支持(提供支持flv視頻文件支持)
--with-http_mp4_module                      #啓用支持(提供支持mp4視頻文件支持,提供僞流媒體服務端支持)
--with-pcre=/usr/local/src/pcre-8.37   #需要注意,這裏指的是源碼,用#./configure --help |grep pcre查看幫助

7)採用多線程編譯加快速度

[root@centos-6 nginx-1.9.4]# make -j 4 && make install


8)創建沒家目錄及登錄權限的nginx運行帳號

[root@centos-6 lnmp]# useradd -M -s /sbin/nologin nginx

9)配置nginx支持php及修改運行用戶

[root@centos-6 nginx-1.9.4]#vim /usr/local/nginx/conf/nginx.conf

#user  nobody;                 
user nginx nginx;                   #添加此行


#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;
        #}                      #找到上面這段內容,將這段內容複製,去掉#且修改爲如下


location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;      #將scripts修改爲nginx的html,即Nginx頁面目錄,因爲要處理的php文件也在這個目錄下
    include        fastcgi_params;
}

10)啓動nginx並查看狀態是否啓動成功

[root@centos-6 nginx-1.9.4]# /usr/local/nginx/sbin/nginx
[root@centos-6 nginx-1.9.4]# ps -aux | grep nginx
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
root      7715  0.0  0.0  21756   840 ?        Ss   07:38   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     7716  0.0  0.0  22108  1412 ?        S    07:38   0:00 nginx: worker process      
root      7718  0.0  0.0 103328   876 pts/2    S+   07:38   0:00 grep nginx
[root@centos-6 nginx-1.9.4]# netstat -antup | grep nginx
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      7715/nginx          
[root@centos-6 nginx-1.9.4]#

11)nginx維護命令

[root@centos-6 nginx-1.9.4]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@centos-6 nginx-1.9.4]#
[root@centos-6 nginx-1.9.4]# /usr/local/nginx/sbin/nginx -s reload            //重新加載配置文而建(平滑重啓)平滑重啓(保持了C-S鏈接,不斷開,服務器只是重新加載了配置文件,沒有開啓和關閉的服務器的一個動作)
[root@centos-6 nginx-1.9.4]# /usr/local/nginx/sbin/nginx -s stop             //停止Nginx,注意:啓動沒有任何參數
[root@centos-6 nginx-1.9.4]# echo "/usr/local/nginx/sbin/nginx &" >> /etc/rc.local


12)查看nginx是否安裝成功

wKiom1k7TYzBS2JBAABWFI0m6rk641.png-wh_50



2、編譯安裝mysql

1)清理已安裝的mysql

[root@centos-6 home]# rpm -qa | grep mysql
mysql-libs-5.1.73-8.el6_8.x86_64
[root@centos-6 home]# yum remove mysql-libs
[root@centos-6 home]# rpm -qa | grep mysql
[root@centos-6 home]#

2)安裝依賴包

[root@centos-6 home]# yum install -y cmake ncurses-devel

3)編譯安裝mysql

[root@centos-6 lnmp]# tar xf mysql-5.6.26.tar.gz -C /usr/local/src/

3)創建運行mysql的帳號

[root@centos-6 mysql-5.6.26]# useradd -M -s /sbin/nologin mysql

4)配置編譯參數

[root@centos-6 mysql-5.6.26]#cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1\
-DWITH_INNOBASE_STORAGE_ENGINE=1\
-DWITH_MEMORY_STORAGE_ENGINE=1\
-DWITH_READLINE=1\
-DENABLED_LOCAL_INFILE=1\
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DMYSQL-USER=mysql

5)編譯參數說明

DCMAKE_INSTALL_PREFIX                                      #制定mysql的安裝根目錄,目錄在安裝的時候會自動創建,這個值也可以在服務器啓動時,用--basedir來設置
DMYSQL_UNIX_ADDR                                           #服務器與本地客戶端進行通信的Unix套接字文件,必須是絕對路徑,默認位置/tmp/mysql.sock,可以在服務器啓動時,用--socket改變
DDEFAULT_CHARSET                        #mysql默認使用的字符集,不指定將默認使用Latin1西歐字符集
DDEFAULT_COLLATION                           #默認字符校對
DWITH_EXTRA_CHARSETS                  #制定mysql拓展字符集,默認值也是all支持所有的字符集
DWITH_MYISAM_STORAGE_ENGINE #靜態編譯MYISAM,INNOBASE,MEMORY存儲引擎到MYSQL服務
                                   器,這樣MYSQL就支持這三種存儲引擎
DWITH_INNOBASE_STORAGE_ENGINE
DWITH_MEMORY_STORAGE_ENGINE
DWITH_READLINE                    #支持readline庫
DENABLED_LOCAL_INFILE                    #允許本地倒入數據,啓用加載本地數據
DMYSQL_DATADIR                                #mysql數據庫存放路徑
DMYSQL-USER                                    #運行mysql的用戶

這些編譯參數的幫助尋找方法:
http://www.mysql.com→→Documentation→→選擇對應的版本(5.6)→→HTML Online→→View→→Installation & Upgrades→→Installing MySQL from Source →→MySQL Source-Configuration Options→→
http://dev.mysql.com/doc/refman/ ... ration-options.html   


6)編譯安裝

[root@centos-6 mysql-5.6.26]#make -j 4 ; make install

        //以4個進程來加快編譯速度,但不要超過CPU核心數,編譯時間較長需耐心等待

7)配置mysql

[root@centos-6 mysql-5.6.26]# chown -R mysql:mysql /usr/local/mysql/     //屬主屬組更改

[root@centos-6 mysql-5.6.26]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf       //覆蓋原配置文件

[root@centos-6 mysql-5.6.26]#  cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld     //複製啓動腳本

[root@centos-6 mysql-5.6.26]#  vim /etc/init.d/mysqld        //更改啓動腳本中指定mysql位置

basedir=
datadir=
#修改爲
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

[root@centos-6 mysql-5.6.26]# chkconfig mysqld  on        //設置開機啓動


8)初始化數據庫

[root@centos-6 mysql-5.6.26]#/usr/local/mysql/scripts/mysql_install_db \
--defaults-file=/etc/my.cnf \
--basedir=/usr/local/mysql/ \
--datadir=/usr/local/mysql/data/ \
--user=mysql

查看初始化是否成功

[root@centos-6 mysql-5.6.26]#  ls /usr/local/mysql/data/
ibdata1  ib_logfile0  ib_logfile1  mysql  performance_schema  test
[root@centos-6 mysql-5.6.26]#

[root@centos-6 mysql-5.6.26]# ln -s /usr/local/mysql/bin/* /bin/    ///這個裏面是部分命令,讓系統直接調用

[root@centos-6 mysql-5.6.26]# /etc/init.d/mysqld restart        //啓動數據庫

 ERROR! MySQL server PID file could not be found!
Starting MySQL.. SUCCESS!

[root@centos-6 mysql-5.6.26]# mysql_secure_installation    ////初始安全設置,設置root密碼,急用root遠程訪問,刪除test數據庫等;

9)測試登陸是否正常

[root@centos-6 mysql-5.6.26]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.6.26 Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

3、編譯安裝php

1)解決依賴關係

yum -y install php-pear

//pear按照一定的分類來管理pear應用代碼庫,你

的pear代碼可以組織到其中適當的目錄中,其他人可以方便的檢索並分享到你的成果;pear不僅僅是一個代碼倉庫,它同時也是一個標準,使用這個標準來書寫你的php代碼,將會增強你的程序的可讀性,複用性,減少出錯的機率;Pear通過兩個類爲你搭建了一個框架,實現了諸如析構函數,錯誤捕獲功能,你通過繼承就可以使用這些功能


PHP添加libmcrypt拓展

       libmcrypt加密算法擴展庫,支持DES, 3DES,RIJNDAEL, Twofish, IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法

       官方網站:http://mcrypt.hellug.gr/index.html  或者 http://mcrypt.sourceforge.net/

       在頁面上點擊下圖連接,可以下載最新的2.5.8版本(頁面上有個Browse All Files→→Libmcrypt)


233937qdbjy6av9byxz4j2.png


2)安裝加密算法擴展庫

[root@centos-6 lnmp]# tar xf libmcrypt-2.5.8.tar.bz2 -C /usr/local/src/ ; cd /usr/local/src/libmcrypt-2.5.8/

[root@centos-6 libmcrypt-2.5.8]#  ./configure --prefix=/usr/local/libmcrypt ; make -j 4 ; make install

3)除開上面的依賴解決之外,還需要安裝圖片,xml,字體支持基本庫,使用yum去安裝,安裝的時候,這些軟件包自身也有依賴!

[root@centos-6 libmcrypt-2.5.8]#  yum install -y libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype freetype-devel libzip libzip-devel

4)需要添加到庫文件路徑

[root@centos-6 libmcrypt-2.5.8]# vim /etc/ld.so.conf

添加如下內容

include ld.so.conf.d/*.conf
/usr/local/libmcrypt/lib                       #此行添加
/usr/local/mysql/lib                           #此行添加

[root@centos-6 libmcrypt-2.5.8]#  ldconfig
[root@centos-6 libmcrypt-2.5.8]# echo 'ldconfig' >> /etc/rc.local

5)加壓php

[root@centos-6 lnmp]#  tar xf php-5.6.13.tar.bz2 -C /usr/local/src/ ; cd /usr/local/src/php-5.6.13
[root@centos-6 php-5.6.13]#

6)配置php編譯參數

[root@centos-6 php-5.6.13]#./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex  --enable-fpm --enable-mbstring --with-gd --enable-mysqlnd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --with-gettext --with-mcrypt=/usr/local/libmcrypt --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql=mysqlnd

7)編譯參數說明

--with-config-file-path                         #設置 php.ini 的搜索路徑。默認爲 PREFIX/lib

--with-mysql                        #mysql安裝目錄,對mysql的支持

--with-mysqli                         #mysqli擴展技術不僅可以調用MySQL的存儲過程、處理MySQL事務,而且還可以使訪問數據庫工作變得更加穩定。是一個數據庫驅動

--with-iconv-dir                      #種字符集間的轉換

--with-freetype-dir                  #打開對freetype字體庫的支持

--with-jpeg-dir                      #打開對jpeg圖片的支持

--with-png-dir                          #打開對png圖片的支持

--with-zlib                        #打開zlib庫的支持,實現GZIP壓縮輸出     

--with-libxml-dir=/usr                     #打開libxml2庫的支持,libxml是一個用來解析XML文檔的函數庫

--enable-xml                        #支持xml文檔

--disable-rpath                         #關閉額外的運行庫文件

--enable-bcmath                      #打開圖片大小調整,用到zabbix監控的時候用到了這個模塊

--enable-shmop                       #shmop共享內存操作函數,可以與c/c++通訊

--enable-sysvsem  #加上上面shmop,這樣就使得你的PHP系統可以處理相關的IPC函數(活動在內核級別)。

--enable-inline-optimization       #優化線程

--with-curl                            #打開curl瀏覽工具的支持

--with-curlwrappers                    #運用curl工具打開url流 ,新版PHP5.6已棄用

--enable-mbregex                    #支持多字節正則表達式

--enable-fpm                       #CGI方式安裝的啓動程序,PHP-FPM服務

--enable-mbstring                     #多字節,字符串的支持

--with-gd                             #打開gd庫的支持,是php處理圖形的擴展庫,GD庫提供了一系列用來處理圖片的API,使用GD庫可以處理圖片,或者生成圖片。

--enable-gd-native-ttf                    #支持TrueType字符串函數庫

--with-openssl                          #打開ssl支持

--with-mhash                       #支持mhash算法擴展

--enable-pcntl                          #freeTDS需要用到的,pcntl擴展可以支持php的多線程操作

--enable-sockets                       #打開 sockets 支持

--with-xmlrpc                       #打開xml-rpc的c語言

--enable-zip                         #打開對zip的支持

--enable-soap                          #擴展庫通過soap協議實現了客服端與服務器端的數據交互操作

--with-mcrypt                       #mcrypt算法擴展

--with-zlib-dir=/usr/local/libzip   ##指定zip庫路徑


8)編譯安裝

[root@centos-6 php-5.6.13]# make -j 3 && make install


9)配置php和php-fpm

[root@centos-6 php-5.6.13]# cp /usr/local/src/php-5.6.13/php.ini-production /usr/local/php/php.ini        //替換php配置文件
[root@centos-6 php-5.6.13]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf        //替換php-fpm配置文件
[root@centos-6 php-5.6.13]# chown -R nginx:nginx /usr/local/php/etc/php-fpm.conf        //修改php-fpm.conf文件權限

[root@centos-6 nginx]# vim /usr/local/php/etc/php-fpm.conf        //修改php-fpm運行帳號爲nginx

user = nobody

group = nobody

改爲

user = nginx

group = nginx


10)配置php-fpm啓動腳本並配置隨機啓動

[root@centos-6 php-5.6.13]# cp /usr/local/src/php-5.6.13/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@centos-6 php-5.6.13]#  chmod +x /etc/init.d/php-fpm
[root@centos-6 php-5.6.13]# chkconfig php-fpm on
[root@centos-6 php-5.6.13]#  /etc/init.d/php-fpm start
Starting php-fpm  done


11)測試php解釋是否生效

[root@centos-6 php-5.6.13]# echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/info.php

wKioL1k7TfHi7vPoAADaQ1Ij2S0429.png-wh_50

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