零基础部署项目,详细

 

1.  linux系统的网络配置

创建host-only网卡

设置当前虚拟机网卡1

NAT目的设置,虚拟机系统与主机共享同一个网络,主机可以访问访问,那么虚拟机linux也可以访问外网

主机访问网络测试

linux访问网络测试

网卡2

网卡2使用host-only,目的解决主机与linux系统进行网络通信,就是主机可以连接到linux系统

2.  部署项目

2.1.  介绍

在企业中,一般都采用linux系统作为Web应用服务器,所以我们需要在linux系统搭建项目运行环境。

2.1.1.    上传软件到linux系统

linux系统上搭建运行环境需要安装jdkmyqlnginxredistomcat相关软件。大家根据将软件目录下的所有软件上传到虚拟机上“/soft”目录下,注意不能将windows中文目录下的文件直接拖拽上传,需要将文件放到非中文目录下;

注意:安装这些软件前提,虚拟机安装centos7系统时,软件选择不能最小需要选择基本网页服务器开发工具

系统安装是,点击软件选择”,进行如下选择,最后点击完成

选择完成

2.1.2.    解决客户端连接工具乱码问题

linux系统soft目录上传一个中文文件"黑马程序员.txt",如下图

使用SSH客户端连接工具,查看此目录数据,会发现中文文件乱码如下图:

由于SSH客户端软件的码表跟随windows系统码表一致,在中国的windows系统码表都是GBK,然而linux系统默认码表是utf-8所以,出现客户端查询乱码,解决方法是修改linux系统的默认码表

运行如下命令,查看linux系统默认码表

vim /etc/locale.conf

修改为如下,保存退出

关闭已有SSH客户端连接工具,重新打开查看效果如下

2.2.  软件安装

2.2.1.    软件安装命令rpm

·       rpm的作用,相当于软件助手,可以查询已安装的软件、卸载软件和安装软件。

·       格式:rpm 参数

-v 显示指令执行过程。

​ -h--hash  安装时列出标记。

​ -q query,查询。

​ -aall,所有安装的软件

​ -i 进行安装软件

​ -U update更新升级

​ -e卸载,删除指定的套件。

​ --nodeps 不验证软件的相互关联性

·       常用

安装:rpm -ivh rpm文件【安装】

升级:rpm -Uvh rpm文件【更新】

删除或卸载:rpm -e --nodeps 软件名

查看所有安装的软件:rpm -qa

2.2.2.    jdk安装

·       步骤1:查看当前Linux系统是否已经安装java

输入 rpm -qa | grep java

如图,说明系统没有安装jdk步骤2:进入“/soft”目录,解压jdk/usr/local下, tar -zxvf jdk-9.0.4_linux-x64_bin.tar.gz -C /usr/local/

·       步骤2:进入“/soft”目录,解压jdk/usr/local下, tar -zxvf jdk-9.0.4_linux-x64_bin.tar.gz -C /usr/local/

查看解压后的目录,目录中有jdk-9.0.4jdk解压的目录

·       步骤3:配置jdk环境变量,打开/etc/profile配置文件,将下面配置拷贝进去

#set java environment
    JAVA_HOME=/usr/local/jdk-9.0.4
    CLASSPATH=.:$JAVA_HOME/lib.tools.jar
    PATH=$JAVA_HOME/bin:$PATH
    export JAVA_HOME CLASSPATH PATH

命令1vim /etc/profile

命令2:在文件末尾处,输入“o”,复制上面的环境变量配置粘贴如图位置,并写入保存

·       步骤4:重新加载/etc/profile配置文件,并测试

2.2.3.    mysql安装

·       步骤1:查看CentOS是否自带的mysql


rpm -qa | grep mysql

说明没有自带mysql

·       步骤2:解压Mysql/usr/local/下的mysql目录(mysql目录需要手动创建)


cd /usr/local
mkdir mysql
cd
tar -xvf MySQL-5.6.22-1.el6.i686.rpm-bundle.tar -C /usr/local/mysql

