天天看点

构建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

在客户机上添加主机名 可以访问