epol源安装
1 |
[root@web01 ~]# yum install -y nginx |
官方源安装
1)配置官方源
1 2 3 4 5 6 7 8 |
[root@web02 ~]# vim /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true |
2)安装依赖
1 |
[root@web02 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree |
3)安装nginx
1 |
[root@web02 ~]# yum install -y nginx |
4)启动服务
1 2 3 |
[root@web02 ~]# systemctl start nginx #或者 [root@web02 ~]# nginx |
5)检验启动
1 2 3 4 5 6 7 8 9 10 11 |
#方式一: [root@web02 ~]# ps -ef | grep nginx #方式二: [root@web02 ~]# netstat -lntp | grep 80 #方式三: 访问页面 10.0.0.8:80 #方式四: #查看版本 [root@web02 ~]# nginx -v #查看安装模块 [root@web02 ~]# nginx -V |
6)nginx常用命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
1.启动命令 [root@web02 ~]# systemctl start nginx #或者 [root@web02 ~]# nginx #注意,使用哪种方式启动就用哪种方式关闭 2.关闭命令 [root@web02 ~]# systemctl stop nginx #或者 [root@web02 ~]# nginx -s stop 3.nginx重启 [root@web02 ~]# systemctl restart nginx 4.nginx重载配置文件 [root@web02 ~]# systemctl reload nginx #或者 [root@web02 ~]# nginx -s reload 5.检查nginx配置 [root@web01 ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 6.加入开机自启 [root@web01 ~]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. 7.CentOS6操作 #启动 [root@web01 ~]# nginx [root@web01 ~]# /etc/init.d/nginx start [root@web01 ~]# service nginx start #配置开机自启 [root@web01 ~]# chkconfig nginx on |
源码包安装
1)安装依赖
1 |
[root@web03 ~]# yum install -y gcc gcc-c++ autoconf pcre pcre-devel make automake wget httpd-tools vim tree |
2)下载或者上传包
1 2 3 |
[root@web03 ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz #或者 [root@web03 ~]# rz nginx-1.18.0.tar.gz |
3)解压
1 |
[root@web03 ~]# tar xf nginx-1.18.0.tar.gz |
4)创建用户
1 2 |
[root@web03 ~]# groupadd www -g 666 [root@web03 ~]# useradd www -u 666 -g 666 |
5)生成
1 2 |
[root@web03 ~]# cd nginx-1.18.0/ [root@web03 ~/nginx-1.18.0]# ./configure --prefix=/usr/local/nginx-1.18.0 --user=www --group=www --with-http_addition_module --with-http_auth_request_module --without-http_gzip_module |
6)编译安装
1 |
[root@web03 ~/nginx-1.18.0]# make && make install |
7)配置system管理
1 2 3 4 5 6 7 8 9 10 11 12 |
#配置system管理 [root@web03 ~]# vim /etc/systemd/system/nginx.service [Unit] Description=nginx - high performance web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop [Install] WantedBy=multi-user.target |
8)做软链接
1 2 3 4 5 6 7 8 9 10 11 12 |
[root@web03 ~]# ln -s /usr/local/nginx-1.18.0 /usr/local/nginx #配置环境变量 [root@web03 ~]# cat /etc/profile.d/nginx.sh export PATH=/usr/local/nginx/sbin/:$PATH #重新加载环境变量 [root@web03 ~]#source /etc/profile #软连接的作用: 1.配置环境变量可以不加版本号- 2.配置system启动可以不加版本号 3.升级直接切换软连接的链接文件即可 |
9)启动
1 2 3 4 |
[root@web03 ~]# systemctl daemon-reload [root@web03 ~]# systemctl start nginx #配置开机自启 [root@web03 ~]# systemctl enable nginx |