Nginx安装部署

一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。

1.选定源码目录

选定目录 /usr/local/

 

cd /usr/local/

 

2.安装PCRE

cd /usr/local/

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz

tar -zxvf pcre-8.21.tar.gz

cd pcre-8.21

./configure

make

make install

 

3.安装zlib

cd /usr/local/ 

wget http://zlib.net/zlib-1.2.8.tar.gz

tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8

./configure

make

make install

 

4.安装ssl

 

cd /usr/local/

wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz

tar -zxvf openssl-1.0.1c.tar.gz

./config

make

make install

 

5.安装nginx

 

Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:

 

cd /usr/local/

wget http://nginx.org/download/nginx-1.2.8.tar.gz

tar -zxvf nginx-1.2.8.tar.gz

cd nginx-1.2.8  

./configure --prefix=/usr/local/nginx 

make

make install

 

--with-pcre=/usr/src/pcre-8.21 指的是pcre-8.21 的源码路径。

--with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源码路径。

 

6.启动

确保系统的 80 端口没被其他程序占用,

/usr/local/nginx/sbin/nginx

 

检查是否启动成功:

netstat -ano|grep 80 有结果输入说明启动成功

 

 

打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

 

7.重启

/usr/local/nginx/sbin/nginx –s reload

 

8.修改配置文件

cd /usr/local/nginx/conf

vi nginx.conf

 

9.常用配置

#nginx运行用户和组

user    www www;  

#启动进程,通常设置成和cpu的数量相等

worker_processes  4;

 

#全局错误日志及PID文件

pid /var/run/nginx.pid;

error_log  /var/log/nginx/error.log;

 

events {

        #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能

use epoll;

                   #单个后台worker process进程的最大并发链接数

        worker_connections  10240;

}

#设定http服务器,利用它的反向代理功能提供负载均衡支持

