Nginx

Nginx-安装

1.RPM包安装

#前置条件
sudo yum install yum-utils
#添加yum源或者下载rpm包
#yum方式,在/etc/yum.repo.d/目录下添加nginx.repo,内容如下
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

#重载yum
sudo yum-config-manager --enable nginx-mainline
#安装
sudo yum install nginx

2.源码安装

http://nginx.org/en/docs/configure.html这是官网的module介绍,用的时候翻翻,或者百度,我这里介绍的几个是针对php环境的

1.1 显示系统版本

[root@centos ~]# cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core)

[root@centos ~]# uname -a
Linux tCentos7 3.10.0-123.13.1.el7.x86_64 #1 SMP Tue Dec 9 23:06:09 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

1.2 安装基本软件包

[root@centos ~]# yum install vim wget lsof gcc gcc-c++ bzip2 -y

[root@centos ~]# yum install net-tools bind-utils -y

2.编译安装nginx

2.1 下载包

[root@centos ~]# cd /usr/local/src/

[root@centos ~]# wget http://nginx.org/download/nginx-1.6.2.tar.gz

[root@centos ~]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz

[root@centos ~]# wget http://zlib.net/zlib-1.2.8.tar.gz

[root@centos ~]# wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz

[root@centos ~]# wget http://www.canonware.com/download/jemalloc/jemalloc-3.6.0.tar.bz2

2.2 安装依赖

[root@centos ~]# yum install zlib-devel openssl-devel -y

2.3 关闭selinux (不关有时会添加不了用户,或者重启后没法开机)

[root@centos ~]# vim /etc/selinux/config

#屏蔽以下两行

#SELINUX=enforcing

#SELINUXTYPE=targeted

#添加以下一行

SELINUXTYPE=disabled

#保存,退出



#重启后,查询是否关闭(显示Disabled则表示关闭)

[root@centos ~]#  shutdown -r now

[root@centos ~]#  getenforce

Disabled

2.4 安装Pcre

[root@centos ~]# cd /usr/local/src/

[root@centos ~]# tar zvxf pcre-8.36.tar.gz

[root@centos ~]# cd pcre-8.36

[root@centos ~]# ./configure

[root@centos ~]# make && make install

2.5 安装openssl

[root@centos ~]# cd /usr/local/src/

[root@centos ~]# tar zvxf openssl-1.0.1j.tar.gz

[root@centos ~]# cd openssl-1.0.1j

[root@centos ~]# ./config

[root@centos ~]# make && make install

2.6 安装zlib

[root@centos ~]# cd /usr/local/src/

[root@centos ~]# tar zvxf zlib-1.2.8.tar.gz

[root@centos ~]# cd zlib-1.2.8

[root@centos ~]# ./configure

[root@centos ~]# make && make install

2.7 安装jemalloc

[root@centos ~]# cd /usr/local/src/

[root@centos ~]# tar xjf jemalloc-3.6.0.tar.bz2

[root@centos ~]# cd jemalloc-3.6.0

[root@centos ~]# ./configure

[root@centos ~]# make && make install

[root@centos ~]# echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf

[root@centos ~]# ldconfig

2.3 创建www用户和组,创建www虚拟主机使用的目录,以及Nginx使用的日志目录,并且赋予他们适当的权限

[root@centos ~]# groupadd www

[root@centos ~]# useradd -g www www -s /sbin/nologin

[root@centos ~]# mkdir -p /data/www

[root@centos ~]# chmod +w /data/www

[root@centos ~]# chown -R www:www /data/www

***如果没法创建用户,需要检查SELinux状态是否关闭

2.8 安装nginx

[root@centos ~]# cd /usr/local/src/

[root@centos ~]# tar zvxf nginx-1.6.2.tar.gz

[root@centos ~]# cd nginx-1.6.2

[root@centos ~]# ./configure --prefix=/opt/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1j --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.36 --with-ld-opt="-ljemalloc"

[root@centos ~]# make && make install

