zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Nginx之一:Nginx的编译安装详解程序员

安装Nginx程序员 详解 编译 之一
2023-06-13 09:20:03 时间

一、Nginx简介

官方网址:http://nginx.org/

Nginx是由1994年毕业于俄罗斯国立莫斯科鲍曼科技大学的同学为俄罗斯rambler.ru公司开发的,开发工作最早从2002年开始,第一次公开发布时间是2004年10月4日,版本号是0.1.0

Nginx的版本分为开发版、稳定版和过期版,nginx以功能丰富著称,它即可以作为http服务器,也可以作为反向代理服务器或者邮件服务器,能够快速的响应静态网页的请求,支持FastCGI/SSL/Virtual Host/URL Rwrite/Gzip/HTTP Basic Auth等功能,并且支持第三方的功能扩展。

Nginx是单进程单线程模型,即启动的工作进程只有一个进程响应客户端请求,不像apache可以在一个进程内启动多个线程响应可请求,因此在内存占用上比apache小的很多。Nginx维持一万个非活动会话只要2.5M内存。Nginx和Mysql是CPU密集型的,就是对CPU的占用比较大,默认session在本地文件保存,支持将session保存在memcache,但是memcache默认支持最大1M的课hash对象。

 

二、实验环境

操作系统:CentOS6.5

Nginx版本:nginx-1.14.0

 

三、安装

1.依赖环境介绍

gcc gcc-c++

gcc为GNU Compiler Collection的缩写,可以编译C和C++源代码等,它是GNU开发的C和C++以及其他很多种语言 的编译器(最早的时候只能编译C,后来很快进化成一个编译多种语言的集合,如Fortran、Pascal、Objective-C、Java、Ada、 Go等。) 

gcc 在编译C++源代码的阶段,只能编译 C++ 源文件,而不能自动和 C++ 程序使用的库链接(编译过程分为编译、链接两个阶段,注意不要和可执行文件这个概念搞混,相对可执行文件来说有三个重要的概念:编译(compile)、链接(link)、加载(load)。源程序文件被编译成目标文件,多个目标文件连同库被链接成一个最终的可执行文件,可执行文件被加载到内存中运行)。因此,通常使用 g++ 命令来完成 C++ 程序的编译和连接,该程序会自动调用 gcc 实现编译。 

gcc-c++也能编译C源代码,只不过把会把它当成C++源代码,后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序;后缀为.cpp的,两者都会认为是c++程序,注意,虽然c++是c的超集,但是两者对语法的要求是有区别的。

make automake

make是一个用来控制可执行文件和其他一些从源文件来的非源代码文件版本的软件。Make可以从一个名为makefile的文件中获得如何构建你所写程序的依赖关系,Makefile中列出了每个目标文件以及如何由其他文件来生成它。
automake是一个从Makefile.am文件自动生成Makefile.in的工具。为了生成Makefile.in,automake还需用到perl,由于automake创建的发布完全遵循GNU标准,所以在创建中不需要perl。libtool是一款方便生成各种程序库的工具。

autoconf

autoconf是用来生成自动配置软件源代码脚本(configure)的工具

pcre pcre-devel

在Nginx编译需要 PCRE(Perl Compatible Regular Expression),因为Nginx 的Rewrite模块和HTTP 核心模块会使用到PCRE正则表达式语法。

zlip zlib-devel

nginx启用压缩功能的时候,需要此模块的支持。

openssl openssl-devel 

开启SSL的时候需要此模块的支持。

libtool

libtool是一个通用库支持脚本,将使用动态库的复杂性隐藏在统一、可移植的接口中;使用libtool的标准方法,可以在不同平台上创建并调用动态库。 

libtool主要的一个作用是在编译大型软件的过程中解决了库的依赖问题;将繁重的库依赖关系的维护工作承担下来,从而释放了程序员的人力资源。libtool提供统一的接口,隐藏了不同平台间库的名称的差异等细节,生成一个抽象的后缀名为la高层库libxx.la(其实是个文本文件),并将该库对其它库的依赖关系,都写在该la的文件中。

 

2.依赖环境安装

[[email protected] ~]# yum -y install gcc gcc-c++ make automake autoconf pcre pcre-devel zlib zlib-devel openssl openssl-devel libtool 

Loaded plugins: fastestmirror, refresh-packagekit, security 

Loading mirror speeds from cached hostfile 

 * base: ftp.sjtu.edu.cn 

 * extras: ftp.sjtu.edu.cn 

 * updates: ftp.sjtu.edu.cn 

Setting up Install Process 

Package gcc-4.4.7-18.el6_9.2.x86_64 already installed and latest version 

Package 1:make-3.81-23.el6.x86_64 already installed and latest version 

Package automake-1.11.1-4.el6.noarch already installed and latest version 

Package autoconf-2.63-5.1.el6.noarch already installed and latest version 

Package zlib-1.2.3-29.el6.x86_64 already installed and latest version 

Package libtool-2.2.6-15.5.el6.x86_64 already installed and latest version 

Resolving Dependencies 

-- Running transaction check 

