Linux-Java服务配置

2021/12/7 7:16:51

本文主要是介绍Linux-Java服务配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Linux 起Java服务

MySQL配置

1、安装依赖 wget

#yum install -y libaio wget 

2、下载MySQL官网的仓库文件

# cd /tmp
# wget http://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
# yum localinstall mysql80-community-release-el7-1.noarch.rpm

3、MySQL安装

# yum install -y mysql-community-server

4、启动MySQL服务

# service mysql start  //启动

#service mysql stop  //关闭
# service mysql stop

5、新建用户 赋权限

# use mysql;        //mysql中以;结束
# select host,user from user;  //查看现有用户
# CREATE USER 'king'@'localhost' IDENTIFIED BY '123456';
#   // king为用户名,但是只为本主机可以操作,密码为123456
# select host,user from user;  //再次查看用户

//赋权限  localhost更改%
# GRANT ALL PRIVILEGES ON *.* TO 'king'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

//king表示用户名,%表示所有的电脑都可以连接,也可以设置某个ip地址运行连接,第二个123456表示密码、

6、修改配置文件

# vim /etc/my.conf  //或者
# vim /etc/mysql/mysql.conf.d/mysqld.cnf
# vim /etc/mysql/my.cnf

//进去将# bing-address = 127.0.0.1 注释掉或改成 0.0.0.0

7、MySQL修改用户密码

// 5.7版本下的mysql数据库下已经没有password 
# mysql> use mysql;
Database changed
mysql> select User from user;  #此处为查询用户命令
+-----------+
| User      |
+-----------+
| *******  |
| mysql.sys |
| root      |
+-----------+
3 rows in set (0.00 sec)

mysql> update user set password=password("*******") where user="*******";  #修改密码报错
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
# mysql> update mysql.user set authentication_string=password('123456') where user='king';  
//修改密码成功 用户king 密码123456
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

# mysql> flush privileges;  #立即生效
Query OK, 0 rows affected (0.00 sec)

# mysql> quit
Bye

# mysql -u ******* -p #以该用户登录成功.
Enter password: ********
…………………………
mysql>

Navicat远程登录测试,记得开放服务器3306端口和服务器防火墙

# firewall-cmd --list-ports
# firewall-cmd --zone=public --add-port=3306/tcp --permanent
# systemctl restart firewall.service
//防火墙配置

Redis配置

1、获取redis资源 下载

# wget http://download.redis.io/releases/redis-4.0.8.tar.gz  //获取资源

# tar xzvf redis-4.0.8.tar.gz  //解压缩

2、安装

# cd redis-4.0.8  //进入目录
# make            //编译
# cd src
# make install PREFIX=/usr/local/redis //再编译

3、移动配置文件到安装目录下

# cd ../
# mkdir /usr/local/redis
# mv redis.conf /usr/local/redis

4、配置redis为后台启动

# vim /usr/local/redis/redis.conf 
//将daemonize no 改成 daemonize yes

5、启动Redis服务

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

6、拷贝redis-cli,redis-server命令

# cp /usr/local/redis/bin/redis-server /usr/local/bin/

# cp /usr/local/redis/bin/redis-cli /usr/local/bin/

//将redis-cli,redis-server拷贝到bin下,让redis-cli指令可以在任意目录下直接使用

7、设置redis密码

进入redis的配置文件

vim /etc/local/redis/redis.conf

修改内容
# bind 0.0.0.0     //所有地址均可访问
# protected-mode no  //不受保护
# requirepass 123456  //密码

8、重启redis服务

# redis-cli -h 127.0.0.1 -p 6379 -a 123456

输入 redis-cli 进入命令模式,使用 auth '123456' 

9、防火墙配置 (还有端口开放)

# firewall-cmd --list-ports
# firewall-cmd --zone=public --add-port=6379/tcp --permanent  //(开放6379端口)
# systemctl restart firewall.service
//防火墙配置 

10、Redis常用命令

常用命令  
# redis-server /usr/local/redis/etc/redis.conf  //启动redis

# pkill redis        //停止redis

Nginx配置

1、下载安装Nginx解压

## 解压
tar -zxvf nginx-1.9.9.tar.gz

##进入nginx目录
cd nginx-1.9.9
## 配置
./configure --prefix=/usr/local/nginx
# make
make
make install

2、测试是否安装成功

# cd到刚才配置的安装目录 /usr/loca/nginx/
./sbin/nginx -t

错误信息

nginx: [alert] could not open error log file: open() "/usr/local/nginx/logs/error.log" failed (2: No such file or directory)
2016/09/13 19:08:56 [emerg] 6996#0: open() "/usr/local/nginx/logs/access.log" failed (2: No such file or directory)

原因分析

nginx/目录下没有logs文件夹

mkdir logs
chmod 700 logs

3、安装成功

cd /usr/local/nginx/sbin
./nginx   //启动nginx

4、修改Nginx配置文件

vim /usr/local/nginx/nginx.conf  

//http 部分内容需要更改
//root 目录需要指向前端文件夹
//默认80端口
worker_processes auto;

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"';


    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /opt/soft/nginx/conf/mime.types;
    default_type        application/octet-stream;

   
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  image_server;


        location / {
            root   /opt/project/nayun/dk-ui;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }

        location /prod-api/{
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:8080/;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

5、防火墙配置以及开放80端口

# firewall-cmd --list-ports
# firewall-cmd --zone=public --add-port=80/tcp --permanent  //(开放80端口)
# systemctl restart firewall.service
//防火墙配置 

application.yml配置文件

//连接mysql数据库配置

# 库数据源
 master:
 url: jdbc:mysql://123.56.120.32:3306/nayun?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
 username: root
 password: 123456
 
 //连接Redis库配置
 
  redis:
    # 地址
    host: 123.56.120.32
    # 端口,默认为6379
    port: 6379
    # 数据库索引
    database: 10
    # 密码
    password: 123456
    # 连接超时时间
    timeout: 10s

后台起java服务

nohup java SocketServer >nohup.out &
//nohup 和 & 是保证后台运行不终止的意思
//>nohup 是将日志重定向输出到nohup.out文件中



这篇关于Linux-Java服务配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程