nginx安装及配置
一、安装
nginx-1.12.0.tar.gz 源码安装包
1、解压安装包
tar zxf nginx-1.12.0.tar.gz
2、编译
nginx 编译参数
--prefix= nginx安装根路径
--sbin-path= 制定nginx二进制文件的路径,若未指定,依赖prefix选项
--user= worker进程运行的用户
--with-http_ssl_module
编译片段
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
中间附带报错:
(1)报错:
checking for OS
+ Linux 2.6.32-431.el6.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
解决:yum install gcc-c++ -y
(2)报错:
./configure: error: the HTTP rewrite module requires the PCRE library.You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option.
解决:yum install pcre-devel -y
(3)报错:
./configure: error: SSL modules require the OpenSSL library.You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using –with-openssl= option.
解决:yum install openssl-devel -y
3、生成脚本及配置文件:make
4、安装:make install —>安装完毕,在编译时设置的prefix的目录中
二、配置
安装完毕后,进入,生成四个目录,作用分别为
conf:保存nginx所有的配置文件,nginx.conf为主配置文件
html:保存nginx服务器中的web文件
logs:保存nginx服务器的访问日志、错误日志等,也可放在其他路径中
sbin:保存nginx二进制启动脚本
1、做nginx软链接,以方便启动
ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin
2、测试nginx是否安装成功 打开nginx和浏览器
[root@server1 nginx]# nginx
在浏览器输入IP地址,可得到如下图所示
3、nginx.conf中的参数以及其他配置
<1> work_process #可以指定启动的固定nginx的进程数,改变后可用ps aux来查看不同
规则为:
eg1:
worker_process 2;
worker_cpu_affinity 01 10;
eg2:
worker_process 4;
worker_cpu_affinity 0001 0010 0100 1000;
nginx -t 命令查看配置是否错误
nginx -s reload 命令重新加载配置
<2> user #指定运行的用户,若不配置,默认为nobody,该用户需已经存在
<3> 改变非超级用户打开文件的默认目录个数
改变/etc/security/limits.conf文件 该文件格式如下:
<domain> <type> <item> <value>或者username|@groupname type resource limit
type 分为soft,hard,
soft:当前系统生效的设置值
hard:系统中所能设定的最大值,其中soft的限制不能比hard高
-:表明同时设置了soft和hard的值
resource 分为:
core:限制内核文件的大小
date:最大数据大小
fsize:最大文件大小
memlock:最大内存地址空间
nofile:打开文件的最大数目
stack:最大栈大小
cpu:以分钟为单位的最多的cpu时间
as:地址空间限制
maxlogins:此用户允许登陆的最大数目
[nginx@server1 ~]$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7820
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 1024
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
[root@server1 conf]# vim /etc/security/limits.conf
-->nginx - nproc 2048
nginx - nofile 2048
[root@server1 conf]# nginx -s reload
[root@server1 conf]# su - nginx
[nginx@server1 ~]$ ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 7820
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 2048
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 2048
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
<4> 设置http页面
[root@server1 conf]# vim nginx.conf
-->118 server {
119 listen 80;
120 server_name www.westos.org;
121
122 location / {
123 root /web1;
124 index index.html;
125 }
126 }
127
128 server {
129 listen 80;
130 server_name www.linux.org;
131
132 location / {
133 root /web2;
134 index index.html;
135 }
然后建立/web1、/web2目录,并在其中写入文件index.html,用浏览器测试是显示出index.html中的内容,还要进行地址解析,否则浏览器无法找到
<5> cd /etc/pki/tls/certs –>make cert.pem –>CN Shaanxi xi’an …
mv cert.pem /usr/local/lnmp/nginx/conf/
vim nginx.conf
-->118 server {
119 listen 80;
120 server_name www.westos.org;
121 rewrite ^(.*) http://$server_name$1 permanent;
刷新nginx服务 测试:curl -I www.westos.org
<6> 用qemu-img再做出两个快照,hostname分别为server2和server3
设置server2和server3(两个设置相同),server2如下:
yum install httpd -y
vim /etc/httpd/conf/httpd.conf --> ServerName 172.25.50.2(ip) vim /etc/www/html/index.html --> <h1>server2<h1>
/etc/init.d/httpd start
测试截图:
<7>
vim nginx.conf
-->18 http {
19
20 upstream westos{
21 server 172.25.50.2:80;
22 server 172.25.50.3:80;
nginx -s reload
测试:
[root@localhost kiosk]# for i in {1..10};do curl www.linux.org;done
<h1>server2<h1>
<h1>server2<h1>
<h1>server3<h1>
<h1>server3<h1>
<h1>server2<h1>
<h1>server2<h1>
<h1>server3<h1>
<h1>server3<h1>
<h1>server2<h1>
<h1>server2<h1>
测试如图: