CentOS 7 安装 nginx

方式一:经过repo安装

第一步 - 添加Nginx存储库

要添加CentOS 7 EPEL仓库,请打开终端并使用如下命令:html

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

输入如下内容:nginx

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

保存后退出c++

第二步 - 安装Nginx

如今Nginx存储库已经安装在您的服务器上,使用如下yum命令安装Nginx :centos

sudo yum install nginx

在对提示回答yes后,Nginx将在服务器上完成安装。浏览器

第三步 - 启动Nginx

Nginx不会自行启动。要运行Nginx,请输入:服务器

sudo systemctl start nginx

若是您正在运行防火墙,请运行如下命令以容许HTTP和HTTPS通讯:ui

sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

您将会看到默认的CentOS 7 Nginx网页url

若是想在系统启动时启用Nginx。请输入如下命令:code

sudo systemctl enable nginx

恭喜!Nginx如今已经安装并运行了!htm

第四步 - 查看Nginx版本

nginx -v

第五步 - 配置Nginx

nginx配置文件位置在/etc/nginx/ 修改nginx配置文件

vi  /etc/nginx/conf.d/default.conf

重载服务

/usr/sbin/nginx -s reload

方式二:经过源码安装

一、准备工做

Nginx的安装依赖如下文件:
1.一、安装gcc:     yum install gcc-c++
    1.2 、安装PCRE pcre-devel:    yum install -y pcre pcre-devel
    1.3 、安装zlib:    yum install -y zlib zlib-devel
    1.四、安装OpenSSL:    yum install -y openssl openssl-devel

二、下载nginx

http://nginx.org/en/download.html

三、解压缩到安装目录

[root@ou Downloads]# tar -xzvf nginx-1.12.2.tar.gz -C /opt

四、配置

[root@ou nginx-1.12.2]# ./configure

默认生成配置信息以下

nginx path prefix: "/usr/local/nginx"
      nginx binary file: "/usr/local/nginx/sbin/nginx"
      nginx modules path: "/usr/local/nginx/modules"
      nginx configuration prefix: "/usr/local/nginx/conf"
      nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
      nginx pid file: "/usr/local/nginx/logs/nginx.pid"
      nginx error log file: "/usr/local/nginx/logs/error.log"
      nginx http access log file: "/usr/local/nginx/logs/access.log"
      nginx http client request body temporary files: "client_body_temp"
      nginx http proxy temporary files: "proxy_temp"
      nginx http fastcgi temporary files: "fastcgi_temp"
      nginx http uwsgi temporary files: "uwsgi_temp"
      nginx http scgi temporary files: "scgi_temp"

五、编译安装

[root@ou nginx-1.12.2]# make
    [root@ou nginx-1.12.2]# make install

六、确认nginx安装路径

[root@ou nginx-1.12.2]# whereis nginx
    nginx: /usr/local/nginx

七、启动、中止nginx

cd /usr/local/nginx/sbin/
    ./nginx 
    ./nginx -s stop
    ./nginx -s quit
    ./nginx -s reload
    ./nginx -s quit:此方式中止步骤是待nginx进程处理任务完毕进行中止。
    ./nginx -s stop:此方式至关于先查出nginx进程id再使用kill命令强制杀掉进程。
浏览器打开localhost便可访问

八、查询nginx进程:

ps aux|grep nginx

九、重启ngixn

9.一、先中止再启动(推荐):
对 nginx 进行重启至关于先中止再启动,即先执行中止命令再执行启动命令。以下:
            ./nginx -s quit
            ./nginx
9.二、从新加载配置文件:
当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效须要重启 nginx,使用-s reload不用先中止             
        ngin x再启动 nginx 便可将配置信息在 nginx 中生效,以下:
        ./nginx -s reload
相关文章
相关标签/搜索