·       步骤3:在/usr/local/mysql下安装mysql安装服务器端:rpm -ivhMySQL-server-5.6.22-1.el6.i686.rpm

从上图可以看出安装mysql服务器端软件需要依赖如下软件:


卸载mariadb(系统自带的数据库,在安装mysql之前卸载这个软件)
libaio.so.1
libc.so.6
libgcc_s.so.1(这个版本有冲突,需要先卸载再安装)
libstdc++.so.6(这个版本有冲突,需要先卸载在安装)

卸载mariadb数据库软件

需要逐个运行命令:yum install XXX软件,yum是去网络下载指定的软件进行安装


yum install libaio.so.1
yum install libc.so.6

先卸载 libgcc 再安装 libgcc


rpm -qa|grep libgcc
rpm -e--nodeps libgcc-4.8.5-16.el7.x86_64
yum install libgcc_s.so.1

卸载libstdc


rpm -qa|grep libstdc
rpm -e--nodeps libstdc++-4.8.5-16.el7.x86_64
rpm -e--nodeps libstdc++-devel-4.8.5-16.el7.x86_64

再安装libstdc++.so.6


yum install libstdc++.so.6

重新执行:rpm -ivh MySQL-server-5.6.22-1.el6.i686.rpm

·       步骤4:安装客户端:rpm -ivh MySQL-client-5.6.22-1.el6.i686.rpm

执行效果

从上图看出,需要安装如下软件


libncurses.so.5

执行安装软件


yum install libncurses.so.5

再次执行:rpm -ivh MySQL-client-5.6.22-1.el6.i686.rpm

·       步骤5:启动mysql

service mysql start

·       步骤6:将mysql加到系统服务中并设置开机启动

加入到系统服务:chkconfig --add mysql自动启动:chkconfig mysql on

·       步骤7:登录mysql

输入:mysql -uroot -p,会发送错误如下

mysql安装好后会生成一个临时随机密码,存储位置在/root/.mysql_secret,看看安装mysql服务器端时提示

进入用户目录输入如下命令

cat .mysql_secret 查看随机生成密码

使用该密码登录mysql

·       步骤8:修改mysql的密码

set password = password('root'); //password函数的参数是密码

·       步骤9:开启mysql的远程登录权限

默认情况下mysql为安全起见,不支持远程登录mysql,所以需要设置开启远程登录mysql的权限登录mysql后输入如下命令:


grantallprivilegeson *.* to'root'@'%' identified by 'root';
flushprivileges;

·       步骤10:开放Linux的对外访问的端口3306(重点)

开启端口

#开放3306端口
/sbin/iptables -I INPUT -p tcp --dport3306-j ACCEPT
#开放的端口永久保存到防火墙
firewall-cmd --zone=public --add-port=3306/tcp --permanent#拓展资料
#firewall-cmd --reload  //重启防火墙(这里不需要执行)

·       步骤11:在本地windows系统使用SQLyogEnt.exe软件连接虚拟机中的linux系统安装的mysql

登录后的效果

·       步骤12:执行如下命令

使用本地客户端连接工具查看

到此说明mysql已经安装好了

2.2.4.   redis安装

2.2.4.1. 安装环境

redisC语言开发,安装redis需要先将官网下载的源码进行编译,编译依赖gcc环境。如果没有gcc环境,需要安装gcc

yum install gcc-c++

如果提示是否下载,输入y。安装过程信息如下

2.2.4.2. redis安装

步骤1:上传"redis-3.0.0.tar.gz"linux系统/soft目录下

步骤2:进入soft目录,"redis-3.0.0.tar.gz"解压到/usr/local目录下


cd /soft
tar -xvf redis-3.0.0.tar.gz -C /usr/local

步骤3:进入redis-3.0.0目录使用make命令编译redis,如下信息代表编译成功

