`
zsjg13
  • 浏览: 145093 次
  • 性别: Icon_minigender_1
  • 来自: 安徽
社区版块
存档分类
最新评论

Nginx起步

阅读更多

1、安装先决条件:

(1)GCC——GNU Compiler Collection

    Nginx is a program written in C, so you will first need to install a compiler tool such as the GNU Compiler Collection (GCC)on your system.

    First, make sure it isn't already installed on your system:

[alex@example.com ~]$ gcc

    GCC can be installed using the default repository of your package manager.

# yum groupinstall "Development Tools"

或者

# apt-get install build-essentials

 

(2)The PCRE library

The Perl Compatible Regular Expression(PCRE)library is required for compiling Nginx. The Rewrite and HTTP Core modules of Nginx use PCRE for the syntax of their regular expressions.

# yum install pcre pcre-devel

或者,安装所有PCRE相关的包:

# yum install pcre*

如果是apt-get,则是:

# apt-get install libpcre3 libpcre3-dev

【pcre——provides the compiled version of the library

pcre-devel——provides development headers and source for compiling projects】

 

(3)The zlib library

The zlib library provides developers with compression algrithms, It is required for the use of gzip compression in various modules of Nginx。

Using yum:

# yum install zlib zlib-devel

Using apt-get

# apt-get install zlib1g zlib1g-dev

 

(4)OpenSSL

 The Open SSL library will be used by Nginx to serve secure web pages.

Using Yum: # yum install openssl openssl-devel

Using apt-get: # apt-get install openssl openssl-dev

 

Nginx is compatible with many computer architectures and operating systems such 

as Windows, Linux, Mac OS, FreeBSD, and Solaris. The application runs fine on 32- 

and 64-bit architectures.

 

2、安装Nginx

(1)Downloading and extracting

# mkdir src && cd src

# wget http://nginx.org/download/nginx-1.2.9.tar.gz

# tar zxf nginx-1.2.9.tar.gz 

 

(2)Configure options

The configuration step allows you to select a number of options that will not be editable after the program is built, as it has a direct impact on the project binaries. Consequently, it is a very important stage that you need to fllow carefully if you want to avoid surprises later, such as the lack of a specific module or files being located in a random folder.

 

(2.1)最简单的方式

# ./configure

# make

# make install

最终,默认会被安装到 /usr/local/nginx. 需要以root身份执行 make install,这要根据 /usr/local目录上授予的权限的情况。

 

(2.2)Path options

# ./configure --conf-path=/etc/nginx/nginx.conf

或者也可以指定--prefix,有很多这样的path配置,这里就不说了。

 

(2.3)Prerequisites options

Prerequisites come in the form of libraries and binaries. You should by now have 

them all installed on your system. Yet, even though they are present on your 

system, there may be occasions where the configuration script cannot locate them.

The reasons might be diverse, for example, if they were installed in non-standard 

directories. In order to solve such problems, you are given the option to specify 

the path of prerequisites 

 

(2.4)Module options

Modules need to be selected before compiling the application. Some are enabled by default and  some

need to be enabled manually.

 

(2.5)Miscellaneous options

Other options are available in the configuration script, for example, regarding the mail server proxy feature or event management.

 

(3)配置样例

(3.1)Nginx的安装目录

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

(3.2)Regular HTTP and HTTPs servers

./configure --user=www-data --group=www-data --with-http_ssl_module --with-http_realip_module

(3.3)All modules enabled

所有的模块都开启,由你来决定在运行时是否用它们:

./configure --user=www-data --group=www-data --with-http_ssl_module 

--with-http_realip_module --with-http_addition_module --with-http_xslt_

module --with-http_image_filter_module --with-http_geoip_module --with-

http_sub_module --with-http_dav_module --with-http_flv_module --with-

http_mp4_module --with-http_gzip_static_module --with-http_random_index_

module --with-http_secure_link_module --with-http_stub_status_module 

--with-http_perl_module --with-http_degradation_module

 

With this setup, all optional modules are enabled, thus requiring additional libraries 

to be installed.

 

3、控制Nginx服务

(1)Daemons and services

When started from the command line, a daemon immediately returns the prompt, and in most cases, does not even bother outputting data to the terminal.

 

Consequently, when starting Nginx you will not see any text appear on the screen 

and the prompt will return immediately.

 

(2)User and group

A very common source of troubles when setting up Nginx is invalid file access permissions——due to

a user or group misconfiguration, you often end up getting 403 forbidden HTTP errors because Nginx

cannot access the requested files.

 

There are two levels of processes with possibly different permission sets:

•  The Nginx master process, which should be started as root. In most Unix-like 

systems, processes started with the root account are allowed to open TCP 

sockets on any port, whereas other users can only open listening sockets on 

a port above 1024. If you do not start Nginx as root, standard ports such as 

80 or 443 will not be accessible. Additionally, the user directive that allows 

you to specify a different user and group for the worker processes will not be 

taken into consideration.

•  The Nginx worker processes, which are automatically spawned by the 

master process under the account you specified in the configuration file 

with the user directive (detailed in Chapter 2, Basic Nginx Configuration ). The 

configuration setting takes precedence over the configure switch you may 

have entered at compile time. If you did not specify any of those, the worker 

processes will be started as user nobody, and group nobody (or nogroup 

depending on your OS).

 

(3)Nginx command-line switches

# cd /usr/local/nginx/sbin

# ./nginx -h

 

(4)Starting and stopping the daemon

You can start Nginx by running the Nginx binary without any switches.

 

nginx –s stop Stops the daemon immediately (using the TERM signal)

nginx –s quit Stops the daemon gracefully (using the QUIT signal)

nginx –s reopen Reopens the log files

nginx –s reload Reloads the configuration

 

注意:因为执行以上操作时,都会先解析配置文件,如果该文件有问题,不管什么命令都会失效,甚至无法停止服务。此时可以用下面的命令:

[root@example.com ~]# killall nginx

 

4、测试配置文件

/usr/local/nginx/sbin/nginx –t

A valid configuration file does not necessarily mean Nginx will start though as there might be additional

problems such as socket issues, invalid paths, or incorrect access permissions.

 

很显然,当服务器处在生产环境中时,操作配置文件就是一个危险的事情。最好的办法就是把新配置文件放到一个独立的临时文件中,测试该临时文件。

./nginx –t –c /home/alex/test.conf

当确认该配置文件是可以的时候,就可以替换掉那个旧的了:

$ cp -i /home/alex/test.conf /usr/local/nginx/conf/nginx.conf ./nginx –t –c /home/alex/test.conf

cp: erase 'nginx.conf' ? yes

$ ./nginx –s reload

 

下面的命令不仅仅可以告诉你当前的 Nginx build version,还能得知构建时传的参数

$ ./nginx -V

为什么说它很重要呢?嗯,如果你尝试去使用一个模块,而该模块在pre-compilation阶段也就是运行configure脚本时没有把这个模块包含进来,当用指令开启这个模块时就会出现配置错误。你的第一反应

是不是语法错啦?第2反应有没有包含该模块啊?运行nginx -V就能回答你的问题了。

 

另外,-g 选项可以指定额外的配置指令(假如在配置文件中没有包含)

$ ./nginx –g "timer_resolution 200ms";

分享到:
评论

相关推荐

    Nginx经典教程

    - **新手起步**:适合Nginx初学者的基础指南。 - **URI匹配策略**:讲解Nginx如何高效地匹配URL路径。 - **日志服务详解**:介绍Nginx的日志记录功能,包括定制化日志格式和高级日志分析。 - **安全与访问控制**:...

    agentzh 的 Nginx 教程(版本 2016.07.21)整理成pdf 添加书签

    - **Nginx新手起步**:入门级教程,适合初学者。 - **Nginx是如何匹配URI的**:讲解Nginx的URI匹配机制。 - **Nginx变量漫谈**:如上所述。 - **Nginx配置指令的执行顺序**:如上所述。 - **Nginx的if是邪恶的*...

    nginx 的一些配置

    例如,在“Nginx新手起步”中,可能涵盖了最基础的安装和配置流程,而在“Nginx是如何匹配URI的”部分,则会深入讲解Nginx的请求处理机制以及如何根据location指令进行URL匹配。在“Nginx变量漫谈”中,将会探讨...

    Nginx教程.pdf

    1. **新手起步**:介绍了Nginx的基础概念和安装配置过程,适合初学者快速入门。 2. **Nginx是如何匹配URI的**:详细解释了Nginx如何解析和匹配URL,以及location块的工作原理。 3. **Nginx变量漫谈**:如上所述,...

    springboot-nginx-redis-session共享、TCPUDP负载均衡.zip

    Spring Boot的核心特性包括自动配置、起步依赖、内嵌HTTP服务器(如Tomcat)等,使得开发者能够快速搭建和运行应用程序。在这个项目中,Spring Boot作为后端服务的基础,提供了与数据库交互的接口,同时也支持...

    agentzh-nginx-tutorials-zhcn.pdf

    - **新手起步**:介绍Nginx的基本安装与配置方法。 - **Nginx是如何匹配URI的**:深入探讨Nginx如何识别和解析URL。 - **Nginx的if是邪恶的**:讨论Nginx中if指令的潜在问题及其替代方案。 - **Nginx子请求**:讲解...

    agentzh 的 Nginx 教程(版本 2019.05.08)openresty 电子书

    教程还涉及到了 Nginx 的一些核心主题,包括新手起步、URI 匹配规则、Nginx 的 if 指令的讨论、子请求处理、静态文件服务、日志服务等。这些是 Nginx 配置和使用中的关键方面,对于深入理解和高效使用 Nginx 有着...

    Springboot+mybaits+mysql+redis+nginx,仓库管理系统。毕业设计

    同时,Spring Boot通过起步依赖(Starter POMs)方便地引入各种功能模块,如Spring Data JPA、MyBatis等。 2. **MyBatis**:MyBatis是一个持久层框架,它允许开发者编写SQL语句并与Java对象映射,避免了传统的JDBC...

    dubbo、redis、solr、activeMQ、freemarker、nginx、管理系统、搜索系统

    通过自动配置和起步依赖,SpringBoot可以让你快速创建独立运行的、生产级别的Java应用。 3. **Redis**: Redis是一款开源的、基于键值对的数据存储系统,常被用作数据库、缓存和消息中间件。它的高速读写性能和丰富...

    PHP编程起步.s.pdg

    首先,它会讲解PHP的安装与配置,包括如何在不同的操作系统(如Windows、Linux和Mac OS)上搭建PHP运行环境,以及如何与Apache或Nginx等Web服务器进行集成。 接下来,读者将学习PHP的基本语法,包括变量、数据类型...

    PHP编程自学起步教程

    1. **PHP环境搭建**:如何安装和配置PHP运行环境,通常包括PHP、Apache(或Nginx)服务器和MySQL数据库,形成LAMP(Linux、Apache、MySQL、PHP)或WAMP(Windows、Apache、MySQL、PHP)开发环境。 2. **PHP基础语法...

    OpenResty 最佳实践

    在文档中,介绍了Nginx的基本操作,例如新手起步、location匹配规则、静态文件服务、日志记录、反向代理、负载均衡等。同时,也提到了一些实际开发中需要注意的陷阱和常见错误。 文档对OpenResty环境搭建的说明覆盖...

    OpenResty-Best-Practices.pdf

    在新手起步章节,我们会学习到如何在不同操作系统平台(如Windows、CentOS、Ubuntu、Mac OS X)上进行OpenResty的环境搭建。 接下来,关于Lua编程的介绍。Lua是一种轻量级的脚本语言,广泛应用于嵌入式系统、游戏...

    PHP编程起步自学教程PDF

    - **安装服务器环境**:指导如何安装配置Apache或Nginx服务器。 - **PHP安装与配置**:详细介绍如何在Windows/Linux/MacOS系统上安装PHP,并进行必要的配置。 ##### 1.4 初识PHP - **Hello World**:通过编写第一个...

    ngx_openresty_lua_技术交流实践

    - **Nginx新手起步**:掌握Nginx的基本配置,如location匹配规则、静态文件服务、日志记录、反向代理和负载均衡。 - **常见错误与陷阱**:学习Nginx常见配置错误和性能优化技巧。 ### OpenResty环境搭建 - **不同...

    乐聊tv源码

    Spring-Boot的特点在于其“开箱即用”的特性,提供了内置的Tomcat服务器、自动配置和大量的起步依赖,使得开发者可以快速搭建并运行应用。 在乐聊TV源码中,Nginx被用作RTMP(Real-Time Messaging Protocol)服务器...

    《OpenResty最佳实践》 .pdf

    - **新手起步**:介绍了Nginx的基本概念和配置方式,例如location匹配规则、静态文件服务、日志记录等。 - **反向代理和负载均衡**:Nginx的一大优势在于其反向代理功能和负载均衡能力,本书详细讲解了这些知识,并...

    基于Docker构建CICD工具链详细文档

    (三)Gitlab Runner搭建起步 (四)使用maven镜像自动化构建Spring Boot项目 (五)使用ssh命令自动化部署 (六)使用Apifox进行自动化测试 (七)使用Jmeter进行自动化压测 (八)用nginx收集测试报告 (九...

Global site tag (gtag.js) - Google Analytics