更新12.1 LNMP架構介紹12.2MYSQL安裝12.3-4 PHP5安裝12.5nginx介紹12.6nginx安裝
12.1 LNMP架構介紹
LNMP就是Linux+Nginx+MySQL+PHP,Linux作為伺服器的作業系統,MySQL即為資料庫。是一組搭建動態網站的開源軟體架構,本身是各自獨立的軟體服務,放在一起使用擁有很高的相容性,共同組成了一個強大的WEB應用程式平臺。
Nginx為一款高效能Web伺服器,本身是不能處理PHP的,當接收到請求時,判斷如果是PHP請求就會將請求交給PHP直譯器處理,然後將結果返回給Client。Nginx一般把請求轉發給fast-cgi管理程序處理,fast-cgi管理程序再選擇cgi子程序處理請求,然後把結果返給Nginx。
LNMP就是Linux+Nginx+MySQL+PHP,Linux作為伺服器的作業系統,MySQL即為資料庫。
區別(lamp和lnmp)
Apache一般是把PHP當做自己的一個模組來啟動;而Ngnix則是把http請求變數轉發給PHP程序,即PHP獨立程序,與Ngnix通訊,這種方式叫做Fast-CGI執行方式。所以Apache所編譯的PHP不能用於Nginx。
Linux、MySQL、PHP這些框架的優點之前已經介紹過,LNMP和LAMP不同的一點就是Web伺服器Nginx,那麼Nginx相比Apache有什麼優點呢?
Nginx是一個小巧而高效的Linux下的Web伺服器軟體,已在一些大型網站上執行多年,很多國內外的入口網站、行業網站也都在是使用Nginx,相當的穩定。
Nginx的主要特點是穩定、功能豐富、安裝配置簡單、低系統資源。Nginx對於反向代理和負載均衡有不錯的支援。Nginx不僅僅可以做為Web伺服器,還可以做負載均衡伺服器、代理伺服器和郵件伺服器。
作為Web伺服器來說,相比 Apache,Nginx 使用更少的資源,支援更多的併發連線,Nginx處理靜態檔案、索引檔案,自動索引的效率非常高,體現更高的效率。
作為代理伺服器,Nginx可以實現無快取的反向代理加速,提高網站的執行速度。
作為負載均衡伺服器,Nginx既可以在內部直接支援Rails和PHP,也可以支援HTTP代理伺服器對外進行服務,同時還支援簡單的容錯和利用演算法進行負載均衡;Nginx 用 C 編寫, 不論是系統資源開銷還是 CPU 使用效率都比 Perlbal 要好的多。
作為郵件代理伺服器來說, Nginx是一個非常優秀的郵件代理伺服器(最早開發這個產品的目的之一也是作為郵件代理伺服器)。
安裝配置上,Nginx安裝簡單、配置靈活、配置檔案非常簡潔(還能夠支援perl語法);效能方面,Nginx是專門為效能優化而開發的,非常注重效率。它採用Poll模型,可以支援更多的併發連線,最大可以支援對50000個併發連線數的形影,而且只佔用很低的記憶體資源;可用性方面,Nginx支援熱部署,啟動速度特別迅速,可以在不間斷服務的情況下,對軟體版本或者配置進行升級,及時執行數月也無需重新啟動,幾乎可以做到7*24顯示不間斷執行。
可見,LNMP(Linux-Nginx-MySQL-PHP)這四種軟體的組合,可以成為一個免費、高效、擴充套件性強的網站服務系統。
Nginx為一款高效能Web伺服器,本身是不能處理PHP的,當接收到請求時,判斷如果是PHP請求就會將請求交給PHP直譯器處理,然後將結果返回給Client。Nginx一般把請求轉發給fast-cgi管理程序處理,fast-cgi管理程序再選擇cgi子程序處理請求,然後把結果返給Nginx。
初步認識Fast-CGI和Nginx+Fast-CGI
1.Apache+PHP 和 Nginx+PHP的區別
Apache一般是把PHP當做自己的一個模組來啟動;而Ngnix則是把http請求變數轉發給PHP程序,即PHP獨立程序,與Ngnix通訊,這種方式叫做Fast-CGI執行方式。所以Apache所編譯的PHP不能用於Nginx。
Nginx+PHP的基本結構圖如下:
2.什麼是Fast-CGI
Fast-CGI是一個可伸縮的、高速的在HTTP server和動態指令碼語言間通訊的介面。多數流行的HTTP server都支援Fast-CGI,包括Apache、Nginx和lighttpd等。同時,Fast-CGI也被許多指令碼語言支援,其中就有PHP。
Fast-CGI是從CGI發展改進而來的。傳統CGI介面方式的主要缺點是效能很差,因為每次HTTP伺服器遇到動態程式時都需要重新啟動指令碼解析器來執行解析,然後將結果返回給HTTP伺服器。這在處理高併發訪問時幾乎是不可用的。另外傳統的CGI介面方式安全性也很差,現在已經很少使用了。
FastCGI介面方式採用C/S結構,可以將HTTP伺服器和指令碼解析伺服器分開,同時在指令碼解析伺服器上啟動一個或者多個指令碼解析守護程序。當HTTP伺服器每次遇到動態程式時,可以將其直接交付給Fast-CGI程序來執行,然後將得到的結果返回給瀏覽器。這種方式可以讓HTTP伺服器專一地處理靜態請求或者將動態指令碼伺服器的結果返回給客戶端,這在很大程度上提高了整個應用系統的效能。
後續還將會有php-fpm和fast-cgi之間的關係的解釋。
3.Nginx+Fast-CGI執行原理
Nginx不支援對外部程式的直接呼叫或者解析,所有的外部程式(包括PHP)必須通過Fast-CGI介面來呼叫。Fast-CGI介面在Linux下是socket(這個socket可以是檔案socket,也可以是ip socket)。
wrapper:為了呼叫CGI程式,還需要一個Fast-CGI的wrapper(wrapper可以理解為用於啟動另一個程式的程式),這個wrapper繫結在某個固定socket上,如埠或者檔案socket。當Nginx將CGI請求傳送給這個socket的時候,通過Fast-CGI介面,wrapper接收到請求,然後Fork(派生)出一個新的執行緒,這個執行緒呼叫直譯器或者外部程式處理指令碼並讀取返回資料;接著,wrapper再將返回的資料通過Fast-CGI介面,沿著固定的socket傳遞給Nginx;最後,Nginx將返回的資料(html頁面或者圖片)傳送給客戶端。這就是Nginx+Fast-CGI的整個運作過程。
所以,我們首先需要一個wrapper,這個wrapper需要完成的工作:
1>通過呼叫fast-cgi(庫)的函式通過socket和Nginx通訊(讀寫socket是fast-cgi內部實現的功能,對wrapper是非透明的)
2>排程thread,進行fork和kill
3>和application(php)進行通訊
4.簡述php-fpm
PHP-FPM是一個PHP FastCGI管理器,是隻用於PHP的,它其實是PHP原始碼的一個補丁,旨在將Fast-CGI程序管理整合進PHP包中。必須將它patch到你的PHP原始碼中,在編譯安裝PHP後才可以使用。新版的PHP已經集成了php-fpm,在./configure的時候帶 –enable-fpm引數即可開啟PHP-FPM。
擴充套件
12.2 MYSQL安裝
yum -y install make gcc-c++ cmake bison-devel ncurses-devel yum install -y autoconf
tar zxvf mysql-5.6.35.tar.gz
cd mysql-5.6.35
mkdir -p /usr/local/mysql/data
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
make && make install make -j 2 && make install -j 2
make clean
複製程式碼
三、配置MySQL
複製程式碼
8、設定許可權
檢視是否有mysql使用者及使用者組
[[email protected] mysql-5.6.35]# cat /etc/passwd
[[email protected] mysql-5.6.35]# cat /etc/group
如果沒有就新增mysql使用者和組
[[email protected] mysql-5.6.35]# groupadd mysql
[[email protected] mysql-5.6.35]# useradd -g mysql mysql
修改/usr/local/mysql許可權
[[email protected] mysql-5.6.35]# chown -R mysql:mysql /usr/local/mysql
9、初始化配置
進入安裝路徑
[[email protected] mysql-5.6.35]# cd /usr/local/mysql
執行初始化配置指令碼,建立系統自帶的資料庫和表
[[email protected] mysql]# scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
10、啟動MySQL
新增服務,拷貝服務指令碼到init.d目錄
如果/etc目錄有之前存在的my.cnf檔案,將其重新命名。啟動MySQL服務時,會優先在/etc目錄下查詢
[[email protected] mysql]# mv /etc/my.cnf /etc/my.cnf.bak
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysql
[[email protected] mysql]# chkconfig mysql on
[[email protected] mysql]# service mysql start --啟動MySQL
11、配置使用者
設定PATH
[[email protected] mysql]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[[email protected] mysql]# source /etc/profile
[[email protected] mysql]# echo $PATH
修改root密碼
[[email protected] mysql]# mysql -uroot
mysql> SET PASSWORD = PASSWORD('mysql5635');
設定mydba使用者可以遠端訪問
mysql> GRANT ALL PRIVILEGES ON *.* TO 'mydba'@'%' IDENTIFIED BY 'mysql5635' WITH GRANT OPTION;
12.3 PHP5安裝
• 和LAMP安裝PHP方法有差別,需要開啟php-fpm服務
yum install expat-devel yum -y install pcre-devel yum install -y epel-release yum install -y libmcrypt-devel
• cd /usr/local/src/
• tar zxf php-5.6.30.tar.gz
• useradd -s /sbin/nologin php-fpm
• cd php-5.6.30
• ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --with-pear --with-curl --with-openssl
./configure --prefix=/usr/local/php-fpm #指定安裝路徑php-fpm
--with-config-file-path=/usr/local/php-fpm/etc #配置檔案所在路徑
--enable-fpm #開啟fpm 不加就不能啟動這個服務
--with-fpm-user=php-fpm --with-fpm-group=php-fpm #指定使用者和組
--with-mysql=/usr/local/mysql #指定mysql路徑
--with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock #指定mysqli 指定mysql sock
• make && make install
• cp php.ini-production /usr/local/php-fpm/etc/php.ini 生產環境
cp php.ini-development /usr/local/php-fpm/etc/php.ini 測試環境
cd /usr/local/php-fpm/etc/ vim php-fpm.conf
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
3456
cd /usr/local/src/php-5.6.30/
• cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm #這個是啟動指令碼
• chmod 755 /etc/init.d/php-fpm
• chkconfig --add php-fpm
• chkconfig php-fpm on
/usr/local/php-fpm/sbin/php-fpm -t # 檢測下有沒有錯誤
• service php-fpm start
• ps aux |grep php-fpm
12.5 nginx介紹
• Nginx官網 nginx.org,最新版1.13,最新穩定版1.12
• Nginx應用場景:web服務、反向代理、負載均衡
• Nginx著名分支,淘寶基於Nginx開發的Tengine,使用上和Nginx一致,服務名,配置檔名都一樣,和Nginx的最大區別在於Tenging增加了一些定製化模組,在安全限速方面表現突出,另外它支援對js,css合併
12.6nginx安裝
•cd /usr/local/src
• tar zxf nginx-1.12.1.tar.gz
• ./configure --prefix=/usr/local/nginx #故意不做複雜
• make && make install
/usr/local/nginx/sbin/nginx -t #檢查配置檔案
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
• chmod 755 /etc/init.d/nginx
• chkconfig --add nginx
• chkconfig nginx on
• cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
• /usr/local/nginx/sbin/nginx -t
• /etc/init.d/nginx start
• netstat -lntp |grep 80
conf # 配置檔案目錄
html #樣例檔案
logs #存放日誌
sbin #程序核心檔案
nginx.conf 檔案詳解
user nobody nobody; #啟動nginx是那個使用者 ningx組上傳圖片的那個使用者 那個使用者使用者讀
worker_processes 2; #子程序有幾個
error_log /usr/local/nginx/logs/nginx_error.log crit; #錯誤日誌
pid /usr/local/nginx/logs/nginx.pid; #PID
worker_rlimit_nofile 51200; # nginx最多可以開啟多少個檔案
events
{
use epoll; #使用epoll模式
worker_connections 6000; #程序最多多少個連線最大連線
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$ #解析PHP部分
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock; #nginx呼叫PHP部分 指定PHP監聽的PHP埠或者是監聽的sock
fastcgi_pass 127.0.0.1:9000; #2種不同的方式
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}