本文主要介绍如何将一个前端项目部署到基于nginx或者apache的虚拟机上,介绍如何搭建自己的web服务
虚拟机安装配置
首先是安装虚拟机,安装过程在这里省略,接下来主要介绍虚拟机环境的简单配置,这里使用的是centos7.16
配置虚拟机网卡信息
寻找网卡配置文件目录,并打卡
cd /etc/sysconfig/network-scripts
vi ifcfg-ens33
修改网卡配置文件
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=ens33
UUID=fe0ce15e-460a-458f-a7ad-bbc1ac41e8cf
DEVICE=ens33
ONBOOT=yes
IPADDR=172.16.6.252
GATEWAY=172.16.6.1
NETMASK=255.255.255.0
NM_CONTROLLED=no
DNS=114.114.114.114
配置DNS
cd ../
vi network
添加DNS
DNS1=114.114.114.114
DNS2=8.8.8.8
保存后关闭,重启网卡,就可以正常上网了。
systemctl restart network.service
查看刚刚配置的虚拟机IP地址
ip addr
关闭防火墙并使防火墙开启22端口
firewall-cmd --zone=public --add-port=22/tcp –permanent
systemctl restart firewalld.service
systemctl restart firewalld.service
配置yum源
为了使下载依赖包更快,这里配置了yum源为阿里云
yum install -y wget
备份/etc/yum.repos.d/CentOS-Base.repo文件
cd /etc/yum.repos.d/
mv CentOS-Base.repo CentOS-Base.repo.back
下载阿里云的Centos-7.repo文件
wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
安装iptable 管理防火墙和端口号
先检查是否安装了iptables
service iptables status
如果没有安装iptables
yum install -y iptables
##升级iptables(安装的最新版本则不需要)
yum update iptables
安装iptables-services
yum install iptables-services
##停止firewalld服务
systemctl stop firewalld
##禁用firewalld服务
systemctl mask firewalld
为虚拟机防火墙增加规则
vi /ect/sysconfig/iptables
##增加规则
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
Apache 安装
使用apache提高HTTP Server,下面是apache的安装命令
sudo yum install httpd
sudo systemctl enable httpd
sudo systemctl start httpd
##启动
sudo aystemctl start httpd.service
sudo service apache2 start
##停止和重启
sudo aystemctl stop httpd.service
sudo aystemctl restart httpd.service
安装完后配置apache的配置文件,配置web服务器
sudo vim /etc/httpd/conf/httpd.conf
这里只介绍基于名称的虚拟主机配置方式,其他配置方式都很简单,可以在apache的官方文档上直接找到
vim /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html/hjyb"
ServerName www.hjyb.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/hjya"
ServerName www.hjya.com
</VirtualHost>
配置完毕后重启apache服务
service httpd restart
安装Mariadb
有人会问为什么不是mysql,MariaDB是MySQL源代码的一个分支,在意识到Oracle会对MySQL许可做什么后分离了出来(MySQL先后被Sun、Oracle收购)。除了作为一个Mysql的“向下替代品”,MariaDB包括的一些新特性使它优于MySQL。
下面是安装命令
yum install mariadb mariadb-service
systemctl start mariadb.service
如果发现重启不了
yum search mariadb
yum install mariadb-bench mariadb-devel mariadb-embedded mariadb-embedded-devel mariadb-libs mariadb-server mariadb mariadb-test
systemctl start mariadb.service
使用navicat连接数据库,如果发现连接不上,是因为centOS7默认是阻止3306端口
安装rar解压rar
wget http://www.rarlab.com/rar/rarlinux-x64-5.3.0.tar.gz
tar -zxvf rarlinux-x64-5.3.0.tar.gz
cd rar
make
#解压
#rar x text.rar
#rar test.rar ./test /将test目录打包为test.rar
安装nginx
安装nginx搭建静态服务器,配置代理。
安装CentOS编译环境
yum -y install gcc automake autoconf libtool make
yum install gcc gcc-c++
选定源码目录
cd /usr/local/src
安装PCRE库
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
tar -zxvf pcre-8.38.tar.gz
cd pcre-8.38
./configure
make
make install
安装zlib库
wget http://zlib.net/zlib-1.2.11.tar.gz
tar –zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make
make install
安装ssl
wget https://www.openssl.org/source/openssl-1.0.1t.tar.gz
tar –zxvf opensll-1.0.1t.tar.gz
安装nginx
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar -zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure --sbin-path=/usr/local/nginx/nginx \
--conf-path=/usr/local/nginx/nginx.conf \
--pid-path=/usr/local/nginx/nginx.pid \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.38 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.0.1t
make
make install
nginx启用
sudo /usr/local/nginx/nginx
如果提示一下信息
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
说明80端口被占用
netstat –antp
修改nginx启动端口 vim /etc/local/nginx/nginx.conf
Server里的listen
/usr/local/nginx/nginx
查看nginx是否运行成功
nginx 停止命令
ps aux|grep nginx
kill –INT nginx进程号 快速停止nginx服务
kill –HUP nginx 进程号(不重启nginx,软关闭)
./nginx/nginx –s reload
kill –QUIT nginx主进程号 优雅的关闭
kill -9 nginx主进程号
kill –HUP `cat logs/nginx.pid`
nginx虚拟主机配置方法
#基于域名
server {
listen 8002;
server_name z.com;
location / {
root z.com;
index index.html;
}
}
#基于端口
server {
listen 2002;
server_name z.com;
location / {
root /var/www/test;
index index.html;
}
access_log logs/z.com.aceesss.log main;
}
#基于IP
server {
listen 8004;
server_name 172.16.42.206;
location / {
root html/ip;
index index.html;
}
}
#修改默认端口
server {
listen 8001;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
#配置反向代理
server {
listen 80;
root /var/www/html/hjyb;
index index.html;
server_name 172.16.42.206;
location / {
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded $proxy_add_x_forwarded_for;
proxy_pass http://172.16.42.206:7001/;
}
}
配置完nginx,web服务器就搭建完毕了,接下来就可以将前端打包好的代码扔到nginx的root配置的目录下就行了