Centos7安装ngnix方法和步骤

  • 添加资源库

              在 CentOS 系统上安装 Nginx ,你得先去添加一个资源库,像这样:vim /etc/yum.repos.d/nginx.repo

              使用 vim 命令去打开 /etc/yum.repos.d/nginx.repo ,如果 nginx.repo 不存在,就会去创建一个这样的文件,打开以后按一下小 i 键,进入编辑模式,然后复制粘贴下面这几行代码,完成以后按 esc 键退出,再输入:wq (保存并退出)

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

完成上边操作以后,我们就可以使用 yum 命令去安装 nginx 了

# yum install nginx

安装成功:
测试nginx配置文件
当你执行 nginx -t 得时候,nginx会去测试你得配置文件得语法,并告诉你配置文件是否写得正确,同时也告诉了你配置文件得路径:

nginx –t


打印如下:

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


说明配置文件成功!
centos7.0+ nginx实现停止、启动、重启
在CentOS7中,进行chkconfig命令操作时会发现有类似“systemctl.....”的提示,systemctl可以简单实现service和chkconfig的结合,这样通过一个命令就可以实现两个命令的功能。
systemctl命令的基本操作格式是:
systemctl [OPTIONS...] {COMMAND}...
以nginx服务为例,实现停止、启动、重启的动作如下:

systemctl stop  nginx.service
systemctl start  nginx.service
systemctl restart nginx.service


检查服务状态

systemctl status nginx.service


使服务开机启动

systemctl enable nginx.service

取消服务开机启动

systemctl disable nginx.service

修改/etc/nginx/nginx.conf
   #user nginx;
   user  root;
worker_processes  1;
修改/etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
       # root   /usr/share/nginx/html;
        root   /wwwroot/images;
        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;
         root   /wwwroot/images;
    }


 

猜你喜欢

转载自blog.csdn.net/qq_39444339/article/details/81086694
今日推荐