步骤4:redis-3.0.0目录中使用 make PREFIX=/usr/local/redis install 命令安装redis/usr/local/redis

2.2.4.3. redis启动
2.2.4.3.1.   前端模式启动

直接运行bin/redis-server将以前端模式启动,前端模式启动的缺点是启动完成后,不能再进行其他操作,如果要操作必须使用ctrl+c,同时redis-server程序结束,不推荐使用此方法。

/usr/local/redis/bin/redis-server

使用CTRL+ C 停止前端模式

2.2.4.3.2.   后端模式启动

/usr/local/redis-3.0.0/redis.conf 复制到 /usr/local/redis/bin/

修改redis.conf配置文件, daemonize yes 以后端模式启动。

vim /usr/local/redis/bin/redis.conf

启动时,指定配置文件

cd /usr/local/redis/bin
./redis-server redis.conf

Redis默认端口6379,通过当前服务进行查看

ps-ef | grep-i redis
2.2.4.4. 启动客户端命令

进入redis/bin目录,启动"redis-cli"

./redis-cli
2.2.4.5. 远程连接

如需远程连接redis,需配置redis端口6379linux防火墙中开发


#开放6379端口
/sbin/iptables -I INPUT -p tcp --dport6379-j ACCEPT
#开放的端口永久保存到防火墙
firewall-cmd --zone=public --add-port=6379/tcp --permanent
2.2.4.6. redis停止

强制结束程序。强行终止Redis进程可能会导致redis持久化数据丢失。

kill-931475       #pid需要通过“ps -ef|grep redis”进行查询

正确停止Redis的方式应该是向Redis发送SHUTDOWN命令,方法为:(关闭默认端口)


cd /usr/local/redis
./bin/redis-cli shutdown

2.2.5.    tomcat安装

·       步骤1:解压Tomcat/usr/local


tar -zxvf apache-tomcat-8.5.27.tar.gz  -C /usr/local/

·       步骤2:开放Linux的对外访问的端口8080


/sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
firewall-cmd --zone=public --add-port=8080/tcp --permanent

·       步骤3:启动关闭Tomcat


进入tomcatbin下启动:./startup.sh
进入tomcatbin下关闭:./shutdown.sh

·       步骤4:打开浏览器浏览

2.3.  项目发布

2.3.1.    linux系统myql导入数据库

·       步骤1:本地数据库导出

导出sql文件位置

·       步骤2linux系统mysql导入数据库文件

选择sql文件导入

查看导入的表与数据

2.3.2.    导出war包部署到tomcat

步骤1:上传war文件

步骤2:修改配置文件

步骤3:打开浏览器浏览网站

2.3.3.    linux系统中mysql软件的特殊处理

特殊处理的原因:

       上面部署的项目发现搜索功能无效,这是由于mysql软件底层码表使用的不是utf-8导致请求参数中文会乱码,最终导致无法查询的数据

解决方案(修改mysql底层码表):

第一步:进入如下文件进行编辑

/usr/share/doc/MySQL-server-5.6.22/my-default.cnf

第二步:增加如下命令:vim my-default.cnf

[mysqld](已有的,不需要复制,复制下面三行)

character-set-server =utf8

[client]

default-character-set=utf8

第三步:文件保存退出

第四步:cp my-default.cnf /etc/my.cnf

第五步:service mysql restart

2.4.  nginx反向代理

我们为了保证web应用服务器的安全,不可以让web应用服务器连接外网。用户请求nginx,由nginx分发请求给web应用服务器。这样nginx可以连接外网,web应用服务器不需要连接外网。这种模式就是使用了nginx的反向代理实现。

2.4.1.    正向代理

正向代理类似一个跳板机,代理访问外部资源。

举个例子:

  我是一个用户,我访问不了某网站,但是我能访问一个代理服务器,这个代理服务器呢,它能访问那个我不能访问的网站,于是我先连上代理服务器,告诉他我需要那个无法访问网站的内容,代理服务器去取回来,然后返回给我。从那个网站的角度来看,只在代理服务器来取内容的时候有一次记录,有时候并不知道是用户的请求,也隐藏了用户的资料,这取决于代理告不告诉网站。

注意:客户端必须设置正向代理服务器,当然前提是要知道正向代理服务器的IP地址,还有代理程序的端口。

本地客户端设置代理服务器操作如下:

正向代理原理

总结来说:正向代理是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端必须要进行一些特别的设置才能使用正向代理。

正向代理的用途:

  (1)访问原来无法访问的资源,如google

2可以做缓存,加速访问资源

  (3)对客户端访问授权,上网进行认证

  (4)代理可以记录用户访问记录(上网行为管理),对外隐藏用户信息

2.4.2.    反向代理介绍

反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

2.4.3.    反向代理的作用

1保证内网的安全,可以使用反向代理提供防火墙(WAF)功能,阻止web攻击。大型网站,通常将反向代理作为公网访问地址,Web服务器是内网。

2)负载均衡,通过反向代理服务器来优化网站的负载。当大量客户端请求代理服务器时,反向代理可以集中分发不同用户请求给不同的web服务器进行处理请求。(本阶段不实现,负载均衡技术项目阶段讲解)

2.4.4.    反向代理实现原理与正向代理区别

正向代理中,proxyclient同属一个局域网(Local Area NetworkLAN),对server透明;

反向代理中,proxyserver同属一个局域网(Local Area NetworkLAN),对client透明;

实际上proxy在两种代理中做的事都是代为收发请求和响应,不过从结构上来看正好左右互换了下,所以把后出现的那种代理方式叫反向代理。

2.4.5.    nginx安装

说明nginxwindows版是直接可以使用的软件,而linux版是c语言源代码。

·       步骤1:安装依赖软件

执行如下命令,第一个命令下载38M


yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

·       步骤2:解压“nginx-1.13.8.tar.gz”"/usr/local"


tar -zxvf nginx-1.13.8.tar.gz -C /usr/local/

·       步骤3:创建安装目录


mkdir /usr/local/nginx

·       步骤4:修改配置


cd  /usr/local/nginx-1.13.8/
./configure --prefix=/usr/local/nginx  

执行命令的过程信息如下:


[root@localhost soft]# cd  /usr/local/nginx-1.13.8/
[root@localhost nginx-1.13.8]# ./configure --prefix=/usr/local/nginx
checking for OS
 + Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
checking for gcc -pipe switch ... found
...
checking for getaddrinfo() ... found
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for zlib library ... found
creating objs/Makefile
​
Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library
​
  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
​
[root@localhost nginx-1.13.8]#  

·       步骤5:安装


make && make install

执行命令信息:


[root@localhost nginx-1.13.8]# make && make install
make -f objs/Makefile
make[1]: 进入目录“/usr/local/nginx-1.13.8”
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
    -o objs/src/core/nginx.o \
    src/core/nginx.c

cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
    -o objs/src/core/ngx_buf.o \
    src/core/ngx_buf.c
...
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/logs' \
    || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/logs' \
    || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
    || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
    || mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/usr/local/nginx-1.13.8”
[root@localhost nginx-1.13.8]# 

安装完成的目录

步骤6:启动nginx


#程序位置:/usr/local/nginx/sbin/nginx 
#配置文件位置:/usr/local/nginx/conf/nginx.conf
cd /usr/local/nginx/sbin/
./nginx

步骤7:开放端口80


#开放80端口
/sbin/iptables -I INPUT -p tcp --dport80-j ACCEPT
#开放的端口永久保存到防火墙
firewall-cmd --zone=public --add-port=80/tcp --permanent

步骤8:打开浏览器远程访问nginx

2.4.6.    常用命令


#查看运行进程状态:
ps aux | grep nginx
​
#停止nginx:
./nginx -sstop#重启nginx(配置文件变动后需要重启才能生效):
./nginx -s reload
​
#检查配置文件是否正确:
./nginx -t#查看nginx版本
./nginx -v

