虚拟机部署Nginx及NginxWebUI

Nginx及Nginx控制台部署

在物理机上新建虚拟机,并部署Nginx及Nginx控制台

一、创建虚拟机并配置

1. 安装vitualbox

VirtualBox-6.1-6.1.16_140961_el7-1.x86_64.rpm

[root@bogon home]# mdkir vagrant  
[root@bogon vagrant]# cd vagrant
[root@bogon vagrant]# rz
[root@bogon vagrant]# yum install VirtualBox-6.1-6.1.16_140961_el7-1.x86_64.rpm

报错:Please install the Linux kernel “header” files matching the current kernel 执行

[root@bogon home]# yum -y install gcc kernel kernel-devel 

安装后重启 运行

[root@bogon ~]# /sbin/vboxconfig

2. 安装vagrant

[root@bogon ~]# yum localinstall vagrant_2.2.14_x86_64.rpm

3. 下载系统文件

官网(http://www.vagrantbox.es)下载需要的虚拟机系统版本

此处下载的是vagrant-centos-7.2.box(https://github.com/CommanderK5/packer-centos-template/releases/download/0.7.2/vagrant-centos-7.2.box)

上传至物理机,/home/vagrant/ 目录下

4. 虚拟机配置

# 添加box
[root@localhost ~]# cd /home/vagrant/
[root@localhost vagrant]# vagrant box add vagrant-centos-7.2 ./vagrant-centos-7.2.box
# 查看本地box列表,可以看到刚创建的
[root@localhost vagrant]# vagrant box list
acs-master         (virtualbox, 0)
vagrant-centos-7.2 (virtualbox, 0)

# 创建虚拟机文件夹
[root@localhost vagrant]# mkdir acs-master-web

[root@localhost vagrant]# cd acs-master-web/

# 新建并编辑配置文件
[root@localhost acs-master-web]# vim Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
Vagrant.configure("2") do |config|
  config.ssh.insert_key = false
  config.vm.box = "vagrant-centos-7.2"
  config.vm.network "public_network",ip:"172.16.44.172",:bridge => "eno1"
  config.vm.provision "shell",inline:"ip route add default via 172.16.44.1"
  config.vm.provider "virtualbox" do |vb|
    vb.name ="acs-master-web"
    vb.memory = "4096"
    vb.cpus = 2
  end
end

5. 进入虚拟机

# 启动虚拟机
[root@localhost acs-master-web]# vagrant up
# 进入虚拟机 切换至root账号
[root@localhost acs-master-web]# vagrant ssh
Last login: Tue Jul 27 19:30:29 2021 from 10.0.2.2
[vagrant@bogon ~]$ su root
密码 vagrant
[root@bogon vagrant]#

6. 网络配置

[root@bogon vagrant]# cd /etc/sysconfig/network-scripts/
[root@bogon network-scripts]# vi ifcfg-enp0s8

#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=no  #修改此处,否则/etc/resove.conf会自动修改
BOOTPROTO=none
ONBOOT=yes
IPADDR=172.16.44.172
NETMASK=255.255.255.0
GATEWAY=172.16.44.1    #添加此处,和配置的IP在同一网段
DEVICE=enp0s8 
PEERDNS=yes            #修改此处,DNS追加到/etc/resove.conf中 并在下面添加DNS服务器地址
DNS1=219.141.136.10
DNS2=202.106.0.20
#VAGRANT-END

# 最后重启网络
[root@bogon network-scripts]#  systemctl restart network

// 至此可使用ssh登入虚拟机

7. 修改yum源

[root@bogon /]# cd /etc/yum.repos.d
[root@bogon yum.repos.d]# cp CentOS-Base.repo CentOS-Base.repo.bak

[root@bogon yum.repos.d]# vi CentOS-Base.repo 
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the# remarked out baseurl= line instead.
#
# 
  
[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7

#released updates
[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7

#additional packages that may be useful
[extras]name=CentOS-$releasever - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-7

# 修改rpm-gpg
[root@bogon yum.repos.d]# cd /etc/pki/rpm-gpg/
[root@bogon rpm-gpg]# vcp RPM-GPG-KEY-CentOS-7 RPM-GPG-KEY-7
[root@bogon rpm-gpg]# vi RPM-GPG-KEY-7

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.5 (GNU/Linux)
mQINBFOn/0sBEADLDyZ+DQHkcTHDQSE0a0B2iYAEXwpPvs67cJ4tmhe/iMOyVMh9Yw/vBIF8scm6T/vPN5fopsKiW9UsAhGKg0epC6y5ed+NAUHTEa6pSOdo7CyFDwtn4HF61Esyb4gzPT6QiSr0zvdTtgYBRZjAEPFVu3Dio0oZ5UQZ7fzdZfeixMQ8VMTQ4y4x5vik9B+cqmGiq9AW71ixlDYVWasgR093fXiD9NLT4DTtK+KLGYNjJ8eMRqfZWs7g7C+9aEGHfsGZ/SxLOumx/GfiTloal0dnq8TC7XQ/JuNdB9qjoXzRF+faDUsjWuvNSQEqUXW1dzJjBvroEvgTdfCJfRpIgOrc256qvDMp1SxchMFltPlo5mbSMKu1x1p4UkAzx543meMlRXOgx2/hnBm6H6L0FsSyDS6P224yF+30eeODD4Ju4BCyQ0jOIpUxmUnApo/m0eRelI6TRl7jK6aGqSYUNhFBu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=OGYX
-----END PGP PUBLIC KEY BLOCK-----

[root@bogon rpm-gpg]# yum makecache

8. 时间设置

# 配置时间及时区 设置时区,同步硬件时间
[root@bogon /]# timedatectl set-timezone Asia/Shanghai
[root@bogon /]# hwclock --systohc --localtime

二、部署Nginx并配置

1. 下载安装包

地址:http://nginx.org/download/nginx-1.16.0.tar.gz

2. 安装依赖库

[root@bogon /]# yum install gcc-c++
[root@bogon /]# yum install pcre
[root@bogon /]# yum install pcre-devel
[root@bogon /]# yum install zlib
[root@bogon /]# yum install zlib-devel
[root@bogon /]# yum install openssl
[root@bogon /]# yum install openssl-devel

3. 安装Nginx

# 上传安装包至root的家目录
[root@bogon ~]# ll
总用量 1016
-rw-r--r--  1 root root 1032345 727 18:54 nginx-1.16.0.tar.gz
# 解压安装包
[root@bogon ~]# tar -xzvf nginx-1.16.0.tar.gz
# 拷贝文件到/usr/local目录
[root@bogon ~]# mv nginx-1.16.0 /usr/local
# 检测当前系统环境
[root@bogon ~]# cd /usr/local/nginx-1.16.0
[root@bogon nginx-1.16.0]# ./configure
# 编译
[root@bogon nginx-1.16.0]# make
# 安装
[root@bogon nginx-1.16.0]# make install

如果一切正常,表示nginx已经成功安装完成,默认安装位置为 /usr/local/nginx,之前的/usr/local/nginx-1.16.0可以删除

4. 开机自启配置

[root@bogon nginx-1.16.0]# cd /lib/systemd/system
[root@bogon system]# vi nginx.service

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

[root@bogon system]# systemctl enable nginx.service

常用启动、停止、重启指令

[root@bogon system]# systemctl start nginx.service
[root@bogon system]# systemctl stop nginx.service
[root@bogon system]# systemctl restart nginx.service

5. 测试:

输入http://服务器IP/ 如果能看到nginx的欢迎界面,就表明安装成功。

三、部署NginxWebUI

开源项目地址:https://gitee.com/cym1102/nginxWebUI

可以下载相关文件,有详细的介绍。这只截取了jar包部署方法

1.安装java

[root@bogon /]# yum -y list java*
[root@bogon /]# yum install java-11-openjdk.x86_64

2. 建文件夹,传jar包

[root@bogon /]# cd /home/ [root@bogon home]# mkdir nginxWebUI

[root@bogon home]# cd nginxWebUI/ 
[root@bogon nginxWebUI]# rz
[root@bogon nginxWebUI]# ll 
总用量 58104 
-rw-r--r-- 1 root root 59258299 727 18:54 nginxWebUI-2.6.3.jar

3. 启动命令

[root@bogon nginxWebUI]# nohup java -jar -Xmx64m /home/nginxWebUI/nginxWebUI.jar --server.port=8080 --project.home=/home/nginxWebUI/ > /dev/null &

参数说明(都是非必填)

-Xmx64m 最大分配内存数

–server.port 占用端口, 默认以8080端口启动

–project.home 项目配置文件目录,存放数据库文件,证书文件,日志等, 默认为/home/nginxWebUI/

–spring.database.type=mysql 使用其他数据库,不填为使用本地sqlite,选项包括mysql和postgresql

–spring.datasource.url=jdbc:mysql://ip:port/nginxwebui 数据库url

–spring.datasource.username=root 数据库用户

–spring.datasource.password=pass 数据库密码

注意命令最后加一个&号, 表示项目后台运行

4. 开机自启

[root@bogon nginxWebUI]# vi /etc/systemd/system/nginxwebui.service
[Unit]
Description=NginxWebUI
After=syslog.target
After=network.target
 
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=/home/nginxWebUI
ExecStart=/usr/bin/java -jar /home/nginxWebUI/nginxWebUI.jar
Restart=always
 
[Install]
WantedBy=multi-user.target

[root@bogon nginxWebUI]# systemctl daemon-reload
[root@bogon nginxWebUI]# systemctl enable nginxwebui.service
[root@bogon nginxWebUI]# systemctl start nginxwebui.service

你可能感兴趣的:(nginx,linux,vagrant)