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:

[[email protected] ~]$ 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

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

[[email protected] ~]# 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";

猜你喜欢

转载自zsjg13.iteye.com/blog/2231557