阿里云服务器ECS centos7.2搭建nginx环境以及负载均衡
来源:互联网 发布:telnet本机ip端口不通 编辑:程序博客网 时间:2024/06/05 00:21
之所以要整理出这篇文章,是因为1是搭建环境的过程中会遇到大大小小各种问题,2是网上目前也没有关于centos7.2搭建nginx环境的问题整理,因此在这里记录。
前置工作就不赘述了,首先ssh root@115.29.102.81
(换成你们自己的公网IP)登陆进入到自己的服务器命令行,之后开始基本的安装:
1.添加资源
添加CentOS 7 Nginx yum资源库,打开终端,使用以下命令(没有换行):
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2.安装Nginx
在你的CentOS 7 服务器中使用yum命令从Nginx源服务器中获取来安装Nginx:
这里有一个需要注意的地方,尽量不要用网上的下载源码包然后再传到服务器上的方式进行安装,因为nginx已经不算是简单的Linux了,做了很多扩展,这个时候如果你用源码包安装会出现各种各样的问题,尽量用已经封装好的rpm\yum进行安装
<code class="hljs bash has-numbering"><span class="hljs-built_in">sudo</span> yum install -y nginx</code>
Nginx将完成安装在你的CentOS 7 服务器中。
3.启动Nginx
刚安装的Nginx不会自行启动。运行Nginx:
<code class="hljs bash has-numbering"><span class="hljs-built_in">sudo</span> systemctl start nginx.service</code>
如果一切进展顺利的话,现在你可以通过你的域名或IP来访问你的Web页面来预览一下Nginx的默认页面
当然,这里一般很可能会无法访问的。
我们先不急于解决我们的问题,先看看nginx的基本配置:
Nginx配置信息
网站文件存放默认目录/usr/share/nginx/html网站默认站点配置/etc/nginx/conf.d/default.conf自定义Nginx站点配置文件存放目录,自己在这里也可以定义别的名字的.conf,这个的作用以后再说。/etc/nginx/conf.d/Nginx全局配置/etc/nginx/nginx.conf在这里你可以改变设置用户运行Nginx守护程序进程一样,和工作进程的数量得到了Nginx正在运行,等等。
Linux查看公网IP
您可以运行以下命令来显示你的服务器的公共IP地址:(这个其实没用,不是公网IP)
<code class="hljs sql has-numbering">ip addr <span class="hljs-operator"><span class="hljs-keyword">show</span> eth0 | grep inet | awk <span class="hljs-string">'{ print $2; }'</span> | sed <span class="hljs-string">'s/\/.*$//'</span></span></code>
好了,这个时候我们再来看看可能遇到的问题:无法在公网访问。
这个时候首先看看配置文件default.conf对不对,一个正确的例子:
(域名要先进行解析到响应的IP)
server { listen 80; server_name nginx.310058.cn; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #}}
确定文件没问题了,看看这个时候是不是开启了nginx进程:
<code class="hljs 1c has-numbering">ps -ef <span class="hljs-string">| grep nginx</span></code>
应该会输出一个或者多个进程,如果没有的话就开启或者重启试试看。
这个时候接下来再试试在服务器上:
<code class="hljs avrasm has-numbering"><span class="hljs-built_in">ping</span> <span class="hljs-number">115.29</span><span class="hljs-number">.102</span><span class="hljs-number">.81</span>telnet <span class="hljs-number">115.29</span><span class="hljs-number">.102</span><span class="hljs-number">.81</span> <span class="hljs-number">80</span>wget nginx<span class="hljs-number">.310058</span><span class="hljs-preprocessor">.cn</span></code>如果有的命令没有就直接yum安装下:
<code class="hljs cmake has-numbering">yum -y <span class="hljs-keyword">install</span> telnet</code>如果都可以的话,之后在本机尝试以上三行。如果没有命令也要安装下:
<code class="hljs cmake has-numbering">brew <span class="hljs-keyword">install</span> wget</code>
发现很可能本机telnet不通,而服务器telnet通。
这个时候就是防火墙的问题。
centos7.2防火墙
由于centos 7版本以后默认使用firewalld后,网上关于iptables的设置方法已经不管用了,所以根本就别想用配置iptables做啥,根本没用。
查看下防火墙状态:<code class="hljs avrasm has-numbering">[root@iZ28dcsp7egZ conf<span class="hljs-preprocessor">.d</span>]<span class="hljs-preprocessor"># systemctl status firewalld </span>● firewalld<span class="hljs-preprocessor">.service</span> - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld<span class="hljs-preprocessor">.service</span><span class="hljs-comment">; enabled; vendor preset: enabled)</span> Active: active (running) since Wed <span class="hljs-number">2016</span>-<span class="hljs-number">08</span>-<span class="hljs-number">03</span> <span class="hljs-number">12</span>:<span class="hljs-number">06</span>:<span class="hljs-number">44</span> CST<span class="hljs-comment">; 2h 49min ago</span> Main PID: <span class="hljs-number">424</span> (firewalld) CGroup: /system<span class="hljs-preprocessor">.slice</span>/firewalld<span class="hljs-preprocessor">.service</span> └─<span class="hljs-number">424</span> /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopidAug <span class="hljs-number">03</span> <span class="hljs-number">12</span>:<span class="hljs-number">06</span>:<span class="hljs-number">41</span> iZ28dcsp7egZ systemd[<span class="hljs-number">1</span>]: Starting firewalld - dynamic firewall daemon...Aug <span class="hljs-number">03</span> <span class="hljs-number">12</span>:<span class="hljs-number">06</span>:<span class="hljs-number">44</span> iZ28dcsp7egZ systemd[<span class="hljs-number">1</span>]: Started firewalld - dynamic firewall daemon.</code><p>增加80端口的权限:</p><pre class="prettyprint" name="code"><code class="hljs brainfuck has-numbering"><span class="hljs-comment">firewall</span><span class="hljs-literal">-</span><span class="hljs-comment">cmd</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">zone=public</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">add</span><span class="hljs-literal">-</span><span class="hljs-comment">port=80/tcp</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">permanent</span> </code>
别忘了更新防火墙的配置:
<code class="hljs brainfuck has-numbering"><span class="hljs-comment">firewall</span><span class="hljs-literal">-</span><span class="hljs-comment">cmd</span> <span class="hljs-literal">-</span><span class="hljs-literal">-</span><span class="hljs-comment">reload</span></code>
这个时候再restart nginx.service
一下就会发现应该好了。
nginx 停止:
<code class="hljs vala has-numbering">service nginx restart也可以重启nginxkill -QUIT 进程号 <span class="hljs-preprocessor">#从容停止</span>kill -TERM 进程号<span class="hljs-preprocessor">#或者</span>kill -INT 进程号<span class="hljs-preprocessor">#快速停止</span>p-kill -<span class="hljs-number">9</span> nginx强制停止nginx -t <span class="hljs-preprocessor">#验证配置文件 前提是进入相应的配置的目录(自己实际测试的时候发现没有进入相应的配置目录也是可以的)</span>nginx -s reload<span class="hljs-preprocessor">#重启</span>kill -HUP 进程号<span class="hljs-preprocessor">#重启的另外一种方式</span></code>
增加80端口的权限:
官方文档地址:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Security_Guide/sec-Using_Firewalls.html#sec-Introduction_to_firewalld
附1:一个简单的负载均衡的实现: weight默认是1,自己也可以更改。<pre class="prettyprint" name="code"><code class="hljs axapta has-numbering">upstream mypro { ip_hash; <span class="hljs-keyword">server</span> <span class="hljs-number">111.13</span><span class="hljs-number">.100</span><span class="hljs-number">.92</span> weight=<span class="hljs-number">2</span>; <span class="hljs-keyword">server</span> <span class="hljs-number">183.232</span><span class="hljs-number">.41</span><span class="hljs-number">.1</span>; <span class="hljs-keyword">server</span> <span class="hljs-number">42.156</span><span class="hljs-number">.140</span><span class="hljs-number">.7</span>; } <span class="hljs-keyword">server</span> { listen <span class="hljs-number">8090</span>; location / { proxy_pass http:<span class="hljs-comment">//mypro;</span> } }</code>附2:防火墙基本学习:
<code class="hljs sql has-numbering">1、firewalld简介firewalld是centos7的一大特性,最大的好处有两个:支持动态更新,不用重启服务;第二个就是加入了防火墙的“zone”概念firewalld有图形界面和工具界面,由于我在服务器上使用,图形界面请参照官方文档,本文以字符界面做介绍firewalld的字符界面管理工具是 firewall-cmd firewalld默认配置文件有两个:/usr/lib/firewalld/ (系统配置,尽量不要修改)和 /etc/firewalld/ (用户配置地址)zone概念:硬件防火墙默认一般有三个区,firewalld引入这一概念系统默认存在以下区域(根据文档自己理解,如果有误请指正):<span class="hljs-operator"><span class="hljs-keyword">drop</span>:默认丢弃所有包block:拒绝所有外部连接,允许内部发起的连接<span class="hljs-keyword">public</span>:指定外部连接可以进入<span class="hljs-keyword">external</span>:这个不太明白,功能上和上面相同,允许指定的外部连接dmz:和硬件防火墙一样,受限制的公共连接可以进入<span class="hljs-keyword">work</span>:工作区,概念和workgoup一样,也是指定的外部连接允许home:类似家庭组internal:信任所有连接对防火墙不算太熟悉,还没想明白<span class="hljs-keyword">public</span>、<span class="hljs-keyword">external</span>、dmz、<span class="hljs-keyword">work</span>、home从功能上都需要自定义允许连接,具体使用上的区别还需高人指点<span class="hljs-number">2</span>、安装firewalldroot执行 # yum install firewalld firewall-config<span class="hljs-number">3</span>、运行、停止、禁用firewalld启动:# systemctl <span class="hljs-keyword">start</span> firewalld查看状态:# systemctl status firewalld 或者 firewall-cmd --state停止:# systemctl disable firewalld禁用:# systemctl stop firewalld<span class="hljs-number">4</span>、配置firewalld查看版本:$ firewall-cmd --version查看帮助:$ firewall-cmd --help查看设置: 显示状态:$ firewall-cmd --state 查看区域信息: $ firewall-cmd --<span class="hljs-keyword">get</span>-active-zones 查看指定接口所属区域:$ firewall-cmd --<span class="hljs-keyword">get</span>-<span class="hljs-keyword">zone</span>-<span class="hljs-keyword">of</span>-interface=eth0拒绝所有包:# firewall-cmd --panic-<span class="hljs-keyword">on</span>取消拒绝状态:# firewall-cmd --panic-off查看是否拒绝:$ firewall-cmd --query-panic更新防火墙规则:# firewall-cmd --reload # firewall-cmd --complete-reload 两者的区别就是第一个无需断开连接,就是firewalld特性之一动态添加规则,第二个需要断开连接,类似重启服务将接口添加到区域,默认接口都在<span class="hljs-keyword">public</span># firewall-cmd --<span class="hljs-keyword">zone</span>=<span class="hljs-keyword">public</span> --<span class="hljs-keyword">add</span>-interface=eth0永久生效再加上 --permanent 然后reload防火墙设置默认接口区域# firewall-cmd --<span class="hljs-keyword">set</span>-<span class="hljs-keyword">default</span>-<span class="hljs-keyword">zone</span>=<span class="hljs-keyword">public</span>立即生效无需重启打开端口(貌似这个才最常用)查看所有打开的端口:# firewall-cmd --<span class="hljs-keyword">zone</span>=dmz --list-ports加入一个端口到区域:# firewall-cmd --<span class="hljs-keyword">zone</span>=dmz --<span class="hljs-keyword">add</span>-port=<span class="hljs-number">8080</span>/tcp若要永久生效方法同上打开一个服务,类似于将端口可视化,服务需要在配置文件中添加,/etc/firewalld 目录下有services文件夹,这个不详细说了,详情参考文档# firewall-cmd --<span class="hljs-keyword">zone</span>=<span class="hljs-keyword">work</span> --<span class="hljs-keyword">add</span>-service=smtp移除服务# firewall-cmd --<span class="hljs-keyword">zone</span>=<span class="hljs-keyword">work</span> --remove-service=smtp还有端口转发功能、自定义复杂规则功能、lockdown,由于还没用到,以后再学习</span></code>
- 阿里云服务器ECS centos7.2搭建nginx环境以及负载均衡
- 阿里云服务器ECS centos7.2搭建nginx环境以及负载均衡
- 阿里云服务器ECS centos7.2搭建nginx环境以及负载均衡
- 阿里云服务器ECS centos7.2搭建nginx环境以及负载均衡
- 阿里云ECS服务器搭建Nginx+PHP+MySql+Redis环境详细步骤(CentOS7环境)
- 阿里云ECS服务器搭建Nginx+PHP+MySql环境详细步骤(CentOS7环境)
- centos7.2 lamp环境安装搭建(基于阿里云ECS服务器)
- 阿里云ECS搭建nginx服务器
- 阿里云ECS CentOS7.2 搭建Shadowscoks
- 在阿里云 CentOS 服务器(ECS)上搭建 nginx + mysql + php-fpm 环境
- 在阿里云 CentOS 服务器(ECS)上搭建 nginx + mysql + php-fpm 环境
- 在阿里云 CentOS 服务器(ECS)上搭建 nginx + mysql + php-fpm 环境
- 在阿里云 CentOS 服务器(ECS)上搭建 nginx + mysql + php-fpm 环境
- 在阿里云ECS上从零开始搭建nginx服务器
- 阿里云ECS centos7.2下的nginx和nodejs
- nginx服务器搭建负载均衡
- 阿里云服务器ECS 第二篇:Java运行环境搭建
- 阿里云服务器ECS 第三篇:Oracle 数据库环境搭建
- to study app
- SQOOP从Oracle导入数据到Hive时hang up在MapReduce作业过程
- 初学linux命令-ls、pwd
- 关于如何将存储在ftp上的文件下载到本地并自由选择保存地址问题
- 解决jsp底部footer一直处于界面底端的问题(自适应,随着屏幕滚动)
- 阿里云服务器ECS centos7.2搭建nginx环境以及负载均衡
- nginx学习笔记
- 计算机视觉(ComputerVision, CV)相关领域的网站链接
- ubuntu 二进制安装mysql
- 使用Xcode8打包上传成功后,在iTunes Connect中不能构建版本
- div css sprites精灵-CSS图像拼合 CSS贴图定位网页背景素材图片拼合定位布局技术教程
- LeetCode---ugly number I and II
- 从Tomcat中学习责任链模式
- 相似度(参数调节代码)