http {

        include       mime.types;

 

        default_type  application/octet-stream;

 

         error_page 400 403 500 502 503 504  /50x.html;

 

        index index.html index.shtml

 

        autoindex off;

 

         fastcgi_intercept_errors on;

 

        sendfile        on;

 

        # These are good default values.

        tcp_nopush      on;

        tcp_nodelay     off;

 

        # output compression saves bandwidth

        gzip  off;

         #gzip_static on;

        #gzip_min_length  1k;

        gzip_http_version 1.0;

        gzip_comp_level 2;

        gzip_buffers  4 16k;

        gzip_proxied any;

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

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

        #gzip_vary on;

 

        server_name_in_redirect off;

 

#设定负载均衡的服务器列表

        upstream portals {

                  server 172.16.68.134:8082 max_fails=2 fail_timeout=30s;

                  server 172.16.68.135:8082 max_fails=2 fail_timeout=30s;

                            server 172.16.68.136:8082 max_fails=2 fail_timeout=30s;

                  server 172.16.68.137:8082 max_fails=2 fail_timeout=30s;

        }

 

        #upstream overflow {

         #       server 10.248.6.34:8090 max_fails=2 fail_timeout=30s;       

         #       server 10.248.6.45:8080 max_fails=2 fail_timeout=30s;       

        #}

 

        server {

                                     #侦听8080端口

                listen       8080;

                server_name  127.0.0.1;

 

                   #403404页面重定向地址

                   error_page  403 = http://www.e100.cn/ebiz/other/217/403.html;

                   error_page  404 = http://www.e100.cn/ebiz/other/218/404.html;

                   proxy_connect_timeout      90;

                   proxy_send_timeout         180;

                   proxy_read_timeout         180;

 

                   proxy_buffer_size 64k;

                   proxy_buffers 4 128k;

                   proxy_busy_buffers_size 128k;

 

 

                   client_header_buffer_size 16k;

                   large_client_header_buffers 4 64k;

 

                #proxy_send_timeout         3m;

                #proxy_read_timeout         3m;

                #proxy_buffer_size          4k;

                #proxy_buffers              4 32k;

 

                proxy_set_header Host $http_host;

                proxy_max_temp_file_size 0;

                #proxy_hide_header Set-Cookie;

                  

         #       if ($host != 'www.e100.cn' ) {

         #                rewrite ^/(.*)$ http://www.e100.cn/$1 permanent;

         #       }

 

 

               location / {

                       deny all;

               }

 

                   location ~ ^/resource/res/img/blue/space.gif {

                    proxy_pass http://tecopera;

               }

 

               location = / {

                   rewrite ^(.*)$  /ebiz/event/517.html last;

               }

 

 

 

                   location = /ebiz/event/517.html {

                    add_header Vary Accept-Encoding;

                    root /data/web/html;

                    expires 10m;

               }

 

 

 

 

               location = /check.html {

                    root /usr/local/nginx/html/;

                    access_log off;

               }

 

               location = /50x.html {

                    root /usr/local/nginx/html/;

                    expires 1m;

                    access_log off;

               }

 

 

              location = /index.html {

                       add_header Vary Accept-Encoding;

#定义服务器的默认网站根目录位置

                    root /data/web/html/ebiz;

                    expires 10m;

               }

#定义反向代理访问名称

                   location ~ ^/ecps-portal/* {

                   # expires 10m;

#重定向集群名称

                    proxy_pass http://portals;

                    #proxy_pass http://172.16.68.134:8082;

               }

 

                   location ~ ^/fetionLogin/* {

                   # expires 10m;

                    proxy_pass http://portals;

                    #proxy_pass http://172.16.68.134:8082;

                }

 

                   #location  ~ ^/business/* {                                                                      

                #   # expires 10m;                                                                                

                #    proxy_pass http://172.16.68.132:8088;                                                                   

                #    #proxy_pass http://172.16.68.134:8082;                                                       

                #}

 

                   location ~ ^/rsmanager/* {

                    expires 10m;

                    root /data/web/;

                    #proxy_pass http://rsm;

               }

#定义nginx处理的页面后缀

                   location ~* (.*)\.(jpg|gif|htm|html|png|js|css)$  {

                            root /data/web/html/;

#页面缓存时间为10分钟

                         expires 10m;

                   }

 

#设定查看Nginx状态的地址     

               location ~* ^/NginxStatus/ {

                    stub_status on;

                    access_log off;

                    allow 10.1.252.126;

                    allow 10.248.6.49;

                    allow 127.0.0.1;

                    deny all;

               }

         #       error_page   405 =200 @405;

         #       location @405

         #       {

         #                proxy_pass http://10.248.6.45:8080;

         #       }  

 

               access_log  /data/logs/nginx/access.log combined;

               error_log   /data/logs/nginx/error.log;

        }

         server {

                listen       8082;

 

                server_name  _;

               location = /check.html {

                    root /usr/local/nginx/html/;

                    access_log off;

               }

                  

        }

         server {

                   listen       8088;

                   server_name  _;

                   location ~ ^/* {

                   root /data/web/b2bhtml/;

                   access_log off;

         }                

         }

        server {

                listen       9082;

                server_name  _;

 

        #        location ~ ^/resource/* {

        #            expires 10m;

         #           root /data/web/html/;

         #       }

 

                location  / {

                     root /data/web/html/sysMaintain/;

                       if (!-f $request_filename) {

                            rewrite ^/(.*)$ /sysMaintain.html last;

                           }

                }

        }

 

}

<think>我们正在回答用户关于Nginx安装部署指南的问题。根据用户需求,我们需要提供清晰、结构化的安装部署步骤。同时,根据系统级指令,我们需要:1.所有行内数学表达式使用$...$格式(但本问题不涉及数学公式,可忽略)2.独立公式使用$$...$$格式(同样不涉及)3.使用中文回答4.生成相关问题5.在回答中引用的段落末尾添加引用标识(我们参考了多个引用,需要标注)参考的引用内容主要是关于Nginx安装部署的步骤。我们将结合这些引用,整理出一个通用的安装部署指南。用户的需求是“Nginx安装部署指南”,因此我们将包括以下内容:1.安装前的准备(如系统更新、依赖安装)2.两种安装方式:通过包管理器(如yum)安装和通过源码编译安装3.安装后的基本操作(启动、停止、重启、设置开机启动等)4.验证安装注意:不同的Linux发行版命令可能不同,我们将以常见的CentOS和Ubuntu为例,但用户需根据实际系统调整。根据引用[5],在CentOS上使用yum安装是一种简单的方式,而引用[1][3][4]则提到了源码编译安装,这种方式可以自定义模块。我们将按照以下结构组织回答:###Nginx安装部署指南####一、安装前准备1.更新系统2.安装依赖####二、安装方式#####方法一:通过包管理器安装(推荐新手)-CentOS7示例-Ubuntu示例#####方法二:通过源码编译安装(适合需要自定义模块的情况)1.下载源码2.解压并进入目录3.配置编译选项(可自定义模块和安装路径)4.编译并安装####三、安装后的操作1.启动Nginx2.设置开机启动3.检查运行状态4.停止和重启命令####四、验证安装1.访问默认页面2.检查端口监听####五、防火墙设置(如果需要)然后,根据引用内容,我们在相应步骤后添加引用标识。最后,生成相关问题。现在开始撰写回答:</think>### Nginx 安装部署指南 #### 一、安装前准备 1. **系统更新** 执行系统更新确保软件包最新(以 CentOS 为例): ```bash sudo yum update ``` 2. **安装依赖** 安装编译所需工具链: ```bash sudo yum install -y gcc make openssl-devel pcre-devel zlib-devel # CentOS sudo apt install -y build-essential libssl-dev libpcre3-dev zlib1g-dev # Ubuntu [^3][^5] --- #### 二、两种安装方式 ##### ▶ 方式1:包管理器安装(推荐新手) ```bash # CentOS/RedHat sudo yum install epel-release # 安装EPEL仓库[^5] sudo yum install nginx # Ubuntu/Debian sudo apt update sudo apt install nginx ``` **优点**:自动处理依赖和服务配置 **缺点**:模块自定义受限[^5] ##### ▶ 方式2:源码编译安装(需自定义模块时) ```bash wget https://nginx.org/download/nginx-1.24.0.tar.gz tar -zxvf nginx-1.24.0.tar.gz cd nginx-1.24.0 # 配置编译参数(示例启用SSL模块) ./configure --prefix=/usr/local/nginx \ --with-http_ssl_module \ --with-pcre # 编译并安装 make sudo make install [^1][^3][^4] ``` **关键参数说明**: - `--prefix`:指定安装路径(默认 `/usr/local/nginx`) - `--with-http_ssl_module`:启用HTTPS支持 - `--without-http_rewrite_module`:禁用不需要的模块[^4] --- #### 三、安装后操作 1. **启动与自启** ```bash # 源码安装方式 /usr/local/nginx/sbin/nginx # 包管理安装方式 sudo systemctl start nginx sudo systemctl enable nginx # 设置开机启动[^5] ``` 2. **验证安装** - 检查进程:`ps aux | grep nginx` - 访问默认页:`curl http://localhost` 或浏览器访问服务器IP - 查看版本:`nginx -v`[^1][^3] 3. **常用命令** ```bash nginx -s stop # 立即停止 nginx -s quit # 优雅停止 nginx -s reload # 重载配置[^2] ``` --- #### 四、防火墙配置 ```bash # 放行HTTP/HTTPS端口 sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload # CentOS # Ubuntu使用ufw sudo ufw allow 'Nginx Full' [^3][^5] ``` --- #### 五、目录结构说明 ``` /usr/local/nginx/ ├── sbin/nginx # 主程序 ├── conf/nginx.conf # 主配置文件 ├── logs/ # 访问/错误日志 └── html/ # 默认网站根目录[^2][^3] ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值