--- Package gcc-c++.x86_64 0:4.4.7-18.el6_9.2 will be installed 

-- Processing Dependency: libstdc++-devel = 4.4.7-18.el6_9.2 for package: gcc-c++-4.4.7-18.el6_9.2.x86_64 

-- Processing Dependency: libstdc++ = 4.4.7-18.el6_9.2 for package: gcc-c++-4.4.7-18.el6_9.2.x86_64 

--- Package openssl.x86_64 0:1.0.1e-15.el6 will be updated 

--- Package openssl.x86_64 0:1.0.1e-57.el6 will be an update
... ...
... ...
... ...
Installed: gcc-c++.x86_64 0:4.4.7-18.el6_9.2 openssl-devel.x86_64 0:1.0.1e-57.el6 pcre-devel.x86_64 0:7.8-7.el6 zlib-devel.x86_64 0:1.2.3-29.el6 Dependency Installed: keyutils-libs-devel.x86_64 0:1.4-5.el6 krb5-devel.x86_64 0:1.10.3-65.el6 libcom_err-devel.x86_64 0:1.41.12-23.el6 libkadm5.x86_64 0:1.10.3-65.el6 libselinux-devel.x86_64 0:2.0.94-7.el6 libsepol-devel.x86_64 0:2.0.41-4.el6 libstdc++-devel.x86_64 0:4.4.7-18.el6_9.2 Updated: openssl.x86_64 0:1.0.1e-57.el6 pcre.x86_64 0:7.8-7.el6 Dependency Updated: e2fsprogs.x86_64 0:1.41.12-23.el6 e2fsprogs-libs.x86_64 0:1.41.12-23.el6 keyutils.x86_64 0:1.4-5.el6 keyutils-libs.x86_64 0:1.4-5.el6 krb5-libs.x86_64 0:1.10.3-65.el6 krb5-workstation.x86_64 0:1.10.3-65.el6 libcom_err.x86_64 0:1.41.12-23.el6 libselinux.x86_64 0:2.0.94-7.el6 libselinux-python.x86_64 0:2.0.94-7.el6 libselinux-utils.x86_64 0:2.0.94-7.el6 libss.x86_64 0:1.41.12-23.el6 libstdc++.x86_64 0:4.4.7-18.el6_9.2 Complete!

3.下载Nginx安装包

[[email protected] tmp]# wget http://nginx.org/download/nginx-1.14.0.tar.gz 

--2018-05-05 00:25:50-- http://nginx.org/download/nginx-1.14.0.tar.gz 

Resolving nginx.org... 95.211.80.227, 206.251.255.63, 2606:7100:1:69::3f, ... 

Connecting to nginx.org|95.211.80.227|:80... connected. 

HTTP request sent, awaiting response... 302 Found 

Location: http://113.215.26.194/files/71490000007FAB32/nginx.org/download/nginx-1.14.0.tar.gz [following] 

--2018-05-05 00:25:51-- http://113.215.26.194/files/71490000007FAB32/nginx.org/download/nginx-1.14.0.tar.gz 

Connecting to 113.215.26.194:80... connected. 

HTTP request sent, awaiting response... 200 OK 

Length: 1016272 (992K) [application/octet-stream] 

Saving to: “nginx-1.14.0.tar.gz” 

100%[================================================================== ] 1,016,272 1.29M/s in 0.8s 

2018-05-05 00:25:52 (1.29 MB/s) - “nginx-1.14.0.tar.gz” saved [1016272/1016272]

3.解压nginx-1.14.0.tar.gz

[[email protected] tmp]# tar -zxvf nginx-1.14.0.tar.gz -C /usr/local/src/ 

[[email protected] nginx-1.14.0]# pwd 

/usr/local/src/nginx-1.14.0 

[[email protected] nginx-1.14.0]# ls 

auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src

 4.侦测程序,生成MakeFile文件

[[email protected] nginx-1.14.0]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre

5.安装:make install

[[email protected] nginx-1.14.0]# make make install

6.启动、停止Nginx服务

[[email protected] ~]# /usr/local/nginx/sbin/nginx #启动Nginx 

nginx: [emerg] getpwnam("nginx") failed #没有Nginx用户 

[[email protected] ~]# /usr/local/nginx/sbin/nginx -s stop #停止Nginx 

[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload #不停止服务,重新加载配置文件

7.验证Nginx是否开启成功

[[email protected] ~]# ps aux|grep nginx 

root 2218 0.0 0.1 47320 1188 ? Ss 10:21 0:00 nginx: master process /usr/local/nginx/sbin/nginx 

nginx 2219 0.0 0.1 47756 1760 ? S 10:21 0:00 nginx: worker process 

root 2222 0.0 0.0 103256 836 pts/0 S+ 10:21 0:00 grep nginx

8.在浏览器中打开,效果如下

Nginx之一:Nginx的编译安装详解程序员

四、说明

1.在编译过程中,如果/usr/local/nginx目录不存在,make install会自动创建

2.在安装之前,先创建好nginx用户和nginx用户组

2504.html

服务器部署程序员系统优化网站设置运维