本文主要介紹如何將一個前端項目部署到基於nginx或者apache的虛擬機上,介紹如何搭建本身的web服務html
首先是安裝虛擬機,安裝過程在這裏省略,接下來主要介紹虛擬機環境的簡單配置,這裏使用的是centos7.16前端
尋找網卡配置文件目錄,並打卡mysql
cd /etc/sysconfig/network-scripts vi ifcfg-ens33
修改網卡配置文件linux
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
配置DNSnginx
cd ../ vi network
添加DNSc++
DNS1=114.114.114.114 DNS2=8.8.8.8
保存後關閉,重啓網卡,就能夠正常上網了。web
systemctl restart network.service
查看剛剛配置的虛擬機IP地址sql
ip addr
關閉防火牆並使防火牆開啓22端口數據庫
firewall-cmd --zone=public --add-port=22/tcp –permanent systemctl restart firewalld.service systemctl restart firewalld.service
爲了使下載依賴包更快,這裏配置了yum源爲阿里雲apache
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
先檢查是否安裝了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提升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
有人會問爲何不是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端口
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搭建靜態服務器,配置代理。
安裝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`
#基於域名 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配置的目錄下就好了