企業級LNMP環境應用實戰:
面試必備:
(1)
LNMP的工作流程:
當LNMP工作的時候,首先是使用者通過浏覽器輸入域名請求Nginx Web服務,如果是請求的是靜态的資源,則由Nginx解析傳回給使用者。
如果是動态的資源,那麼久通過Fast CGI接口發送給PHP引擎服務(Fast CGI程序php-fpm)進行解析。
如果這個動态的請求要讀取資料庫,那麼PHP就會繼續向後請求MySQL資料庫,讀取需要的資料。
最終通過Nginx服務把擷取的資料傳回給使用者,這就是LNMP的基本流程。
(2)企業選用MySQL作為資料庫的優點:
1.性能卓越,服務穩定,很少出現異常當機。
2.開放源代碼并且沒有版權的限制,自主傳播,使用成本低。
3.曆史悠久,社群及使用者非常活躍,遇到問題很快可以擷取幫助。
4.軟體體積小安裝簡單,并且易于維護,安裝及維護的成本低。
5.支援多種作業系統,提供API接口。
6.品牌效應,使得企業無需考慮就直接使用。
(3)LNMP環境搭建問題:
當安裝LNMP一體化環境的時候MySQL資料庫要裝在Nginx所在的伺服器上,
如果MySQL和Nginx不在同一台機器上,那麼Nginx伺服器上的MySQL資料庫軟體隻要解壓移動安裝目錄中就行。
不需要對MySQL進行初始化配置。
在PHP5.3以上的版本中,Nginx伺服器上安裝了MySQL軟體,隻需要在編譯PHP的時候指定相關參數即可。
編譯參數:--with-mysql=mysqlnd
表示在編譯的時候會調用内置的MySQL的庫。
(4)什麼是FCGI:
FastCGI是一個可伸縮的、高速的在HTTP伺服器的動态腳本語言間通信的接口(在Linux下,FastCGI就是socket,這個socket可以是檔案socket或IPsocket)。
FastCGI采用C/S架構,它可以将HTTP伺服器和腳本伺服器分開,同時還可以在腳本解析伺服器上啟動一個或多個伺服器來解析守護程序。
當HTTP伺服器遇到動态程式的時候,可以将其直接傳遞給FastCGI程序來執行,然後将得到的結果傳回給浏覽器。這種方式可以讓HTTP伺服器專一的處理靜态的請求。
這會很高的提高整個應用系統的性能。
(5)FastCGI的重要特點:
1.HTTP伺服器和動态腳本語言間通信的接口或工具。
2.可以把動态語言解析或HTTP伺服器分離開。
3.Nginx、Apache、Lighttpd,以及多數動态語言都支援FastCGI。
4.PHP動态語言方式采用C/S結構,分為用戶端(HTTP伺服器)和伺服器端(動态語言解析伺服器)。
5.PHP動态語言伺服器端可以啟動多個FastCGI的守護程序。
6.HTTP伺服器通過FastCGI用戶端和動态語言FastCGI伺服器端通信。
(6)Nginx FCGI運作原理:
Nginx不支援對外部動态程式的直接調用或者解析。所有的外部程式(包括PHP)必須通過FastCGI接口來調用。
FastCGI接口在Linux下是一個socket,為了調用CGI程式,還需要一個FastCGI的wrapper(可以了解為使用者啟動另一個程式的程式),這個wrapper綁定在某個固定的socket上。
當Nginx将CGI請求發送給這個socket的時候,通過FastCGI接口,wrapper接受到請求,然後派生出一個新的線程,這個線程調用解釋器或外部的程式處理腳本來讀取傳回的資料。
然後wrapper再将傳回的資料通過FastCGI接口,沿着固定的socket傳遞給Nginx,最後Nginx将傳回的資料發送給用戶端。
FastCGI的主要優點就是把動态的語言和HTTP伺服器分離開來,使Nginx專門處理靜态的請求,動态的請求直接使用PHP/PHP-FPM伺服器專門處理。
rpm安裝MySQL資料庫:
#建立MySQL賬号
groupadd mysql #建立mysql組
useradd -s /sbin/nologin -g mysql -M mysql #建立mysql使用者,指定不可以登入,指定屬組,指定不建立home目錄
id mysql #檢視mysql使用者資訊
uid=500(mysql) gid=500(mysql) groups=500(mysql)
ll #檢視安裝包
total 207M
-rw-r--r--. 1 7155 wheel 18M Nov 18 2013 MySQL-client-5.6.15-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel 3.2M Nov 18 2013 MySQL-devel-5.6.15-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel 81M Nov 18 2013 MySQL-embedded-5.6.15-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel 52M Nov 18 2013 MySQL-server-5.6.15-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel 1.9M Nov 18 2013 MySQL-shared-5.6.15-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel 3.8M Nov 18 2013 MySQL-shared-compat-5.6.15-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 wheel 48M Nov 18 2013 MySQL-test-5.6.15-1.el6.x86_64.rpm
rpm -Uvh ./MySQL-* #采用更新安裝MySQL
/etc/init.d/mysql start #啟動服務
采用二進制檔案安裝MySQL安裝時,需要初始化資料庫:
mkdir -p /app/mysql/data/ #建立資料目錄
chown -R mysql.mysql /app/mysql/ #授權目錄
/app/mysql/scripts/mysql_install_db --basedir=/app/mysql/ --datadir=/app/mysql/data/ --user=mysql #初始化資料庫
采用二進制安裝配置MySQL指令使用全局路徑:
echo 'export PATH=/app/mysql/bin:$PATH' >> /etc/profile
source /etc/profile #重新整理配置檔案
采用二進制安裝MySQL資料庫的時候需要配置啟動:
cp /tools/MySQL/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
MySQL二進制預設安裝路徑是/usr/local/mysql,啟動腳本裡是/usr/local/mysql的地方需要替換為MySQL的安裝路徑。
初始化資料庫故障集錦:
(1)顯示ERROR:1004 Can't create file '/tmp/#sql300e_1_0.frm'(errno:13)
原因是/tmp目錄的權限有問題。
chomd -R 1777 /tmp/
(2)WARNING:The host 'mysql' could not be looked up with resoleip
需要修改主機名
netstat -tunlap | grep mysql #檢視MySQL資料庫是否啟動
tcp 0 0 :::3306 :::* LISTEN 2992/mysqld
chkconfig mysql on #設定開機自啟動,也可以将啟動指令放到/etc/rc.local檔案裡面
chkconfig mysql --list
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
初次使用MySQL資料庫:
cd ~ #回到使用者home目錄
cat ./.mysql_secret #檢視MySQL的安全檔案
mysql -uroot -pew4pubHF #登入MySQL
mysql> set password for root@"localhost"=password("123456");
mysql> quit
PHP(FastCGI)服務安裝:
yum install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel openssl-devel -y
yum install freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel -y
這些lib庫不是必須安裝的,但是目前企業裡邊一般都需要安裝,否則可能會導緻PHP無法顯示驗證碼等問題。
編譯安裝libiconv-devel:
tar -xf ./libiconv-1.15.tar.gz
cd ./libiconv-1.15
./configure --prefix=/usr/local/libicon
make && make install
libtool --finish /usr/local/libiconv/lib
編譯安裝libmcrypt:
libmcrypt是一個動态加載的子產品化的limbcrypt,libmcrypt對于在程式運作時添加/移除算法是有用的。
libmcrypt-nm目前不再被官方支援,編譯PHP的時候不是必須的包。
在CentOS中預設沒有libmcrypt-devel,是以需要事先配置epel第三方yum源:
下載下傳阿裡雲的epel的.repo源檔案。
直接使用yum install libmcrypt -y安裝。
tar -xf ./libmcrypt-2.5.8.tar.gz
cd ./libmcrypt-2.5.8
./configure --prefix=/usr/local/libmcrypt-devel
編譯安裝mhash庫:
mhash是基于離散數學原理的不可逆的PHP加密方式擴充庫,其在預設情況下不會開啟。
mhash可以建立校驗值、消息摘要、消息認證碼、以及無需原文的關鍵資訊儲存(如:密碼)。
它為PHP提供了多種雜湊演算法,如MD5,SHA1,GOST等。
可以通過MHASH_hashname()檢視其支援的算法有哪些。
tar -zxvf ./mhash-0.9.9.9.tar.gz
cd ./mhash-0.9.9.9
./configure --prefix=/usr/local/mhash
可以直接使用yum install mhash -y安裝。
注意:
該擴充不可以提供最新的雜湊演算法。
該算法原則上不可逆。
編譯安裝mcrypt加密擴充庫:
mcrypt擴充庫是實作加密解密的功能,既可以将明文加密,也可以将密文還原。
mcrypt是PHP裡面的重要的加密檔案支援擴充庫,該庫在預設情況下不開啟。
mcrypt庫支援20多種加密算法和8種加密模式。具體可以通過mcrypt_list_algorithms()和mcrypt_list_modes()來顯示。
使用指令yum install mhash -y安裝。
export LD_LIBRARY_PATH=/usr/local/libmcrypt-devel/lib:/usr/local/mhash/lib
export LDFLAGS="-L/usr/local/mhash/lib/ -I/usr/local/mhash/include/"
export CFLAGS="-I/usr/local/mhash/include/"
./configure --prefix=/usr/local/mcrypt/ --with-libmcrypt-prefix=/usr/local/libmcrypt
編譯安裝PHP:
tar -zxvf ./php-5.4.24.tar.gz
cd ./php-5.4.24
./configure \
--prefix=/app/php-5.4.24 \
--with-mysql=/app/mysql/mysql/ \
--with-iconv-dir=/usr/local/libiconv/ \
--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 \
--with-curlwrappers \
--enable-mbstring \
--enable-fpm \
--enable-mbregex \
--with-mcrypt=/usr/local/mcrypt/ \
--enable-gd-jis-conv \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-ftp
PHPFastCGI模式說明,如果是PHP5.3及以上的版本,所使用的編譯參數是:--enable-fpm,
如果是PHP5.2版本是--enable-fastcgi --enable-fpm --enable-force-cgi。
+--------------------------------------------------------------------+
| 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
其他的MySQL包場景還需要使用如下的編譯參數:
--enable-mysqlnd
--with-pdo-mysql=mysqlnd
--with-mysqli=mysqlnd
make
make install
報錯集錦:
(1)解決 Cannot find OpenSSL's <evp.h>:
解決:沒有安裝oopenssl-devel的開發包導緻的
yum install openssl openssl-devel
ln -s /usr/lib64/libssl.so /usr/lib/
(3)/tools/lnmp_soft/php-5.4.24/sapi/cli/php: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
make: *** [ext/phar/phar.php] Error 127
原因:PHP找不到指定庫檔案
解決:
ln -s /app/mysql/mysql/lib/libmysqlclient.so.18 /usr/lib64/
(4)Generating phar.phar
chmod: cannot access `ext/phar/phar.phar': No such file or directory
make: [ext/phar/phar.phar] Error 1 (ignored)
Build complete.
Don't forget to run 'make test'.
原因:PHP本身的Bug建立所需的檔案即可
touch ext/phar/phar.phar
配置PHP引擎配置檔案php.ini:
設定軟連接配接便于業務通路:
ln -s /app//php-5.4.24/ /app/php
ll
total 8.0K
drwxr-xr-x 4 mysql mysql 4.0K Sep 15 15:44 mysql
lrwxrwxrwx 1 root root 17 Sep 16 12:13 php -> /app//php-5.4.24/
drwxr-xr-x 9 root root 4.0K Sep 16 11:42 php-5.4.24
檢視php的模闆配置檔案:
ll ./php*
-rw-r--r-- 1 root root 1.5K Sep 16 11:29 ./php5.spec
-rw-r--r-- 1 nginx games 1.5K Jan 8 2014 ./php5.spec.in
-rw-r--r-- 1 nginx games 2.5K Jan 8 2014 ./php.gif
-rw-r--r-- 1 nginx games 66K Jan 8 2014 ./php.ini-development
-rw-r--r-- 1 nginx games 66K Jan 8 2014 ./php.ini-production
php.ini-development是開發環境,跟多的開啟了日志、調試資訊。
php.ini-production是生産環境,有些處于關閉的狀态。
建立PHP的配置檔案:
cp /tools/lnmp_soft/php-5.4.24/php.ini-production /app/php/lib/php.ini
配置PHP服務,使用FastCGI的方式,配置檔案:php-fpm-conf:
cp /app/php/etc/php-fpm.conf.default /app/php/etc/php-fpm.conf
可以先使用PHP預設的配置檔案,後面有專門的PHP服務優化。
啟動PHP服務:
/app/php/sbin/php-fpm
netstat -tunlap | grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 88555/php-fpm
配置Nginx支援PHP程式請求通路:
/app/nginx/conf/extra/blog.conf:
worker_processes 1;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name www.blog.com;
location / {
root html;
index index.html index.htm;
}
location ~ .*\.(php|php5)?$ {
root html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
測試配置檔案:
/app/nginx/sbin/nginx -t
nginx: the configuration file /app/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /app/nginx-1.6.3//conf/nginx.conf test is successful
mkdir /app/nginx/html/blog
測試LNMP環境是否成功:
vim /app/nginx/html/blog/test.php
<?php
phpinfo();
?>
修改hosts檔案,通路http://www.blog.com/test.php
部署web應用:
上傳Web應用:
rz
登入MySQl 資料庫:
mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>
建立WordPress專用的資料庫:
mysql> create database wp;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
| information_schema |
| mysql |
| performance_schema |
| test |
| wp |
5 rows in set (0.01 sec)
mysql> grant all on wp.* to wp@'localhost' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
Bye
解壓部落格檔案:
tar -zxvf ./wordpress-4.8.1-zh_CN.tar.gz -C /app/nginx/html/blog/
mv /app/nginx/html/blog/wordpress/* /app/nginx/html/blog/
rm -rf /app/nginx/html/blog/wordpress/
設定應用的權限:
chown -R nginx.nginx /app/nginx/html/blog/
檢視部落格程式的檔案權限:
ll -d /app/nginx/html/blog/
drwxr-xr-x 5 nginx nginx 4.0K Sep 16 13:44 /app/nginx/html/blog/
建立部落格程式的配置檔案:
cp /app/nginx/html/blog/wp-config-sample.php /app/nginx/html/blog/wp-config.php
vim /app/nginx/html/blog/wp-config.php
// ** MySQL 設定 - 具體資訊來自您正在使用的主機 ** //
/** WordPress資料庫的名稱 */
define('DB_NAME', 'wp');
/** MySQL資料庫使用者名 */
define('DB_USER', 'wp');
/** MySQL資料庫密碼 */
define('DB_PASSWORD', '123456');
/** MySQL主機 */
define('DB_HOST', 127.0.0.1');
/** 建立資料表時預設的文字編碼 */
define('DB_CHARSET', 'utf8');
本文轉自 棋帥小七 51CTO部落格,原文連結:http://blog.51cto.com/xvjunjie/1965854