基于centOS7.5 x64/openjdk1.8/tomcat8.5/nginx1.16/nodejs/mysql8.0/redis3/git的服务器环境配置)

基于centOS7.5 64/openjdk1.8/tomcat/nginx/nodejs/mysql5.7/git的服务器环境配置)

我以前的三年和未来的三年从事网站开发,主要手段是JAVA,python,nodejs,前端大套间,偶尔写写服务器脚本,做做自动化部署。记录这篇博客的目的在于记录服务器部署过程,很多次都要重新部署服务器,什么安装openjdk然后配https,做过就忘了,下次又重新开始,很烦恼。记下来吧,要配服务器的时候直接过来拔就是了*

第一步、更新yum仓库

//新安装的服务器更新一下yum的版本,这一步不是强制要求,详细参考update与upgrade的用法和作用
ruan$ yum -y update

第二步、 先安装简单的东西

2.1 git

git官方网址:https://git-scm.com/download/linux, git官方的download页面推荐用法是直接使用yum安装git,因为版本对git的影响实则不大,一般来说,不需要下载源码自己编译或者去找新版本的仓库。

ruan$ yum install -y git
// 总下载量:4.5 M
// 安装大小:22 M

2.2 nginx

  • 安装nginx,nginx的中文网站有关于自己编译源码安装的手段:
    http://www.nginx.cn/install
    不过源码编译很麻烦,在centOS中采用yum安装就可以了,对于作为普通的服务器来说,各个版本的nginx差异不大,使用yum安装默认版本nginx:
//总下载量:4.2 M
//安装大小:14 M
ruan$ yum -y install nginx
  • 查看一下安装的版本:
ruan$ yum list nginx
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
已安装的软件包
nginx.x86_64                        1:1.16.1-1.el7                         @epel
  • 启动nginx:
ruan$ systemctl start nginx.service
  • 查看nginx服务的状态(以下是正常启动的状态):
[root@VM_7_129_centos ~]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 三 2019-12-18 13:42:14 CST; 1min 1s ago
  Process: 29192 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 29190 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 29188 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 29194 (nginx)
   CGroup: /system.slice/nginx.service
           ├─29194 nginx: master process /usr/sbin/nginx
           └─29195 nginx: worker process

12月 18 13:42:14 VM_7_129_centos systemd[1]: Starting The nginx HTTP and re....
12月 18 13:42:14 VM_7_129_centos nginx[29190]: nginx: the configuration fil...k
12月 18 13:42:14 VM_7_129_centos nginx[29190]: nginx: configuration file /e...l
12月 18 13:42:14 VM_7_129_centos systemd[1]: Failed to parse PID from file ...t
12月 18 13:42:14 VM_7_129_centos systemd[1]: Started The nginx HTTP and rev....
Hint: Some lines were ellipsized, use -l to show in full.
  • 关闭nginx
ruan$ systemctl stop nginx.service
  • 设置开机自动启动服务
