Debian8(jessie)编译安装NGINX1.15

实验环境

OS: debian_version_8.11 64位
CPU: Intel(R) Xeon(R) CPU E5-2630 v4 @ 2.20GHz
Mem: 8GB
Kernel: 3.16.0-10-amd64
Nginx: nginx-1.15.12

简要说明

1、安装包内网中已下载好,此文档中不演示
2、所有的源码包都在/apps/apps_src/
3、所有的服务安装路径都在/apps/xxxxx

编译安装过程
1、安装相关依赖包

apt update && apt -y install gcc make zlib1g* libssl-dev libpcre+*

2、解压缩下载包

tar zxvf nginx-1.15.12 && cd nginx-1.15.12

3、创建运行nginx的用户

groupadd -r -g 200 nginx
useradd -r -g 200 -u 200 -s /bin/false -d /dev/null -M nginx

4、查看可编译的选项

./configure --help

5、配置选项

./configure \
--prefix=/apps/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/apps/nginx/logs/error.log \
--http-log-path=/apps/nginx/logs/access.log \
--pid-path=/apps/nginx/run/nginx.pid \
--lock-path=/apps/nginx/lock/nginx.lock \
--http-client-body-temp-path=/apps/nginx/tmp/client \
--http-proxy-temp-path=/apps/nginx/tmp/proxy \
--http-fastcgi-temp-path=/apps/nginx/tmp/fastcgi \
--user=nginx --group=nginx \
--with-poll_module --with-threads \
--with-file-aio --with-pcre \
--with-http_realip_module \
--with-http_ssl_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_gunzip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module 2> ../nginx-1.15.12-configure.err

configure命令的作用是检测系统环境,配置nginx设置,最后成功后会生成Makefile文件。

6、编译

make -j 4 2> ../nginx-1.15.12-make.err

make是把刚生成的Makefile文件中的指令编译成计算机识别的二进制文件

7、安装

make -j 4 install 2> ../nginx-1.15.12-install.err

8、配置全局变量

echo "export PATH=$PATH:/apps/nginx/sbin" >> /etc/profile.d/nginx.sh 
source /etc/profile.d/nginx.sh

9、进入安装目录检查目录创建缺少目录

mkdir -v /apps/nginx/{lock,tmp}

10、修改nginx配置文件 /etc/nginx/nginx.conf

worker_processes  4;                #nginx进程数, 通常设置为和CPU核数一样

pid /apps/nginx/run/nginx.pid;

use epoll;                   #多路复用IO中的一种方式,可提高nginx性能。仅适用于linux2.6以上内核
worker_connections  1024;    #单个后台worker_processes进程的最大并发连接数,系统优化后可调整

server_tokens off;            #隐藏响应header和错误(404等)通知中的版本号
sendfile       on;            #开启高效传输模式
tcp_nopush     on;            #减少网络报文段的数量
tcp_nodelay    on;            #内核会等待将更多的字节组成一个数据包,从而提高I/O性能

gzip  on;                        #开启压缩功能
gzip_min_length  1k;            #允许压缩的页面最小字节,建议大于1k,如果小于1k可能会越压越大
gzip_types  text/plain application/x-javascript text/css application/xml;        #指定“text/html”类型总是会被压缩

#以下参数是为了改善网站性能,减少资源占用,提高访问速度
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;

listen       192.168.1.146:80;      #修改监听本机内网地址(此处举例)
server_name  host12.i.super.org;        #修改name,如果有域名解析可以设置主机名(此处举例)

location / {
    root   html;
    index  index.php index.html index.htm;      #新增index.php
}


location ~ \.php$ {
root           html;                    #网站的根目录
fastcgi_pass   127.0.0.1:9000;            #通过fastcgi接口将请求发送给这个IP
fastcgi_index  index.php;                #设置首页
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        #在浏览器中访问.php的文件时,实际读取的是$document_root(网站根目录)下的.php文件
include        fastcgi_params;
}

#以下参数为设定查看nginx状态的地址
location /nginx_status {
    stub_status on;
    access_log off;
}

11、检测nginx配置文件是否有错误

nginx -t    #执行结果如下
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

12、启动/关闭/重载nginx服务

/apps/nginx/sbin/nginx
/apps/nginx/sbin/nginx -s stop      #关闭
/apps/nginx/sbin/nginx -s reload    #重新加载
/apps/nginx/sbin/nginx -h           #查看帮助

13、编写nginx服务脚本加入systemd服务 /etc/systemd/system/nginx.service

[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/apps/nginx/run/nginx.pid
ExecStart=/apps/nginx/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/apps/nginx/sbin/nginx -s reload
ExecStop=/apps/nginx/sbin/nginx -s stop
TimeoutStopSec=5

[Install]
WantedBy=multi-user.target

#按 Esc 键退出编辑模式,输入 :wq 保存并关闭nginx.service文件。

chmod 754 /etc/systemd/system/nginx.service
systemctl start nginx && systemctl enable nginx

你可能感兴趣的:(debian,nginx,运维)