1. 程式人生 > >LNMP環境搭建

LNMP環境搭建

linux 電子 代理服務器 master 區分大小寫

Nginx是一款輕量的異步Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器。

Nginx的處理連接方式與apache的模式類似,nginx通過主進程master產生多個子進程worker,然後每個worker產生很多的線程,由線程去處理具體的請求,處理速度快的原因是因為處理方式為異步處理。

技術分享

Nginx與Apache對比:

輕量級,同樣起web 服務,比apache 占用更少的內存及資源

抗並發,nginx 處理請求是異步非阻塞的,而apache 則是阻塞型的,在高並發下nginx 能保持低資 源低消耗高性能

高度模塊化的設計,編寫模塊相對簡單

社區活躍,各種高性能模塊出品迅速啊


Nginx與apache、lighttp性能綜合對比,如下圖:

技術分享


一.系統需求:

CentOS/RHEL/Fedora/Debian/Ubuntu系統

需要3GB以上硬盤剩余空間

MySQL 5.6及MariaDB 10必須1G以上內存

Selinux 必須關

Linux下區分大小寫,輸入命令時請註意!

確定yum源正常使用!


二.安裝步驟:

下載並安裝LNMP一鍵安裝包:

#tar -xvf lnmp1.2-full.tar.gz

#cd lnmp1.2-full

#./install.sh lnmp

安裝LNMP執行:wget -c http://soft.vpser.net/lnmp/lnmp1.2-full.tar.gz && tar xvf lnmp1.2-full.tar.gz && cd lnmp1.2-full && ./install.sh lnmp

如需要安裝LNMPA或LAMP,將./install.sh 後面的參數替換為lnmpalamp即可。

按上述命令執行後,會出現如下提示:

技術分享


需要設置MySQL的root密碼(不輸入直接回車將會設置為root),輸入後回車進入下一步,如下圖所示:

技術分享


這裏需要確認是否啟用MySQL InnoDB,如果不確定是否啟用可以輸入 y ,輸入 y 表示啟用,輸入 n 表示不啟用。默認為y 啟用,輸入後回車進入下一步,選擇MySQL版本:

技術分享


輸入MySQL或MariaDB版本的序號,回車進入下一步,選擇PHP版本:

技術分享


輸入PHP版本的序號,回車進入下一步,選擇是否安裝內存優化:

技術分享


可以選擇不安裝、Jemalloc或TCmalloc,輸入對應序號回車。

如果是LNMPA或LAMP的話還需要設置管理員郵箱

技術分享


再選擇Apache版本

技術分享

提示"Press any key to install...or Press Ctrl+c to cancel"後,按回車鍵確認開始安裝。

LNMP腳本就會自動安裝編譯Nginx、MySQL、PHP、phpMyAdmin、Zend Optimizer這幾個軟件。


安裝完成

如果顯示Nginx: OK,MySQL: OK,PHP: OK

技術分享


並且Nginx、MySQL、PHP都是running,80和3306端口都存在,並Install lnmp V1.2 completed! enjoy it.的話,說明已經安裝成功。

安裝失敗

技術分享


如果出現類似上圖的提示,則表明安裝失敗,說明沒有安裝成功!!

執行./uninstall進行卸載

LNMP相關軟件安裝目錄

Nginx 目錄: /usr/local/nginx/

MySQL 目錄 : /usr/local/mysql/

MySQL數據庫所在目錄:/usr/local/mysql/var/

PHP目錄 : /usr/local/php/

PHPMyAdmin目錄 : /home/wwwroot/default/phpmyadmin/

默認網站目錄 : /home/wwwroot/default/

Nginx日誌目錄:/home/wwwlogs/

LNMP相關配置文件位置:

Nginx主配置文件:/usr/local/nginx/conf/nginx.conf

MySQL配置文件:/etc/my.cnf

PHP配置文件:/usr/local/php/etc/php.ini

php-fpm配置文件:/usr/local/php/etc/php-fpm.conf

LNMP狀態管理命令:

