Nginx安装部署!

摘要:
main块设置的指令将影响其他所有设置;server块的指令主要用于指定主机和端口;upstream指令主要用于负载均衡,设置一系列的后端服务器;location块用于匹配网页位置。=============================================================创建单个基于域名的虚拟主机并测试[root@localhost~]#rpm-qhttpd//有httpd软件必须删除未安装软件包httpd安装支持软件[root@localhost~]#rpm-qgccgcc-c++zlib-develpcre-develmake[root@localhost~]#yum-yinstallgccgcc-c++zlib-develpcre-develmake完毕!

安装Nginx方法一:利用u盘导入Nginx软件包

二nginx -t 用于检测配置文件语法

如下报错1:配置文件43行出现错误

[root@www ~]# nginx -t
nginx: [emerg] "location" directive is not allowed here in /usr/local/nginx/conf/nginx.conf:43
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

如下错误2:worker里面工作区出现问题

[root@www ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

解决办法:

[root@www ~]# ulimit -n 10240

Nginx安装部署!第1张

main(全局设置)、server(主机设置)、upstream(负载均衡服务器设置)和 location(URL匹配特定位置的设置)。

    main块设置的指令将影响其他所有设置;
    server块的指令主要用于指定主机和端口;
    upstream指令主要用于负载均衡,设置一系列的后端服务器;
    location块用于匹配网页位置。

=============================================================

创建单个基于域名的虚拟主机并测试

[root@localhost ~]# rpm -q httpd //有httpd软件必须删除

未安装软件包 httpd

安装支持软件
[root@localhost ~]# rpm -q gcc gcc-c++ zlib-devel pcre-devel make
[root@localhost ~]# yum -y install gcc gcc-c++ zlib-devel pcre-devel make

完毕!

创建运行用户、组
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tail -l /etc/passwd ;tail -l /etc/group

导入nginx软件包
[root@localhost ~]# rz -E
rz waiting to receive.
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg nginx-1.16.0.tar.gz original-ks.cfg
[root@localhost ~]# tar xf nginx-1.16.0.tar.gz -C /usr/src
[root@localhost ~]# cd /usr/src/nginx-1.16.0/

编译安装 Nginx
[root@localhost nginx-1.16.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-stream --with-http_gzip_static_module && make && make install

--prefix 设定Nginx的安装目录

--user--group指定Nginx运行用户和组

--with-http_stub_status_module 启用http_stub_status_module模块以支持状态统计

--with-http_ssl_module 启用SSL模块

--with-http_flv_module 启用FLV模块,提供寻求内存使用基于时间的偏移量文件

--with-stream 用于做七层,四层负载的模块

[root@localhost nginx-1.16.0]# cd

为主程序 nginx 创建链接文件
[root@localhost ~]# ls /usr/local/nginx/
confhtmllogs sbin

[root@localhost ~]#ln -sf /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost ~]# ll /usr/local/sbin/nginx
lrwxrwxrwx. 1 root root 27 9月 10 17:12 /usr/local/sbin/nginx ->/usr/local/nginx/sbin/nginx

Nginx 的运行控制方法
手动方法控制 Nginx:
nginx -t 检测配置文件语法
执行 nginx 主程序启动 Nginx

编写 nginx 服务脚本

[root@localhost ~]# vim /etc/init.d/nginx

#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Server Control Scripts shell
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"

case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0

[root@localhost ~]# chmod +x /etc/init.d/nginx

Nginx 修改主配置文件

[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# cp nginx.conf nginx.conf.origin
[root@localhost conf]# vim nginx.conf

user  nginx nginx;                           //nginx的程序账户及程序组
worker_processes  2;                        //指定进程数一般与cpu数量一致
worker_cpu_affinity 00000001 00000010;      //为每个进程分配核心数
error_log  logs/error.log  info;            //全局错误日志文件位置
pid        logs/nginx.pid;                  //PID文件的位置
events {
   use epoll;                              //使用epoll模型
    worker_connections  10240;              //每个进程允许的最多的连接数默认为1024一般10000以下
}
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"';

    access_log  logs/access.log  main;       //访问日志位
    sendfile        on;                      //支持文件发送超时
    keepalive_timeout  65;
server {                                     //web服务的监听配置
        listen       80;                      //监听地址及端口(IPPORT
server_name  localhost; //网站名称(FQDN)charset utf-8;//网页的默认字符集
access_log logs
/localhost.access.log main; location /{ //根目录配置
root
html; //网站根目录的位置安装位置的html中
index index.html index.htm;
//默认首页
}

error_page 500 502 503 504 /50x.html; //内部错误的反馈页面
location =/50x.html { //错误页面配置
root html;
}
}
}

[root@localhost ~]# nginx -t //检测语法错误
nginx: the configuration file /usr/local/nginx/conf/nginx.confsyntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conftest is successful
[root@localhost ~]# nginx //启动nginx服务

[root@localhost ~]# killall -HUP nginx

[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 64726/nginx: master

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0

Nginx安装部署!第2张

nginx安装完成

安装Nginx方法二:

从官网下载相应的源码包:

Nginx下载地址:http://nginx.org/en/download.html

[root@localhost ~]#wget http:;//nginx.org/download/nginx-1.14.2.tar.gz
[root@localhost ~]#tar xf nginx-1.14.2.tar.gz -C /usr/src
[root@localhost ~]#cd /usr/src/nginx-1.14.2
[root@localhost nginx-1.14.2]#yum install gcc gcc-c++ make zlib-devel pcre-devel -y
[root@localhost nginx-1.16.0]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
&& make && make install

[root@localhost nginx-1.17.4]# cd /usr/share/vim/vimfiles/ //拷入相应的语法

[root@localhost ~]# cd /usr/local/nginx/conf/

[root@localhost conf]# vim nginx.conf

user  nginx nginx;                           //nginx的程序账户及程序组
worker_processes  2;                         //指定进程数一般与cpu数量一致
worker_cpu_affinity 00000001 00000010;       //为每个进程分配核心数
error_log  logs/error.log  info;             //全局错误日志文件位置
pid        logs/nginx.pid;                   //PID文件的位置
events {
   use epoll;                                //使用epoll模型
    worker_connections  10240;               //每个进程允许的最多的连接数默认为1024一般10000以下
}
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"';

    access_log  logs/access.log  main;        //访问日志位
    sendfile        on;                       //支持文件发送超时
    keepalive_timeout  65;
server {                                      //web服务的监听配置
        listen       80;                      //监听地址及端口(IP:PORT)
        server_name  localhost;     //网站名称(FQDN)        
       charset utf-8; //网页的默认字符集        
       access_log  logs/localhost.access.log  main; 
location / {                                //根目录配置            
         root   html;             //网站根目录的位置安装位置的html中            
         index  index.html index.htm;       //默认首页       
}          

     error_page 500 502 503 504 /50x.html;       //内部错误的反馈页面
       location =/50x.html {                     //错误页面配置
root html;
        }
    }
}

Nginx安装部署!第3张

免责声明:文章转载自《Nginx安装部署!》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[linux常用命令]findvi/vim 跳转到指定行下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Nginx使用教程(四):提高Nginx网络吞吐量之buffers优化

请求缓冲区在NGINX请求处理中起着重要作用。 在接收到请求时,NGINX将其写入这些缓冲区。 这些缓冲区中的数据可作为NGINX变量使用,例如$request_body。 如果缓冲区与请求大小相比较小,则数据将写入磁盘上的文件,因此将涉及I/O操作。 NGINX提供了可以改变请求缓冲区的各种指令。 client_body_buffer_size <...

使用nginx-prometheus-exporter 监控nginx

因为nginx 已经提供了stub_status 模块,一般我们可以基于此进行监控,目前官方提供了一个exporter 尽管有一些限制(web必须使用8080)。以下是一个简单的学习使用 环境准备 docker-compose 文件 注意使用了ranadeeppolavarapu 提供的nginx 镜像(很方便,可以学习各种nginx 插件的使用)...

[tls][https][nginx] https的client session cache与session ticket机制分析

more title tls的客户端会话恢复与会话票证机制分析 golang fasthttp库关于会话恢复与会话票证的源码分析 前言 https握一次手是很艰辛的,计算量很大。所以如果连续两次短连接通信的话,完全可以 复用上一次的会话。这样可以压缩通信,节省计算。 TLS提供了两个机制来做这个事。分别是 session cache(会话缓存,会话恢复)...

Jenkins安装

 一, Jenkins简介  1.开源自动化持续集成与部署平台 CI, 持续集成 CD, 持续部署 2.Jenkins支持的任务类型 自由风格任务 流水线(Pipeline) Maven 项目 多配置项目 多分支流水线任务 3.Jenkins常见的任务功能 定时任务 Pull SCM -- 定时检查代码库中的代码有没有更新, 指定的分支或者ta...

nginx常用命令及nginx.conf基本配置

进入nginx安装目录后执行命令! 1、启动:start nginx 默认是根据nginx.conf来启动的。如果要指定配置文件来启动则使用以下命令即可:nginx -c ./conf/jason.conf2、关闭:nginx -s stop 快速关闭nginx服务。nginx -s quit 优雅的关闭,优雅是指当一个请求被处理完成之后才被关闭。 在li...

nginx无法启动: libpcre.so.1/libpcre.so.0: cannot open shared object file解决办法

NGINX启动时提示错误: /usr/local/nginx/sbin/nginx -t/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or direct...