Nginx 安装配置 ==========  Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 在高连接并发的情况下,Nginx是Apache服务器不错的替代品。 * * * Nginx 安装 -------- 系统平台:CentOS release 6.6 (Final) 64位。 ### 一、安装编译工具及库文件 ```java 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](http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz) ```java [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 ```  2、解压安装包: ```java [root@bogon src\]# tar zxvf pcre-8.35.tar.gz ``` 3、进入安装包目录 ```java [root@bogon src\]# cd pcre-8.35 ``` 4、编译安装 ```java [root@bogon pcre-8.35\]# ./configure [root@bogon pcre-8.35\]# make && make install ``` 5、查看pcre版本 ```java [root@bogon pcre-8.35\]# pcre-config --version ```  ### 安装 Nginx 1、下载 Nginx,下载地址:[https://nginx.org/en/download.html](https://nginx.org/en/download.html) ```java [root@bogon src\]# cd /usr/local/src/ [root@bogon src\]# wget http://nginx.org/download/nginx-1.6.2.tar.gz ```  2、解压安装包 ```java [root@bogon src\]# tar zxvf nginx-1.6.2.tar.gz ``` 3、进入安装包目录 ```java [root@bogon src\]# cd nginx-1.6.2 ``` 4、编译安装 ```java [root@bogon nginx-1.6.2\]# ./configure --prefix=/usr/local/webserver/nginx --with-http\_stub\_status\_module --with-http\_ssl\_module --with-pcre=/usr/local/src/pcre-8.35 [root@bogon nginx-1.6.2\]# make [root@bogon nginx-1.6.2\]# make install ``` 5、查看nginx版本 ```java [root@bogon nginx-1.6.2\]# /usr/local/webserver/nginx/sbin/nginx -v ```  到此,nginx安装完成。 * * * Nginx 配置 -------- 创建 Nginx 运行使用的用户 www: ```java [root@bogon conf\]# /usr/sbin/groupadd www [root@bogon conf\]# /usr/sbin/useradd -g www www ``` 配置nginx.conf ,将/usr/local/webserver/nginx/conf/nginx.conf替换为以下内容 ```java \[root@bogon conf\]# cat /usr/local/webserver/nginx/conf/nginx.conf user www www; worker\_processes 2; #设置值和CPU核心数一致 error\_log /usr/local/webserver/nginx/logs/nginx\_error.log crit; #日志位置和日志级别 pid /usr/local/webserver/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker\_rlimit\_nofile 65535; events { use epoll; worker\_connections 65535; } http { include mime.types; default\_type application/octet-stream; log\_format main '$remote\_addr - $remote\_user \[$time\_local\] "$request" ' '$status $body\_bytes\_sent "$http\_referer" ' '"$http\_user\_agent" $http\_x\_forwarded\_for'; #charset gb2312; server\_names\_hash\_bucket\_size 128; client\_header\_buffer\_size 32k; large\_client\_header\_buffers 4 32k; client\_max\_body\_size 8m; sendfile on; tcp\_nopush on; keepalive\_timeout 60; tcp\_nodelay on; fastcgi\_connect\_timeout 300; fastcgi\_send\_timeout 300; fastcgi\_read\_timeout 300; fastcgi\_buffer\_size 64k; fastcgi\_buffers 4 64k; fastcgi\_busy\_buffers\_size 128k; fastcgi\_temp\_file\_write\_size 128k; gzip on; gzip\_min\_length 1k; gzip\_buffers 4 16k; gzip\_http\_version 1.0; gzip\_comp\_level 2; gzip\_types text/plain application/x-javascript text/css application/xml; gzip\_vary on; #limit\_zone crawler $binary\_remote\_addr 10m; #下面是server虚拟主机的配置 server { listen 80;#监听端口 server\_name localhost;#域名 index index.html index.htm index.php; root /usr/local/webserver/nginx/html;#站点目录 location ~ .\*\\.(php|php5)?$ { #fastcgi\_pass unix:/tmp/php-cgi.sock; fastcgi\_pass 127.0.0.1:9000; fastcgi\_index index.php; include fastcgi.conf; } location ~ .\*\\.(gif|jpg|jpeg|png|bmp|swf|ico)$ { expires 30d; # access\_log off; } location ~ .\*\\.(js|css)?$ { expires 15d; # access\_log off; } access\_log off; } } ``` 检查配置文件nginx.conf的正确性命令: ```java [root@bogon conf\]# /usr/local/webserver/nginx/sbin/nginx -t ```  * * * 启动 Nginx -------- Nginx 启动命令如下: ```java \[root@bogon conf\]# /usr/local/webserver/nginx/sbin/nginx ```  * * * 访问站点 ---- 从浏览器访问我们配置的站点ip:  * * * Nginx 其他命令 ---------- 以下包含了 Nginx 常用的几个命令: ```java /usr/local/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件 /usr/local/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx /usr/local/webserver/nginx/sbin/nginx -s stop # 停止 Nginx ``` * * * Nginx 开机自启 ---------- centos 7以上是用Systemd进行系统初始化的,Systemd 是 Linux 系统中最新的初始化系统(init),它主要的设计目标是克服 sysvinit 固有的缺点,提高系统的启动速度。关于Systemd的详情介绍在这里。 Systemd服务文件以 .service结尾,比如现在要建立nginx为开机启动,如果用`yum install`命令安装的,yum命令会自动创建nginx.service文件,直接用命令: 1:**systemcel enable nginx.service** 设置开机启动即可。 在这里我是用源码编译安装的,所以要手动创建nginx.service服务文件。 开机没有登陆情况下就能运行的程序,存在系统服务(system)里,即: /lib/systemd/system/ 1. 在系统服务目录里创建nginx.service文件 vi /lib/systemd/system/nginx.service 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\]的启动、重启、停止命令全部要求使用绝对路径 \[Install\]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3 保存退出。 **2:设置开机启动** >**systemctl enable nginx.service** 自此,重新centos后,nginx就自动启动了 3:nginx其他命令: >systemctl start nginx.service (启动nginx服务) systemctl stop nginx.service (停止nginx服务) systemctl enable nginx.service (设置开机自启动) systemctl disable nginx.service (停止开机自启动) systemctl status nginx.service (查看服务当前状态) systemctl restart nginx.service (重新启动服务) systemctl list-units --type=service (查看所有已启动的服务) nginx开启SSL ---------- ``` 进入到nginx解压文件夹下 ./configure --with-http_ssl_module 如果报错./configure: error: SSL modules require the OpenSSL library说明openssl未安装,安装: #检查安装 yum list installed | grep openssl #安装 yum install -y openssl openssl-devel #编译 make #更新nginx cp 解压目录下/objs/nginx /usr/local/nginx/sbin/nginx #检查是否成功 /usr/local/nginx/sbin/nginx -t #重启nginx服务 cd /usr/local/nginx/sbin ## 检查是否安装此模块 /usr/local/nginx/sbin/nginx -V ``` 本文转自 [https://www.runoob.com/linux/nginx-install-setup.html](https://www.runoob.com/linux/nginx-install-setup.html),如有侵权,请联系删除。
评论