LNMP 狀態管理: lnmp {start|stop|reload|restart|kill|status}

LNMP 各個程序狀態管理: lnmp {nginx|mysql|mariadb|php-fpm|pureftpd} {start|stop|reload|restart|kill|status}

配置文件

#vi /usr/local/nginx/conf/nginx.conf

user www www;

#程序運行用戶和組

worker_processes auto;

#啟動進程,指定nginx啟動的工作進程數量,建議按照cpu數目來指定,一般等於cpu數目

error_log /home/wwwlogs/nginx_error.log crit;

#全局錯誤日誌

pid /usr/local/nginx/logs/nginx.pid;

#主進程PID保存文件

#Specifies the value for maximum file descriptors that can be opened by this process.

worker_rlimit_nofile 51200;

#文件描述符數量

events

{

use epoll;

#使用epoll模型,對於2.6以上的內核,建議使用epoll模型以提高性能

worker_connections 51200;

#工作進程的最大連接數量,根據硬件調整,和前面工作進程配合起來用,盡量大,但是別把cpu跑到100%就行每個進程允許的最多連接數, 理論上每臺nginx服務器的最大連接數為worker_processes*worker_connections,具體還要看服務器的硬件、帶寬等。

multi_accept on;

}

http

#整體環境配置--網站配置