2.4.7.    nginx配置反向代理

·       步骤1:编辑配置文件


vim nginx.conf

执行信息如下,nginx.conf默认配置文件内容


[root@localhost conf]# vim nginx.conf
user  nobody;
​
worker_processes  1;
​
error_log  logs/error.log;
​
error_log  logs/error.log  notice;
​
error_log  logs/error.log  info;
​
pid        logs/nginx.pid;
​
  events {
​
      worker_connections  1024;
​
  }
​
  http {
​
      include       mime.types;
      default_type  application/octet-stream;
    
      #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
      #                  '$status $body_bytes_sent "$http_referer" '
      #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
      #access_log  logs/access.log  main;
    
      sendfile        on;
      #tcp_nopush     on;
    
      #keepalive_timeout  0;
      keepalive_timeout  65;
    
      #gzip  on;
    
      server {
          listen       80;
          server_name  localhost;
    
          #charset koi8-r;
    
          #access_log  logs/host.access.log  main;
    
          location / {
             root   html;
             index  index.html index.htm;
          }
    
          #error_page  404              /404.html;
    
          # redirect server error pages to the static page /50x.html
          #
          error_page   500 502 503 504  /50x.html;
          location = /50x.html {
              root   html;
          }
    
          # proxy the PHP scripts to Apache listening on 127.0.0.1:80
          #
          #location ~ \.php$ {
          #    proxy_pass   http://127.0.0.1;
          #}
    
          # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
          #
          #location ~ \.php$ {
          #    root           html;
          #    fastcgi_pass   127.0.0.1:9000;
          #    fastcgi_index  index.php;
          #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
          #    include        fastcgi_params;
          #}
    
          # deny access to .htaccess files, if Apache's document root
          # concurs with nginx's one
          #
          #location ~ /\.ht {
          #    deny  all;
          #}
      }
​
      # another virtual host using mix of IP-, name-, and port-based configuration
      #
      #server {
      #    listen       8000;
      #    listen       somename:8080;
      #    server_name  somename  alias  another.alias;
    
      #    location / {
      #        root   html;
      #        index  index.html index.htm;
      #    }
      #}
​
      # HTTPS server
      #
      #server {
      #    listen       443 ssl;
      #    server_name  localhost;
    
      #    ssl_certificate      cert.pem;
      #    ssl_certificate_key  cert.key;
    
      #    ssl_session_cache    shared:SSL:1m;
      #    ssl_session_timeout  5m;
    
      #    ssl_ciphers  HIGH:!aNULL:!MD5;
      #    ssl_prefer_server_ciphers  on;
    
      #    location / {
      #        root   html;
      #        index  index.html index.htm;
      #    }
      #}
    
  }

·       步骤2:修改配置文件nginx.conf

增加或修改如下内容:


#增加反向代理tomcat
upstream test{server localhost:8080;}
server {
listen 80;
server_name localhost;
​
location / {
           # root   html;
           index  index.html index.htm;
           # 访问tomcat
           proxy_pass http://test;
        }
}

修改后的配置文件


  [root@localhost sbin]# vim ../conf/nginx.conf
  #增加反向代理tomcat
  upstream test{server localhost:8080;}  
  server {
        listen       80;
        server_name  localhost;
​
        #charset koi8-r;
​
        #access_log  logs/host.access.log  main;
​
        location / {
           # root   html;
           index  index.html index.htm;
           # 访问tomcat
           proxy_pass http://test;
        }
​
        #error_page  404              /404.html;
​
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
​
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
​
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
​
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
     # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
​
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
        # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
​
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
​
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
​
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
​
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
​
}

·       步骤3:重启nginx

#重启nginx(配置文件变动后需要重启才能生效):
./nginx
-s reload

·       步骤4:打开浏览器,远程浏览

 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值