安装nginx和 nginx负载均衡
初学nginx总结,仅供参考;
centos7系统库中默认是没有nginx的rpm包的,所以我们自己需要先更新下rpm依赖库
1.使用yum安装nginx需要包括Nginx的库,安装Nginx的库
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2.使用下面命令安装nginx
yum install nginx
注意:
如果80被占用的话建议进入vi /etc/nginx/conf.d/default.conf修改端口成为listen 81;
之后开放81端口 设置防火墙
firewall-cmd --zone=public--add-port=81/tcp --permanent
重新启动防火墙
systemctl restart firewalld.service
3.启动
systemctl start nginx.service
systemctl restart nginx.service
4、解决nginx读取php的问题
1)安装php-fpm
yum install php70w-fpm
2)添加vi /etc/nginx/conf.d/default.conf(配置文件地址也许会不同,可以使用find查找路径,如:find / -namedefault.conf 回车)配置文件的内容为以下:
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
3)启动php-fpm
systemctl start php-fpm.service
systemctl restartphp-fpm.service
5、nginx文件页面存放位置 /usr/share/nginx/html
6、查看nginx错误所在:
检测nginx是否已使用了你配置的文件(可以故意出错测试)
确保目录/tmp/html; 有执行权限
二、nginx负载均衡作用:Nginx能够配置代理多台服务器。当一台服务器宕机之后。仍能保持系统可用。详细配置步骤例如以下:
如果192.168.1.224为主机,192.168.1.223为负载中的同一个静态文件可以在92.168.1.224:81/a.php同weight权重进入;
1、vi /etc/nginx/conf.d/default.conf
upstream bakend {
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器
down
掉,
能自动剔除。
server 192.168.1.223 weight=1; #weight权重server 192.168.1.224 weight=1;
}
5 server {
6 listen 81;
7 server_name localhost;
8
9 #charset koi8-r;
10 #access_log /var/log/nginx/host.access.log main;
11
12 location / {
13 root /usr/share/nginx/html;
14 index index.html index.htm;
15 proxy_pass http://bakend;
16 proxy_set_header Host $host;
17 proxy_set_header X-Real-IP $remote_addr;
18 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
19 }
20 # location ~ \.php$ {
21 # root /usr/share/nginx/html;
22 # fastcgi_pass 127.0.0.1:9000;
23 # fastcgi_index index.php;
24 # fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
25 # include fastcgi_params;
26 # }
27
28
29 #error_page 404 /404.html;
30
31 # redirect server error pages to the static page /50x.html
32 #
33 error_page 500 502 503 504 /50x.html;
34 location = /50x.html {
35 root /usr/share/nginx/html;
36 }
37
38 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
39 #
40 #location ~ \.php$ {
41 # proxy_pass http://127.0.0.1;
42 #}
43
44 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
45 #
46 #location ~ \.php$ {
47 # root html;
48 # fastcgi_pass 127.0.0.1:9000;
49 # fastcgi_index index.php;
50 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
51 # include fastcgi_params;
52 #}
53
54 # deny access to .htaccess files, if Apache's document root
55 # concurs with nginx's one
56 #
57 #location ~ /\.ht {
58 # deny all;
59 #}
60 }
61
"/etc/nginx/conf.d/default.conf" 61L, 1674C written
[root@bogon html]# systemctl restart nginx.service #重启nginx
[root@bogon html]# service php-fpm restart #重启php-fpm
注意:要见解析php的注释掉