{

include mime.types;

default_type application/octet-stream;

#設定mime類型,文件傳送類型由mime.type文件定義

server_names_hash_bucket_size 128;

#保存服務器名字的hash表大小

client_header_buffer_size 32k;

#客戶端請求頭部緩沖區大小

large_client_header_buffers 4 32k;

#最大客戶端頭緩沖大小

client_max_body_size 50m;

#客戶端最大上傳文件大小(M)

sendfile on;

#sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,必須設為 on。如果用來進行下載等應用磁盤IO重負載應用,可設置為off,以平衡磁盤與網絡I/O處理速度,降低系統的uptime.

tcp_nopush on;

#這個是默認的,結果就是數據包不會馬上傳送出去,等到數據包最大時,一次性的 ,傳輸出去,這樣有助於解決網絡堵塞。(只在sendfile on時有效)

keepalive_timeout 60;

#連接超時時間

tcp_nodelay on;

#禁用nagle算法,也即不緩存數據。有效解決網絡阻塞

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 256k;

#fastcgi設置

gzip on;

gzip_min_length 1k;

gzip_buffers 4 16k;

gzip_http_version 1.1;

gzip_comp_level 2;

gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;

gzip_vary on;

gzip_proxied expired no-cache no-store private auth;

gzip_disable "MSIE [1-6]\.";

#limit_conn_zone $binary_remote_addr zone=perip:10m;

#If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

server_tokens off;

#隱藏nginx版本號(curl -I 192.168.4.154可以查看,更加安全)

#log format

log_format access ‘$remote_addr - $remote_user [$time_local] "$request" ‘

‘$status $body_bytes_sent "$http_referer" ‘

‘"$http_user_agent" $http_x_forwarded_for‘;

#定義日誌格式

server

{

listen 80 default_server;

#listen [::]:80 default_server ipv6only=on;

#監聽80端口,WEB服務的監聽設置,可以采用"IP地址:端口"形式

server_name www.lnmp.org;

#服務器名,可以寫多個域名,用空格分隔

index index.html index.htm index.php;

#默認網頁文件

root /home/wwwroot/default;

#網頁主目錄

#error_page 404 /404.html;

include enable-php.conf;

location /nginx_status

{

stub_status on;

access_log off;

}

#開啟status狀態監測

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{

expires 30d;

}

#靜態文件處理,保存期30天

location ~ .*\.(js|css)?$

{

expires 12h;

}

#js和css文件處理,保存期12小時

location ~ /\.

{

deny all;

}

access_log /home/wwwlogs/access.log access;

#正確訪問日誌

}

include vhost/*.conf;

#vhost/下子配置文件生效

}

檢查nginx配置文件語句錯誤

/usr/local/nginx/sbin/nginx -t

平滑重啟nginx進程

1)pkill -HUP nginx

2)/usr/local/nginx/sbin/nginx -s reload

配置實驗

1.nginx狀態監控

location /nginx_status{

stub_status on;

access_log off;

}

#客戶端訪問網址:http://IP/nginx_status

註:"Active connections"表示當前的活動連接數;

"server accepts handled requests"表示已經處理的 連接信息

三個數字依次表示已處理的連接數、成功的TCP握手次數、已處理的請求數

技術分享


nginx虛擬主機

使用Nginx搭建虛擬主機服務器時,每個虛擬WEB站點擁有獨立的"server { }"配置段,各自監聽的IP地址、端口號可以單獨指定,當然網站名稱也是不同的。

---基於域名的虛擬主機---

兩個站點www.benet.comwww.accp.com

192.168.1.1 www.benet.com

192.168.1.1 benet.com

192.168.1.1 www.accp.com

192.168.1.1 accp.com

修改Linux下和windows下的hosts文件,使其能正常解析域名

為兩個虛擬WEB主機分別建立網頁根目錄,並準備測試首頁

mkdir -p /home/wwwroot/benet/

mkdir -p /home/wwwroot/accp/

修改nginx.conf配置文件---> 建立域名虛擬主機

配置兩個"server {}"區域,分別對應兩個WEB站點,指定各自的網站名稱、監聽 地址、 網站根目錄、訪問日誌等信息。

在主配置文件當中添加兩個虛擬主機的配置信息(添加在http { }範圍內

server {

listen 80;

server_name www.benet.com benet.com;

charset utf-8;

access_log logs/benet.access.log;

location / {

root /home/wwwroot/benet;

index index.php index.html;

}

}

server {

listen 80;

server_name www.accp.com accp.com;

charset utf-8;

access_log logs/accp.access.log;

location / {

root /home/wwwroot/accp;

index index.html index.php;

}

}

技術分享


重新啟動nginx,使用瀏覽器分別通過www.benet.com和www.accp.com訪問 兩個WEB站點

結果:

技術分享

技術分享

技術分享

技術分享


列表頁顯示

server

{

listen 80;

server_name www.benet.com;

index index.html index.htm index.php;

root /home/wwwroot/benet;

autoindex on;

技術分享

技術分享

技術分享

rewrite

location ~ \.php$ {

proxy_pass http://127.0.0.1;

}


例1:域名跳轉

輸入www.benet.com,跳轉到www.accp.com

server {

listen 80;

server_name www.benet.com benet.com;

charset utf-8;

access_log logs/benet.access.log access;

location / {

root /home/wwwroot/benet;

index index.php index.html;

}

if ($http_host = www.benet.com) {

rewrite (.*) http://www.accp.com permanent;

}

}

技術分享

技術分享

例2:文件跳轉

server {

listen 80;

server_name www.accp.com accp.com;

charset utf-8;

access_log logs/benet.access.log access;

location / {

root /home/wwwroot/accp;

index index.html index.php;

}

rewrite inde /indexa.html last;

}


基於多個IP地址的虛擬主機:

配置多個ip地址(可使用一個網卡綁定多個IP如:eth0:1…)

技術分享


修改配置文件:(nginx.conf)

server

{

listen 192.168.115.180:80;

server_name 192.168.115.180;

access_log logs/180.access.log combined;

location /

{

index index.html index.htm;

root /home/wwwroot/180;

}

}

server

{

listen 192.168.115.181:80;

server_name 192.168.115.181;

access_log logs/181.access.log combined;

location /

{

index index.html index.htm;

root /home/wwwroot/181;

}

}

技術分享

技術分享


基於不同端口的虛擬主機:

技術分享

技術分享

技術分享


Rpm包安裝的Lamp中Apache網址別名定義:

技術分享

技術分享

技術分享

本文出自 “運維之旅” 博客,請務必保留此出處http://ahscwy.blog.51cto.com/9853317/1940210

LNMP環境搭建