Ubuntu下怎么用最新源码编译Nginx并打入插件,以及相关nginx.conf配置说明。
安装依赖库
apt update #安装依赖:gcc、g++依赖库 apt install build-essential libtool #安装 pcre依赖库(http://www.pcre.org/) apt install libpcre3 libpcre3-dev #安装 zlib依赖库(http://www.zlib.net) apt install zlib1g-dev #安装ssl依赖库 apt install openssl libssl-dev
上面是一些必要的依赖,忽略上面,一把梭命令如下:
apt install build-essential libtool libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev
下载Nginx源码编译
#Nginx官方下载并解压(https://nginx.org/en/download.html) wget https://nginx.org/download/nginx-1.19.2.tar.gz -O - | tar -xz cd nginx-1.19.2 #--prefix是各个文件路径,--with是打入的插件 ./configure --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --modules-path=/usr/lib/nginx/modules \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --user=nginx \ --group=nginx \ --with-compat \ --with-file-aio \ --with-threads \ --with-http_addition_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_realip_module \ --with-http_secure_link_module \ --with-http_slice_module \ --with-http_sub_module \ --with-mail --with-mail_ssl_module \ --with-stream_realip_module \ --with-stream_ssl_module \ --with-stream_ssl_preread_module \ --with-pcre \ --with-stream \ --with-http_mp4_module \ --with-http_auth_request_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_v2_module make make install
编译完成后就可以用nginx -t
检查一下nginx有没有什么错误提示,如下显示即为ok
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
如果出现报错的话可以按下面方法修复。
常见报错
当然启动Nginx前先用下面命令检查一下80/443端口有没有被占用:
netstat -lntp
nginx -t
常见报错:
#如下错误说明nginx用户不存在,创建nginx用户即可 nginx: [emerg] getpwnam("nginx") failed in /etc/nginx/nginx.conf:2 nginx: configuration file /etc/nginx/nginx.conf test failed useradd nginx
#如果出现下错误,说明client_temp文件夹不存在,创建一个即可 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory) nginx: configuration file /etc/nginx/nginx.conf test failed mkdir -p /var/cache/nginx/client_temp
创建systemd服务
vi /lib/systemd/system/nginx.service
添加内容如下:
[Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPre=/usr/sbin/nginx -t ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
重载服务:systemctl daemon-reload
然后就可以启动Nginx了:
systemctl start nginx
Nginx的常用命令
systemctl enable nginx
#设置开机自动启动
systemctl status nginx
#查看运行状态,显示running表示成功运行
systemctl reload nginx
#重新载入
systemctl restart nginx
#重新启动
systemctl stop nginx
#立刻停止运行nginx
nginx -t
#测试配置文件
配置nginx.conf
Nginx 配置文件主要分成四部分:main(全局设置)、server(主机设置)、upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)和 location(URL匹配特定位置后的设置)。main 部分设置的指令影响其它所有部分的设置;server 部分的指令主要用于制定虚拟主机域名、IP 和端口号;upstream 的指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡;location 部分用于匹配网页位置(比如,根目录“/”,“/images”,等等)。他们之间的关系:server 继承 main,location 继承 server;upstream 既不会继承指令也不会被继承。
文件大体结构如下,最外层的main在nginx.conf中是默认实现的,并不会实际写出来。
main { user nginx; events { worker_connections 1024; } http { server_tokens off; gzip on; server { listen 80; location / { return 301 https://$server_name$request_uri; } } } }
nginx的默认配置项:
#nginx的子进程运行的账户名 #user nobody; #工作进程数,可以设置为auto,nginx会探测cpu核心数,启动相同数量的work process worker_processes 1; #主进程pid文件存放的地点 #pid /var/run/nginx.pid; #events模块只能在main上下文中,并且只能配置一个,其中可以包含7种简单指令 events { worker_connections 1024; #每个worker process可以支持的最大连接数,生成环境根据需要可以设置更大一些(9000) #值得注意的是,此数字是包括了反向代理等等所需要的连接数在内,并不仅仅是指web端发起的连接数 #multi_accept on #是否work进程一次只接收一个连接。并发较大时应打开 #use epoll #选择底层处理连接的模型,Linux下默认epoll,无需自己设置 #accept_mutex #accept_mutex_delay #上面两参数是表示是否让worker进程使用one by one的工作模式,默认是关闭的。 #开启的时候,在低并发的时候非工作状态下的worker process将休眠,避免浪费资源。 } #nginx配置的“核心” http { #include指令用于加载单独的配置文件模块,避免过于臃肿 #而这里的mime.types表示此文件应该在nginx.conf的同级目录下。 #其中的types模块用于表示nginx响应的文件后缀名和content-type直接的映射 #比如你想要让浏览器识别你返回的mytxt.data文件为文本内容在浏览器中显示为文本,添加 text/plain data; 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"'; #access_log /var/log/nginx/access.log main; #错误日志和日志级别,debug | info | notice | warn | error | crit | alert | emerg级别从低到高,默认是error error_log /var/log/nginx/error.log crit; #io时不阻塞处理connection,在传输大文件时使用 sendfile on; #tcp_nopush on; #貌似意思是只有在sendfile启用时使用,让nginx发包的时候满包发送(不明白)通常不使用 #连接idle的最大时间,现代浏览器往往在一个页面同时打开多个connection传输js,css,html等。 #设置最大idle时间避免过长等待浪费connection #keepalive_timeout 0; keepalive_timeout 65; #开启response响应压缩,可以节省带宽,默认关闭。 #gzip on; #server 上下文代表一个虚拟主机,可以有多个,内嵌在比如http,mail模块中 server { listen 80; server_name myapp.info; #域名,可多个域名空格隔开 location / { #location的匹配规则too tricky,单开一篇blog 注意匹配的文件夹的访问权限 root /usr/local/nginx; } } }
文章评论