原创

Nginx安装与配置


二十五、Nginx安装与配置

1、Nginx安装

  • 安装依赖包:
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
  • 要安装 PCRE

    PCRE 作用是让 Nginx 支持 Rewrite 功能。

    1、下载 PCRE 安装包,下载地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

[root@bogon src]# cd /usr/local/src/
[root@bogon src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

img

​ 2、解压安装包:

[root@bogon src]# tar zxvf pcre-8.35.tar.gz

​ 3、进入安装包目录

[root@bogon src]# cd pcre-8.35

​ 4、编译安装

[root@bogon pcre-8.35]# ./configure
[root@bogon pcre-8.35]# make && make install

​ 5、查看pcre版本

[root@bogon pcre-8.35]# pcre-config --version

img

  • 开始安装Nginx解压文件

    cd /usr/local/
    tar -zxvf nginx-1.23.2.tar.gz
    mv nginx-1.23.2 nginx
    
  • 编译和安装

    ./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
    

    说明:

    nginx大部分常用模块,编译时./configure –help以–without开头的都默认安装。

    • –prefix=PATH : 指定nginx的安装目录。默认 /usr/local/nginx
    • –conf-path=PATH : 设置nginx.conf配置文件的路径。nginx允许使用不同的配置文件启动,通过命令行中的-c选项。默认为prefix/conf/nginx.conf
    • –user=name: 设置nginx工作进程的用户。安装完成后,可以随时在nginx.conf配置文件更改user指令。默认的用户名是nobody。–group=name类似
    • –with-pcre : 设置PCRE库的源码路径,如果已通过yum方式安装,使用–with-pcre自动找到库文件。使用–with-pcre=PATH时,需要从PCRE网站下载pcre库的源码(版本4.4 – 8.30)并解压,剩下的就交给Nginx的./configure和make来完成。perl正则表达式使用在location指令和 ngx_http_rewrite_module模块中。
    • –with-zlib=PATH : 指定 zlib(版本1.1.3 – 1.2.5)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module时需要使用zlib 。
    • –with-http_ssl_module : 使用https协议模块。默认情况下,该模块没有被构建。前提是openssl与openssl-devel已安装
    • –with-http_stub_status_module : 用来监控 Nginx 的当前状态
    • –with-http_realip_module : 通过这个模块允许我们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意义在于能够使得后台服务器记录原始客户端的IP地址
    • –add-module=PATH : 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要重新编译(Tengine可以在新加入module时无需重新编译)
    make && make install
    
  • 启动和关闭服务

    ./usr/local/nginx/sbin/nginx 
    ./usr/local/nginx/sbin/nginx -s top  
    

2、Nginx设置开机自动启动(centos 7):

​ centos 7以上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度。关于Systemd的详情介绍在这里。Systemd服务文件以 .service结尾,比如现在要建立nginx为开机启动,如果用yum install命令安装的,yum命令会自动创建nginx.service文件,直接用命令:1:systemcel enable nginx.service设置开机启动即可。

在这里我是用源码编译安装的,所以要手动创建nginx.service服务文件。

开机没有登陆情况下就能运行的程序,存在系统服务(system)里,即:

  • /etc/systemd/system/在系统服务目录里创建nginx.service文件:
vi /etc/systemd/system/nginx.service

内容如下:

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

***参数解释:*Description:描述服务 After: 描述服务类别 [Service] 服务运行参数的设置 Type=forking 是后台运行的形式 ExecStart 为服务的具体运行命令 ExecReload 为重启命令 ExecStop 为停止命令 PrivateTmp=True 表示给服务分配独立的临时空间 注意:[Service]的启动、重启、停止命令全部要求使用绝对路径

  • 然后重载系统服务使其生效
		systemctl daemon-reload
  • 设置开机启动

    systemctl enable nginx.service
    

    其它命令:

    systemctl start nginx.service (启动nginx服务)
    systemctl stop nginx.service (停止nginx服务)
    systemctl disable nginx.service (停止开机自启动)
    systemctl status nginx.service (查看服务当前状态)
    systemctl restart nginx.service (重新启动服务)
    systemctl list-units --type=service (查看所有已启动的服务)
    

3、Nginx配置文件(部署网站)

vi /usr/local/nginx/conf/nginx.conf

配置内容如下:

server {
        listen       80;  #设置访问端口
        server_name  localhost;

        #charset koi8-r;
		
        #access_log  logs/host.access.log  main;

        location / {
            root   html;  #设置网站源码文件目录,默认是nginx安装目录下的html文件
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

  • 配置多个网站

    ​ 说明:如需配置多个网站,在nginx.conf中再添加一个server框架即可,例如:

    #网站1:
    server {
            listen       80;  #设置访问端口
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
                root   html;  #设置网站源码文件目录,默认是nginx安装目录下的html文件
                index  index.html index.htm;
           	 }
    
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    #网站2:
    server {
            listen       81;  #设置访问端口
            server_name  localhost;
    
            location / {
                root   html;  #设置网站源码文件目录,默认是nginx安装目录下的html文件
                index  index.html index.htm;
            	}
    
            }
    
教程