centos安装nginx

欢迎访问我的个人博客网站:http://www.yanmin99.com/

一、在/etc/yum.repos.d/目录下创建一个源配置文件nginx.repo

vim /etc/yum.repos.d/nginx.repo

//创建内容
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

保存以后会在/etc/yum.repos.d/中创建nginx.repo文件

二、yum install nginx -y安装

yum install nginx -y

三、Nginx的命令以及配置文件位置

/etc/init.d/nginx start # 启动Nginx服务
/etc/init.d/nginx stop # 停止Nginx服务
/etc/nginx/nginx.conf # Nginx配置文件位置
chkconfig nginx on    #设为开机启动

四、设置防火墙

//设置80端口白名单
iptables -I INPUT 5 -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
//保存
service iptables save
//重启防火墙
service iptables restart

五、配置Nginx文件

  • 1、编辑/etc/nginx/nginx.conf
//在nginx.conf中,有一行代码,代表它去加载文件夹/conf.d/中的所有.conf文件
include /etc/nginx/conf.d/*.conf;
  • 2、编辑/etc/nginx/conf.d文件
server {
     listen       80;
     server_name  localhost;
 
     #charset koi8-r;
     #access_log  /var/log/nginx/host.access.log  main;
 
     location / {
         root   /usr/share/nginx/html;
         index  index.html index.htm;
     }
     ....
 }
  • 3、一台主机上适应多个服务器
http {
....
  server {
          listen       80;
          server_name  www.a.com;
          charset utf-8;
          access_log  /home/a.com.access.log  main;
          location / {
              proxy_pass http://127.0.0.1:80;
          }
      }
  
   server {
          listen       80;
          server_name  www.b.com;
          charset utf-8;
          access_log  /home/b.com.access.log  main;
          location / {
              proxy_pass http://127.0.0.1:81;
          }
      }
...

你可能感兴趣的:(centos安装nginx)