2.9 修改 nginx.conf

[root@centos ~]# vim /opt/nginx/conf/nginx.conf

#修改前面几行为:

user www www;

worker_processes auto;

error_log logs/error.log crit;

pid logs/nginx.pid;

events{
  use epoll;
  worker_connections 65535;
}


找到,并修改 root 行的内容
location / {
root /data/www;
index index.html index.htm;
}


保存,退出

2.10 建立测试首页

[root@centos ~]# vim /data/www/index.html

<html>
head><title>nginx index.html</title></head>
<body>
<h1>index.html</h1>
</body>
</html>

#保存,退出

2.11 测试和运行

[root@centos ~]# cd /opt/nginx

[root@centos ~]# ldconfig

[root@centos ~]# ./sbin/nginx -c /opt/nginx/conf/nginx.conf -t

#如果显示下面信息,即表示配置没问题

nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful

常用的配置选项

--prefix=path	 #安装目录指定,不指定默认为/usr/local/

--sbin-path 指向(执行)程序文件(nginx),不指定默认为prefix/sbin/nginx

--conf-path= 指向配置文件(nginx.conf)不指定默认为prefix/conf/nginx.conf(ps:yum安装在/etc/nginx/nginx.conf)

--error-log-path= 指向错误日志目录,不指定默认为prefix/logs/error.log

--pid-path= 指向pid文件(nginx.pid)这里pid文件是什么,你可以去了解下,常用

--lock-path= 指向lock文件(nginx.lock)(安装文件锁定,防止安装文件被别人利用,或自己误操作。)

--user= 指定程序运行时的非特权用户,正常来说,生产环境,最好不要使用root来运行服务

--group= 指定程序运行时的非特权用户组,同上

--builddir= 指向编译目录

几个常用配置

1,反向代理,通过域名解析到特定服务器

以这个例子,假设我这条服务器为10.10.1.1(公网),192.168.12.123(私网),作为代理转发服务器,我所有的域名解析指向我,通过我再来转发到我的内部服务器内,就可以实现,内外网通信,懂吧,服务对外不透明,我添加修改只需要修改nginx就可以实现。这里的配置文件sever啊,events啊有些我就不详细给你写了,你自己看看相关介绍,有些写的仔细的

server {
listen       80;					#nginx监听的端口
server_name  www.laowanglaji.com;	#nginx监听的域名,我现在的dns服务这个域名指向的是10.10.1.1,服务放在192.168.12.21内网的服务器上面,现在的意思是,nginx监听本服务器上面80端口,一旦有访问域名为www.laowanglaji.com的请求,我就转发,转发规则在下面
charset utf-8; 						#设置字符集utf-8
location / {						#对以/后面的地址进行负载
        root   html;
        index  index.jsp index.html index.htm;
        proxy_pass                    http://192.168.12.21:8001;  #这个就是转发的真是服务器所在的,192.168.12.21上面8001上面监听的服务
        proxy_set_header              Host           $host:80;
        proxy_set_header              X-Real-IP       $remote_addr;
        proxy_set_header              X-Forwarded-For  $proxy_add_x_forwarded_for;
		#上面的这几行,将访问者的真实信息转发给目标服务器
        client_max_body_size 500m;
        client_body_buffer_size 512k;
        proxy_connect_timeout 90;
        proxy_send_timeout 90;
        proxy_read_timeout 90;
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
        proxy_busy_buffers_size 256k;

    }
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   html;
}

}

负载均衡

https://www.cnblogs.com/qlqwjy/p/8536779.html这个讲的挺不错

剩下就是多配置试试和涉及到一些具体的业务需求就来配置,调优什么的,默认的,一般来说,是最不容易出错的,上线的时候在根据业务量和机器配置来进行调优,调优的方向,一般就是进程,缓存池,文件数,i/o,安全性,认证等。

posted @ 2019-07-26 21:43  一个种树的人  阅读(190)  评论(0编辑  收藏  举报