【1】下载对应当前系统版本的nginx包(package)
安装 pcre 依赖:
wget http://downloads.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz
tar -xvf pcre-8.37.tar.gz
# 进入解压后的目录
cd pcre-8.37
./configure
make && make install
安装 openssl 、 zlib 、 gcc 、pcre依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel pcre-devel
下载nginx的rpm包:
wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

【2】建立nginx的yum仓库
这条命令的作用是安装Nginx的yum仓库文件,以便于后续能够通过yum命令方便地安装和管理Nginx软件包。
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm

【3】安装nginx
yum install nginx


nginx 信息如下:
[root@ecs-q97onp softinstall]# yum info nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Installed Packages
Name : nginx
Arch : x86_64
Epoch : 1
Version : 1.26.1
Release : 2.el7.ngx
Size : 2.8 M
Repo : installed
From repo : nginx
Summary : High performance web server
URL : https://nginx.org/
License : 2-clause BSD-like license
Description : nginx [engine x] is an HTTP and reverse proxy server, as well as
: a mail proxy server.
【4】启动nginx服务
Centos7中命令格式如下:
systemctl start nginx

Centos7之前关于Nginx服务常用命令格式如下:
service nginx start #启动 nginx 服务
service nginx stop #停止 nginx 服务
service nginx restart #重启 nginx 服务
Centos7+命令操作:
# 停止服务
systemctl stop nginx
#查看服务状态
systemctl status nginx
#启动服务
systemctl start nginx
#添加开机启动
systemctl enable nginx
# 查看开机启动服务
systemctl list-unit-files |grep enable
在浏览器地址栏中输入部署nginx环境的机器的IP,如果一切正常,应该能看到如下图:

重启NGINX
如果没有配置服务脚本,则可以使用命令进行重启,进入sbin目录:
cd /usr/local/nginx/sbin
./nginx -s reload
【5】nginx的几个默认目录

1) 配置所在目录:/etc/nginx/
2) PID目录:/var/run/nginx.pid
3) 错误日志:/var/log/nginx/error.log
4) 访问日志:/var/log/nginx/access.log
5) 默认站点目录:/usr/share/nginx/html
【6】nginx配置文件
默认配置文件如下图:

修改配置文件如下:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
#include /etc/nginx/conf.d/*.conf;
#设定负载均衡服务器列表
upstream group1{
#后端服务器访问规则
#ip_hash;
#weight参数表示权重值,权值越高被分配到的几率越大
#PC_Local
server 192.168.187.133:80 weight=5;
#PC_Server
server 192.168.187.134:80 weight=5;
}
server {
listen 81; #设置对外端口
server_name 192.168.187.133 ; #设置识别请求域名
location / {
#定义服务器的默认网站根目录位置
#root html;
#定义首页索引文件的名称
#index index.html index.htm index.php;
proxy_pass http://group1 ; #分流到group1集群
}
}
}
【7】查看nginx版本
查看nginx版本:
[root@bogon ~]# ps -ef|grep nginx
root 3496 1 0 2016 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
[root@bogon ~]# cd /usr/local/nginx/sbin/
[root@bogon sbin]# ./nginx -v
nginx version: nginx/1.10.2
【8】卸载Nginx
卸载命令如下:
rpm -e nginx
如果因为依赖包的关系报错,就尝试用:
rpm -e --nodeps nginx

本文介绍如何在CentOS系统上安装Nginx,并详细解释了配置过程及常用命令,包括启动、停止、重启等操作。此外,还提供了负载均衡的配置示例。
801

被折叠的 条评论
为什么被折叠?



