天天看點

建構LANMP架構二:源碼安裝nginx及簡單應用

yum install pcre-devel -y

yum install openssl-devel -y

tar zxvf nginx-1.0.2.tar.gz

優化安裝

cd nginx-1.0.8

vi auto/cc/gcc   

#CFLAGS=”$CFLAGS -g” (注釋掉這行,去掉debug模式編譯,編譯以後程式隻有幾百k)

vi src/core/nginx.h

#define NGINX_VERSION "1.0.2”

#define NGINX_VER "nginx" (修改此行,去掉後面的“NGINX_VERSION”,為了安全,這樣編譯後

外界無法擷取程式的版本号)

useradd -M -s /sbin/nologin nginx

./configure --prefix=/usr/local/lnmp/nginx --user=nginx --with-http_stub_status_module --withhttp_

ssl_module

make && make install

vi /usr/local/lnmp/nginx/conf/nginx.conf

user nginx nginx;

worker_processes 1;

events {

use epoll; /wiki.nginx.org/ 可以查到調優參數

worker_connections 1024;

}

server {

listen 80;

server_name desktop144.example.com;

/usr/local/lnmp/nginx/sbin/nginx 啟動程式

vi ~/.bash_profile                                   

PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/nginx/sbin

nginx -t                                           檢測文法

nginx 運作

nginx -s                                          reload stop

nginx  加密

vi nginx.conf

listen 443;

ssl on;

ssl_certificate cert.pem; 同時指定pem

ssl_certificate_key cert.pem;

ssl_session_timeout 5m;

ssl_protocols SSLv2 SSLv3 TLSv1;

ssl_ciphers HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers on;

location / {

root html;

index index.html index.htm;

在 /etc/pki/tls/certs下建立證書

mv cert.pem /usr/local/lnmp/nginx/conf/    将證書放到指定位置

虛拟主機   編輯nginx主配置檔案nginx.conf

log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 指定日志格式

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log logs/access.log main; 指定日志main                       取消"#"

server_name www.example1.com;

access_log logs/example2.access.log main;

index index.html;

root html/example1.com;

server_name www.example2.com;

root html/example2.com;

負載均衡

主ip192.168.0.144

輔助2apache 192.168.0.66 192.168.0.126           為友善輔助就用apache了   

http {

upstream myproject {

server 192.168.0.66:80 weight=3; 權重:連續3次後切換126

server 192.168.0.126:80;

server_name www.example.com;

proxy_pass http://myproject;

vi /etc/hosts

192.168.0.144 www.example.com

在客戶機上添加主機名 可以通路