ruan$ systemctl enable nginx.service
  • 简单配置
    • 首先切换到== /etc/nginx ==下,这是nginx配置的根目录,以后配置https,pem等文件就要丢在这个位置。:
      conf.d                  koi-utf             scgi_params
      default.d               koi-win             scgi_params.default
      fastcgi.conf            mime.types          uwsgi_params
      fastcgi.conf.default    mime.types.default  uwsgi_params.default
      fastcgi_params          nginx.conf          win-utf
      fastcgi_params.default  nginx.conf.default
      // 当前目录下有一个nginx.conf文件,这是nginx配置的根配置文件,其中有一句:
      // include /etc/nginx/conf.d/*.conf;
      // 这一句说明conf.d目录下的**.conf都会被加载成为配置文件,所以我们的配置文件自己书写,然后保存
      // 在conf.d下面就行了
      
      其中nginx.conf:
      # For more information on configuration, see:
      #   * Official English Documentation: http://nginx.org/en/docs/
      #   * Official Russian Documentation: http://nginx.org/ru/docs/
      
      user nginx;
      worker_processes auto;
      error_log /var/log/nginx/error.log;
      pid /run/nginx.pid;
      
      # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
      include /usr/share/nginx/modules/*.conf;
      
      events {
          worker_connections 1024;
      }
      
      http {
          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  /var/log/nginx/access.log  main;
      
          sendfile            on;
          tcp_nopush          on;
          tcp_nodelay         on;
          keepalive_timeout   65;
          types_hash_max_size 2048;
      
          include             /etc/nginx/mime.types;
          default_type        application/octet-stream;
      
          # Load modular configuration files from the /etc/nginx/conf.d directory.
          # See http://nginx.org/en/docs/ngx_core_module.html#include
          # for more information.
          include /etc/nginx/conf.d/*.conf;
      
          server {
              listen       80 default_server;
              listen       [::]:80 default_server;
              server_name  _;
              root         /usr/share/nginx/html;
      
              # Load configuration files for the default server block.
              include /etc/nginx/default.d/*.conf;
              location / {
              }
      
              error_page 404 /404.html;
                  location = /40x.html {
              }
      
              error_page 500 502 503 504 /50x.html;
                  location = /50x.html {
              }
          }
      
      # Settings for a TLS enabled server.
      #
      #    server {
      #        listen       443 ssl http2 default_server;
      #        listen       [::]:443 ssl http2 default_server;
      #        server_name  _;
      #        root         /usr/share/nginx/html;
      #
      #        ssl_certificate "/etc/pki/nginx/server.crt";
      #        ssl_certificate_key "/etc/pki/nginx/private/server.key";
      #        ssl_session_cache shared:SSL:1m;
      #        ssl_session_timeout  10m;
      #        ssl_ciphers HIGH:!aNULL:!MD5;
      #        ssl_prefer_server_ciphers on;
      #
      #        # Load configuration files for the default server block.
      #        include /etc/nginx/default.d/*.conf;
      #
      #        location / {
      #        }
      #
      #        error_page 404 /404.html;
      #            location = /40x.html {
      #        }
      #
      #        error_page 500 502 503 504 /50x.html;
      #            location = /50x.html {
      #        }
      #    }
      
      }
      
    • 切换到conf.d目录,新建my.conf,文件名随意
      因为上面片段的include与server节点是同级的,所以我们在自己的配置文件中直接写server节点配置就可以了。如果默认的话,其实可以不用写配置文件,直接80端口就行了,修改其中server节点下的root指向的位置即可。默认指向:root /usr/share/nginx/html;
    • 配置https(详情我是参照的腾讯云,请你们自己参考别的)
      在你的my.conf中增加如下代码:
      server {
           #SSL 访问端口号为 443
           listen 443; 
           #填写绑定证书的域名
           server_name www.domain.com; 
           #启用 SSL 功能
           ssl on;
           #证书文件名称
           ssl_certificate 1_www.domain.com_bundle.crt; 
           #私钥文件名称
           ssl_certificate_key 2_www.domain.com.key; 
           ssl_session_timeout 5m;
           #请按照以下协议配置
           ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
           #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
           ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
           ssl_prefer_server_ciphers on;
           location / {
              #网站主页路径。此路径仅供参考,具体请您按照实际目录操作。
               root /var/www/www.domain.com; 
               index  index.html index.htm;
           }
       }
      
      其中server_name填写域名,比如www.ruankun.xyz
      ssl_certificate 1_www.domain.com_bundle.crt;填写你的文件名
      ssl_certificate_key 2_www.domain.com.key; 一样填写你的文件名
      location节点看情况部署
    • 测试配置文件是否正确
      ruan$ nginx -t
      //正确的输出
      nginx: [warn] the "ssl" directive is deprecated, use the "listen ... ssl" directive instead in /etc/nginx/conf.d/my.conf:7
      nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
      nginx: configuration file /etc/nginx/nginx.conf test is successful
      
    • 配置http跳转https
      http跳转https的方案有很多种:
      1. 页面中添加 JS 脚本
      2. 后端程序中添加重定向
      3. 通过 Web 服务器实现跳转
      4. rewrite 功能
      在这个地方,我们通过rewrite来实现http跳转https.在my.conf中继续加入如下代码:
      注意更改其中的server_name节点。
server {
    listen 80;
    #填写绑定证书的域名
    server_name www.ruankun.xyz;
    #把http的域名请求转成https
    rewrite ^(.*)$ https://$host$1 permanent;
}

第三步、安装MySQL8.0

3.1 更新rpm包

安装mysql8.0, 默认的yum里面就是mariaDB, 不晓得用不用得,所以要重新下载rpm, 官方rpm下载地址:
https://dev.mysql.com/downloads/repo/yum/
redhat和centOS源出同门,选red hat版本的就行了。
rpm下载地址(red hat 7):https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm

  • 下载rpm包:
ruan$ wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
  • 安装rpm包到yum仓库(-ivh: i install安装, v verbose打印全日志, h hash安装进度)
ruan$ rpm -ivh mysql80-community-release-el7-3.noarch.rpm
警告:mysql80-community-release-el7-3.noarch.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
	准备中...                          ################################# [100%]
	正在升级/安装...
	   1:mysql80-community-release-el7-3  ################################# [100%]
  • 查看是否可以安装:
ruan$ yum repolist all | grep mysql

输出如下:

mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community      禁用
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - So 禁用
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community      禁用
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - So 禁用
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community      禁用
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - So 禁用
mysql-connectors-community/x86_64  MySQL Connectors Community       启用:    131
mysql-connectors-community-source  MySQL Connectors Community - Sou 禁用
mysql-tools-community/x86_64       MySQL Tools Community            启用:    100
mysql-tools-community-source       MySQL Tools Community - Source   禁用
mysql-tools-preview/x86_64         MySQL Tools Preview              禁用
mysql-tools-preview-source         MySQL Tools Preview - Source     禁用
mysql55-community/x86_64           MySQL 5.5 Community Server       禁用
mysql55-community-source           MySQL 5.5 Community Server - Sou 禁用
mysql56-community/x86_64           MySQL 5.6 Community Server       禁用
mysql56-community-source           MySQL 5.6 Community Server - Sou 禁用
mysql57-community/x86_64           MySQL 5.7 Community Server       禁用
mysql57-community-source           MySQL 5.7 Community Server - Sou 禁用
mysql80-community/x86_64           MySQL 8.0 Community Server       启用:    145
mysql80-community-source           MySQL 8.0 Community Server - Sou 禁用

看到 mysql80-community/x86_64 MySQL 8.0 Community Server 启用: 145, 可以开始安装了

3.2 安装mysql:

  • 安装
ruan$ yum install -y mysql-community-server
安装  3 软件包 (+3 依赖软件包)

总下载量:473 M

这样就会自动开始安装mysql了。

  • 启动服务
ruan$ systemctl start mysqld.service
  • 查看服务状态以及开机启动
ruan$ systemctl status mysqld.service
ruan$ systemctl stop mysqld.service
ruan$ systemctl enable mysqld.service
  • 初次连接mysql
    第一次安装的mysql密码在/var/log/mysqld.log中:
ruan$ cat mysqld.log
2019-12-18T10:29:01.406509Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.18) initializing of server in progress as process 6124
2019-12-18T10:29:07.688866Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: HowQel2iu3+a
2019-12-18T10:29:13.492460Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.18) starting as process 6227
2019-12-18T10:29:14.866511Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2019-12-18T10:29:14.915740Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.18'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server - GPL.

其中密码是: HowQel2iu3+a
连接mysql并修改密码,修改密码的语句:

ruan$ ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '1234';

紧接着需要设置root的远程访问:

mysql>  use mysql
mysql> update user set host='%' where user='root';
mysql> flush privileges;
  • 卸载mySQL

为什么要卸载?原因很多,例如官方声明:mysql8不支持在my.conf中设置lower_case_table_names变量,只能在初始化时指定该变量,如果你没有,那么默认你的mysql就不会忽略大小写敏感问题,导致你大小写出现很多问题。
卸载mysql:

ruan$ systemctl stop mysqld.service
ruan$ rpm -qa|grep -i mysql
//找到mysql的包后,挨着卸载,如果出现依赖错误就先卸载依赖的。
ruan$ rpm -ev mysql-community-client-8.0.11-1.el7.x86_64 --nodeps
...

删除相关目录:

ruan$ find / -name mysql
/var/lib/mysql/mysql
/etc/selinux/targeted/active/modules/100/mysql
/root/.gradle/caches/modules-2/files-2.1/mysql
/root/.gradle/caches/modules-2/metadata-2.82/descriptors/mysql
ruan$ rm -rf /var/lib/mysql/
ruan$ rm -rf /var/lib/mysql/mysql
ruan$ rm -rf /etc/selinux/targeted/active/modules/100/mysql

卸载到此完成。

第四步、安装openjdk1.8

yum默认安装的java只有jre,没有jdk,所以安装时需要小心。

4.1找open jdk 1.8

ruan$ yum search java-1.8.0|grep openjdk

as is shown:

java-1.8.0-openjdk.i686 : OpenJDK Runtime Environment 8
java-1.8.0-openjdk.x86_64 : OpenJDK Runtime Environment 8
java-1.8.0-openjdk-accessibility.i686 : OpenJDK accessibility connector
java-1.8.0-openjdk-accessibility.x86_64 : OpenJDK accessibility connector
java-1.8.0-openjdk-accessibility-debug.i686 : OpenJDK 8 accessibility connector
java-1.8.0-openjdk-accessibility-debug.x86_64 : OpenJDK 8 accessibility
java-1.8.0-openjdk-debug.i686 : OpenJDK Runtime Environment 8 with full debug on
java-1.8.0-openjdk-debug.x86_64 : OpenJDK Runtime Environment 8 with full debug
java-1.8.0-openjdk-demo.i686 : OpenJDK Demos 8
java-1.8.0-openjdk-demo.x86_64 : OpenJDK Demos 8
java-1.8.0-openjdk-demo-debug.i686 : OpenJDK Demos 8 with full debug on
java-1.8.0-openjdk-demo-debug.x86_64 : OpenJDK Demos 8 with full debug on
java-1.8.0-openjdk-devel.i686 : OpenJDK Development Environment 8
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment 8
java-1.8.0-openjdk-devel-debug.i686 : OpenJDK Development Environment 8 with
java-1.8.0-openjdk-devel-debug.x86_64 : OpenJDK Development Environment 8 with
java-1.8.0-openjdk-headless.i686 : OpenJDK Headless Runtime Environment 8
java-1.8.0-openjdk-headless.x86_64 : OpenJDK Headless Runtime Environment 8
java-1.8.0-openjdk-headless-debug.i686 : OpenJDK Runtime Environment with full
java-1.8.0-openjdk-headless-debug.x86_64 : OpenJDK Runtime Environment with full
java-1.8.0-openjdk-javadoc.noarch : OpenJDK 8 API documentation
java-1.8.0-openjdk-javadoc-debug.noarch : OpenJDK 8 API documentation for
java-1.8.0-openjdk-javadoc-zip.noarch : OpenJDK 8 API documentation compressed
java-1.8.0-openjdk-javadoc-zip-debug.noarch : OpenJDK 8 API documentation
java-1.8.0-openjdk-src.i686 : OpenJDK Source Bundle 8
java-1.8.0-openjdk-src.x86_64 : OpenJDK Source Bundle 8
java-1.8.0-openjdk-src-debug.i686 : OpenJDK Source Bundle 8 for packages with
java-1.8.0-openjdk-src-debug.x86_64 : OpenJDK Source Bundle 8 for packages with

其中,java-1.8.0-openjdk-devel.i686这个是OpenJDK Development Environment 8
我们就是要安装这个:

ruan$ yum install -y java-1.8.0-openjdk-devel
...
完毕!
  • 验证openJDK安装完毕
ruan$ java
...
ruan$ javac
...
ruan$ java -version
openjdk version "1.8.0_232"
OpenJDK Runtime Environment (build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)

第五步、an装redis

redis官网上有安装教程:https://redis.io/download

5.1 搜索yum中的redis并安装

yum仓库中的redis是3.x的版本,现在已经更新到5.x了,但我觉得3.x就够用了,好装稳定才是硬道理。

ruan$ yum list redis
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
可安装的软件包
redis.x86_64                          3.2.12-2.el7                          epel
ruan$ yum install -y redis
总下载量:648 k
安装大小:1.7 M

这redis也太小了,真的厉害。

5.2 启动redis、查看状态并设置开机启动

ruan$ systemctl start redis.service
ruan$ systemctl status redis.service
● redis.service - Redis persistent key-value database
   Loaded: loaded (/usr/lib/systemd/system/redis.service; disabled; vendor preset: disabled)
  Drop-In: /etc/systemd/system/redis.service.d
           └─limit.conf
   Active: active (running) since 三 2019-12-18 23:46:17 CST; 6s ago
 Main PID: 22121 (redis-server)
   CGroup: /system.slice/redis.service
           └─22121 /usr/bin/redis-server 127.0.0.1:6379

12月 18 23:46:17 VM_7_129_centos systemd[1]: Starting Redis persistent key-....
12月 18 23:46:17 VM_7_129_centos systemd[1]: Started Redis persistent key-v....
Hint: Some lines were ellipsized, use -l to show in full.
ruan$ systemctl enable redis.service

5.3 连接redis并设置登录密码

ruan$ redis-cli
127.0.0.1:6379>
127.0.0.1:6379>config set requirepass [your password]

六、 安装gradle

一般spring项目会采用maven或者gradle打包,我们常用gradle,所以需要安装gradle,gradle直接去网上下载就是了。

6.1 下载gradle并解压

ruan$ wget https://downloads.gradle-dn.com/distributions/gradle-6.0.1-bin.zip
ruan$ cp gradle-6.0.1-bin.zip /usr/local/gradle-6.0.1-bin.zip
ruan$ cd /usr/local
ruan local$  unzip gradle-6.0.1-bin.zip
...

6.2 配置环境变量

ruan$ vim /etc/profile

输入以下内容并保存

export GRADLE_HOME=/usr/local/gradle-6.0.1
export GRADLE_BASE=/usr/local/gradle-6.0.1
export PATH=$PATH:$GRADLE_HOME/bin

使环境变量配置生效

ruan$ source /etc/profile

测试gradle是否安装正确:

ruan$ gradle --version
Welcome to Gradle 6.0.1!

Here are the highlights of this release:
 - Substantial improvements in dependency management, including
   - Publishing Gradle Module Metadata in addition to pom.xml
   - Advanced control of transitive versions
   - Support for optional features and dependencies
   - Rules to tweak published metadata
 - Support for Java 13
 - Faster incremental Java and Groovy compilation
 - New Zinc compiler for Scala
 - VS2019 support
 - Support for Gradle Enterprise plugin 3.0

For more details see https://docs.gradle.org/6.0.1/release-notes.html


------------------------------------------------------------
Gradle 6.0.1
------------------------------------------------------------

Build time:   2019-11-18 20:25:01 UTC
Revision:     fad121066a68c4701acd362daf4287a7c309a0f5

Kotlin:       1.3.50
Groovy:       2.5.8
Ant:          Apache Ant(TM) version 1.10.7 compiled on September 1 2019
JVM:          1.8.0_232 (Oracle Corporation 25.232-b09)
OS:           Linux 3.10.0-862.el7.x86_64 amd64

七、安装tomcat

使用某些垃圾软件,比如webbuilder(特别讨厌这玩意儿,但公司买了这软件, 必须要用),所以需要tomcat环境的支撑。安装tomcat8.5.

7.1 获取tomcat并保存到local

ruan$ wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.50/bin/apache-tomcat-8.5.50.tar.gz
ruan$ cp apache-tomcat-8.5.50.tar.gz /usr/local/
ruan$ cd /usr/local
ruan local$ tar -xvzf apache-tomcat-8.5.50.tar.gz

7.2 把tomcat配置到环境变量

不想折腾tomcat加到服务了,所以直接把bin配置到环境变量算了,直接./startup.sh就可以启动tomcat了

ruan$ vim /etc/profile

在后面接着加上

export CATALINA_HOME=/usr/local/apache-tomcat-8.5.50
export CATALINA_BASE=/usr/local/apache-tomcat-8.5.50
export PATH=$PATH:$CATALINA_HOME/bin

退出保存,使环境变量生效:

source /etc/profile

7.3 启动tomcat(配置端口过于简单,略)

ruan$ ./startup.sh
Using CATALINA_BASE:   /usr/local/apache-tomcat-8.5.50
Using CATALINA_HOME:   /usr/local/apache-tomcat-8.5.50
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.5.50/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/apache-tomcat-8.5.50/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.50/bin/tomcat-juli.jar
Tomcat started.

八、安装nodejs环境

因为前端采用npm构建,所以不安装nodejs环境实在是受不了,安装nodejs环境。

8.1 下载并安装node环境

nodejs中文网站:http://nodejs.cn/download/,官网正好抽风,不好意思。

ruan$ wget https://cdn.npm.taobao.org/dist/node/v12.14.0/node-v12.14.0-linux-x64.tar.xz

因为是xz结尾的压缩文件,解压缩:

ruan$ mv node-v12.14.0-linux-x64.tar.xz /usr/local/
ruan$ xz -d node-v12.14.0-linux-x64.tar.xz
ruan$ tar -xf node-v12.14.0-linux-x64.tar

解压后配置基础环境变量:

vim /etc/profile

加上这句话

export PATH=$PATH:/usr/local/node-v12/bin

使环境变量生效

source /etc/profile

8.2 测试node是否安装正确

ruan$ node -v
v12.14.0
ruan$ npm -v
6.13.4

8.3 深入配置(非必须)

npm安装包使用命令e.g: npm install -g express, 如果不希望安装在默认目录,则需要配置npm包的安装路径, 通常配置在node里面。

  • 新建文件夹(注意你的node目录名字跟我的不一样,不要全抄我的下面命令)
ruan$ cd /usr/local/node-v12
ruan$ mkdir node_global
ruan$ mkdir node_cache
  • 给npm添加配置项:
ruan$ npm config set prefix="/usr/local/node-v12/node_global"
ruan$ npm config set cache="/usr/local/node-v12/node_cache"
  • 配置最后的环境变量
ruan$ vim /etc/profile

输入如下信息(不太理解这个配置的是否完全正确,但可以用):

export PATH=$PATH:/usr/local/node-v12/bin
export NODE_PATH=/usr/local/node-v12/node_global/node_modules
export PATH=$PATH:/usr/local/node-v12/node_global

使环境变量生效:

ruan$ source /etc/profile

安装express测试一下:

ruan$ npm install -g express
+ [email protected]
added 50 packages from 37 contributors in 32.143s

在以下目录找到express:

/usr/local/node-v12/node_global/lib/node_modules/express

使用一下:

ruan$ touch test.js
ruan$ vim test.js

键入如下内容:

require('express')
console.log('hello world');

保存退出,运行一下:

ruan$ node test.js
internal/modules/cjs/loader.js:800
    throw err;
    ^

Error: Cannot find module 'express'
Require stack:
- /root/testNode2/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:797:15)
    at Function.Module._load (internal/modules/cjs/loader.js:690:27)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object. (/root/testNode2/index.js:1:1)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/root/testNode2/index.js' ]
}

全局的是不能用了,环境变量没有配置好,不过我们都这样用,不存在问题:

ruan$ cd wxhouse
ruan$ npm init
ruan$ npm install express --save
ruan$ vim index.js

输入:

require('express')
console.log('hello world');

运行:

ruan$ node index.js
hello world

九、总结

总算写完了,以后配置服务器就来这儿扒,还有一部分没写完,在下一步中介绍怎么:

  • 如何配置docker容器并将端口映射到外层,然后使用nginx访问不同的docker,实现负载均衡。
  • 如何配置jenkins自动化部署,以后代码推送到git上再也不用手动build run了,妈妈再也不用担心改一百次BUG就要打开服务器一百次然后手动java -jar xxx.jar了,还要手动杀进程,写配置文件,run,重启容器,麻不麻烦啊?

你可能感兴趣的:(基于centOS7.5 x64/openjdk1.8/tomcat8.5/nginx1.16/nodejs/mysql8.0/redis3/git的服务器环境配置))