linux下nginx1.16.1安装/部署简单(html)静态页面

使用工具:centos7 (3.10.0) 、 VMware Workstation 15 ProCRT

一、获取安装包

  1. 网页下载
    点此获取 密码:xsnq

  2. 官网获取

//执行语句
[root@xsnq ~]# wget https://nginx.org/download/nginx-1.16.1.tar.gz

二、安装

//安装依赖
[root@xsnq ~]# yum  -y  install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
//解压
[root@xsnq ~]# tar zxvf nginx-1.16.1.tar.gz
[root@xsnq ~]# cd nginx-1.16.1
//配置
[root@xsnq nginx-1.16.1]# ./configure --prefix=/usr/local/nginx
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library
  nginx path prefix: "/usr/local/nginx"
  ...
  
 //编译安装
[root@xsnq nginx-1.16.1]# make && make install
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/root/nginx-1.16.1”

//启动nginx
[root@xsnq nginx-1.16.1]# cd /usr/local/nginx/sbin/
[root@xsnq sbin]# ./nginx 

//查看nginx版本
[root@xsnq sbin]# /usr/local/nginx/sbin/nginx -V

//验证是否安装成功
[root@xsnq sbin]# netstat -naltp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      4349/nginx: master  
[root@xsnq sbin]# ps -ef | grep nginx 
root       4349      1  0 15:13 ?        00:00:00 nginx: master process ./nginx
nobody     4350   4349  0 15:13 ?        00:00:00 nginx: worker process
root       4352   1780  0 15:13 pts/1    00:00:00 grep --color=auto nginx

//添加防火墙规则
[root@xsnq ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@xsnq ~]# firewall-cmd --reload
打开浏览器-->域名栏中输入自己ip地址
安装成功会显示:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working.
Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.

//nginx启动关闭
[root@xsnq ~]# cd /usr/local/nginx/sbin/
[root@xsnq sbin]# ./nginx                           #启动
[root@xsnq sbin]# ./nginx -s stop                   #停止
[root@xsnq sbin]# ./nginx -s reload                 #重启
//至此已安装成功

//添加nginx服务
[root@xsnq sbin]# vi /lib/systemd/system/nginx.service
添加以下“*”号包括内容:
*
[Unit]
Description=nginx
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 quit
PrivateTmp=true
*
#注释
[Unit]:对服务的说明
Description:描述服务
After:描述服务类别

[Service]服务运行参数的设置
Type=forking后台运行的形式
ExecStart服务的具体运行命令
ExecReload服务的重启命令
ExecStop服务的停止命令
PrivateTmp=True表示给服务分配独立的临时空间
#
//重启nginx服务
[root@xsnq sbin]# ./nginx -s  reload
//可用systemctl对nginx启动关闭
systemctl start nginx                              #启动
systemctl stop nginx                               #停止
systemctl reload nginx                             #重启

三、部署简单的静态页面

写一个简单的测试文件

mv /usr/local/nginx/html/index.html /usr/local/nginx/html/index.html.old
vi /usr/local/nginx/html/index.html

index.html中内容

<!DOCTYPE html>
<html>
<head>
<title>My nginx server</title>
<style>
    body {
		    margin: auto;
		    width: 60%;
		    border: 3px solid #73AD21;
		    padding: 10px;
         }
</style>
</head>
<body>
<h1>My nginx server</h1>
<p>Open this page, which means you have successfully configured!</p>
</body>
</html>

修改配置文件(这里我是替换了默认的文件,所以不需要做修改):

vi /usr/local/nginx/conf/nginx.conf
     35 server {
     36         listen       80;  //端口号,浏览器测试时,域名写入 ip:端口号
     37         server_name  localhost;//本机地址
     38 
     39         #charset koi8-r;
     40 
     41         #access_log  logs/host.access.log  main;
     42 
     43         location / {
     44             root   html;  //静态文件相对路径
     45             index  index.html index.htm; //顺序读取文件
     46         }

若修改了配置文件,请重启nginx服务;
修改nginx.conf文件时,注意45行空格和其他行“{ }”符号有没有被误删;

本文出现任何错误,请留言批评指正。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值