系统基础环境:
# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
# cat /proc/version Linux version 3.10.0-957.5.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Fri Feb 1 14:54:57 UTC 2019
安装方法一:源码编译安装
基础环境 : CentOS Linux release 7.6.1810 (Core)
目录:/usr/local/src
1.1、安装前准备(根据各软件最新版本信息更改url)
下载nginx、openssl、zlib、pcre
# cd /usr/local/src # wget http://nginx.org/download/nginx-1.16.0.tar.gz # wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz # wget http://zlib.net/zlib-1.2.11.tar.gz # wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
1.2、安装(顺序不要错)
安装make
# yum -y install gcc automake autoconf libtool make
安装gcc编译环境
# yum install gcc-c++
安装pcre
# tar zxvf pcre-8.43.tar.gz # cd pcre-8.43 # ./configure && make && make install
安装openssl:
# cd .. # tar zxvf openssl-fips-2.0.10.tar.gz # cd openssl-fips-2.0.10 # ./config && make && make install
安装zlib
# cd .. # tar zxvf zlib-1.2.11.tar.gz # cd zlib-1.2.11 # ./configure && make && make install
安装nginx
# cd .. # tar zxvf nginx-1.16.0.tar.gz # cd nginx-1.16.0 # ./configure && make && make install
这样安装完成后,nginx的路径为/usr/local/nginx/sbin/nginx
# whereis nginx
nginx: /usr/local/nginx
安装方法二:
查看CentOS的版本
# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
添加资源库 , 在 CentOS 系统上安装 Nginx ,得先去添加一个资源库:
# vim /etc/yum.repos.d/nginx.repo
输入如下代码:
[nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=0 enabled=1
安装nginx
# yum -y install nginx
使用命令
# systemctl stop nginx.service #停止nginx服务
# systemctl start nginx.service #打开nginx服务
# systemctl restart nginx.service #重启nginx服务
# systemctl status nginx.service #查看nginx服务状态
开启并验证nginx安装成果:
开启nginx:# cd /usr/local/nginx/sbin # ./nginx
或
# systemctl start nginx.service
# curl http://localhost
应该能输出Welcome to nginx的html文件,说明安装启动成功。
开启外部访问
centos7默认开启了防火墙模块firewalld,可以查看一下是否开启
# ystemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled) Active: active (running) since Mon 2019-03-18 10:12:46 EDT; 34s ago Docs: man:firewalld(1) Main PID: 3618 (firewalld) CGroup: /system.slice/firewalld.service └─3618 /usr/bin/python -Es /usr/sbin/firewalld --nofork... Mar 18 10:12:46 localhost.localdomain systemd[1]: Starting firewal... Mar 18 10:12:46 localhost.localdomain systemd[1]: Started firewall... Hint: Some lines were ellipsized, use -l to show in full. 或者: # firewall-cmd --state running
关闭firewalld,然后在虚拟机外可以访问
# systemctl stop firewalld #关闭
# systemctl start firewalld #打开
# systemctl restart firewalld #重启
# systemctl disable firewalld #关闭开机启动
# systemctl enable firewalld #开机启动
外部访问nginx主页:http://***.***.***.***
添加 nginx 为系统服务,设置开机自启
1、在 /lib/systemd/system 目录添加 nginx.service 文件
# vim /lib/systemd/system/nginx.service
2、写入以下内容,保存退出
[Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop ExecRestart=/usr/local/nginx/sbin/nginx -s restart PrivateTmp=true [Install] WantedBy=multi-user.target
3、设置开机自启
# systemctl enable nginx
4、其它命令
# 启动 nginx
systemctl start nginx
# 停止
systemctl stop nginx
# 加载配置文件
systemctl reload nginx
# 重启
systemctl restart nginx
# 查看状态
systemctl status nginx