在Linux里安装、启动nginx
来源:互联网 发布:mac切换中文输入法 编辑:程序博客网 时间:2024/06/10 08:57
由于nginx依赖于pcre和zlib,所以在安装nginx时,需要先安装pcre和zlib(为了方便起见,我把pcre、zlib、nginx的包放到了同一个目录下)。
1、安装pcre
从pcre的官网下载tar.gz包,官网地址为:https://sourceforge.net/projects/pcre/files/pcre/,在这里我下载的是:pcre-8.39.tar.gz
2、安装zlib
从zlib的官网下载tar.gz包,官网地址为:http://zlib.net/,在这里我下载的是:zlib-1.2.8.tar.gz
3、安装nginx
从nginx的官网下载tar.gz包,官网地址为:http://nginx.org/en/download.html,在这里我下载的是: nginx-1.11.5.tar.gz
至此,nginx就安装完毕4、配置并启动nginx
经过上面的安装步骤和目录设置,nginx的启动程序就是/usr/local/nginx/sbin/nginx,配置文件是/usr/local/nginx/conf/nginx.conf,可在配置文件里修改端口、反向代理路径等
启动nginx的命令为:
安装pcre
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz tar -zxvf pcre-8.37.tar.gzcd pcre-8.37./configuremakemake install
安装openssl
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gztar -zxvf openssl-1.0.1c.tar.gz
cd openssl-1.0.1c/
./configmakemake install
安装zlib
wget http://zlib.net/zlib-1.2.8.tar.gztar -zxvf zlib-1.2.8.tar.gzcd zlib-1.2.8./configuremakemake install
下载Nginx1.8至/usr/local,看命令吧:
$ tar xzf nginx-1.8.0.tar.gz$ ./configure --prefix=/home/nginx/nginx$ make$ make install
若出现cc找不到执行
yum -y install gcc automake autoconf libtool make
若启动出现如下错误
error while loading shared libraries: libpcre.so.1: cannot open shared object file:No such file or directory
执行以下命令
ln -s /usr/local/lib/libpcre.so.1 /lib64
安装完成之后生成一个nginx的文件夹,cd进去并切换到sbin目录
启动命令
$ ./nginx
关闭命令:
$ ./nginx -s stop
优雅重启
$ ./nginx -s reload
下面是配置:
1、反向代理配置
修改部署目录下conf子目录的nginx.conf文件(如nginx-1.5.13\conf\nginx.conf)内容,可调整相关配置。
反向代理配置示例:
location / {#设置主机头和客户端真实地址,以便服务器获取客户端真实IP proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #禁用缓存 proxy_buffering off; #设置反向代理的地址 proxy_pass http://192.168.1.1;}
代理地址根据实际情况修改。
2、负载均衡配置
nginx 的 upstream默认是以轮询的方式实现负载均衡,这种方式中,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
另外一种方式是ip_hash:每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
负载均衡配置示例:
upstream test{ #ip_hash; server 192.168.1.251; server 192.168.1.252; server 192.168.1.247; }server {listen 80;server_name helloword;location / { #反向代理的地址 proxy_pass http://test; }}
Upstream命名和服务器地址根据实际情况修改。
3、负载均衡+反向代理完整配置示例
nginx.conf:worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream test{ #ip_hash; server 192.168.1.251; server 192.168.1.252; server 192.168.1.247; } server { listen 80; server_name 2; location / { #设置主机头和客户端真实地址,以便服务器获取客户端真实IP proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #禁用缓存 proxy_buffering off; #反向代理的地址 proxy_pass http://test; } }}
4、动静分离配置
worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream test { #ip_hash; server 192.168.1.251; server 192.168.1.252; server 192.168.1.247; } server { listen 80; server_name 2;#配置Nginx动静分离,定义的静态页面直接从Nginx发布目录读取。 location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { root /usr/local/nginx/html/myloan; #expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力 expires 7d; } #所有jsp、do的动态请求都交给后面的tomcat处理 location ~ (\.jsp)|(\.do)$ { #tomcat地址 proxy_pass http://test; proxy_redirect off; proxy_set_header HOST $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; client_body_buffer_size 128k; proxy_connect_timeout 90; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; } }}
附录:nginx.conf配置详解
#运行用户user www-data; #启动进程,通常设置成和cpu的数量相等worker_processes 1;#全局错误日志及PID文件error_log /var/log/nginx/error.log;pid /var/run/nginx.pid;#工作模式及连接数上限events { use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能 worker_connections 1024;#单个后台worker process进程的最大并发链接数 # multi_accept on; }#设定http服务器,利用它的反向代理功能提供负载均衡支持http { #设定mime类型,类型由mime.type文件定义 include /etc/nginx/mime.types; default_type application/octet-stream; #设定日志格式 access_log /var/log/nginx/access.log; #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用, #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfile on; #tcp_nopush on; #连接超时时间 #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; #开启gzip压缩 gzip on; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; #设定请求缓冲 client_header_buffer_size 1k; large_client_header_buffers 4 4k; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; #设定负载均衡的服务器列表 upstream mysvr { #weigth参数表示权值,权值越高被分配到的几率越大 #本机上的Squid开启3128端口 server 192.168.8.1:3128 weight=5; server 192.168.8.2:80 weight=1; server 192.168.8.3:80 weight=6; } server { #侦听80端口 listen 80; #定义使用www.xx.com访问 server_name www.xx.com; #设定本虚拟主机的访问日志 access_log logs/www.xx.com.access.log main; #默认请求 location / { root /root; #定义服务器的默认网站根目录位置 index index.php index.html index.htm; #定义首页索引文件的名称 fastcgi_pass www.xx.com; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include /etc/nginx/fastcgi_params; } # 定义错误提示页面 error_page 500 502 503 504 /50x.html; location = /50x.html { root /root; } #静态文件,nginx自己处理 location ~ ^/(images|javascript|js|css|flash|media|static)/ { root /var/www/virtual/htdocs; #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。 expires 30d; } #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置. location ~ \.php$ { root /root; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name; include fastcgi_params; } #设定查看Nginx状态的地址 location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus"; auth_basic_user_file conf/htpasswd; } #禁止访问 .htxxx 文件 location ~ /\.ht { deny all; } }}
- 在Linux里安装、启动nginx
- 在Linux里安装、启动nginx
- 在Linux里安装、启动nginx
- nginx linux 安装、启动、升级
- Nginx(1) Linux 快速安装,启动nginx
- 在 Linux 下安装 Nginx
- nginx在linux安装笔记
- Nginx在linux下安装
- 在linux上安装nginx
- 在linux上安装nginx
- 在Linux上安装Nginx
- linux下安装nginx并启动
- linux下nginx编译安装启动
- linux上安装nginx,启动及停止
- Linux:在已安装nginx情况下安装nginx模块
- 在U盘里安装Puppy linux
- 在linux里安装mysql(二进制)
- 如何在Linux里安装chrome
- 究极java工程师第一步基本服务搭建(二),redhat6.4配置yum源
- 《深入理解mybatis原理》 MyBatis缓存机制的设计与实现
- Vitalik Buterin:以太坊Casper惩罚条件的最小化
- Linux学习笔记|一道题搞清楚目录与文件权限设置
- ZooKeeper_Java操作(创建节点/获取节点/删除节点)
- 在Linux里安装、启动nginx
- Problem C: 时间类的常量
- JVM调优方法(转)
- 设计模式学习--组合模式
- 分布式基础理论
- 构建嵌入式Linux + web+sqlite +php5.0 平台
- NJCTF-easy_crypto writeup
- 人人都可以做深度学习应用:入门篇
- Problem D: 时间类的12小时制输出