前一段时间,得到一台linux服务器,我初始化该服务器部署Java,web,nginx,数据库。
虽然不是难度很多大的事情,但还是记录一下。并将我遇到的所有问题列出附加解决方式。
服务器初始化
网络配置
# 查看当前网络状态
ip addr
# 修改网络
vi /etc/sysconfig/network-scripts/ifcfg-eno3
service network restart
修改并新增以下内容(信息需要按实际修改)
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
# BOOTPROTO=dhcp
# 修改 将网络状态修改为静态网络
BOOTPROTO="static"
# 新增
IPADDR=192.168.220.138
GATEWAY=192.168.220.2
NETMASK=255.255.255.0
DNS1=192.168.220.2
DNS2=8.8.8.8
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="515d14b5-8262-44e4-8772-dcce1e3dc498"
DEVICE="ens33"
ONBOOT="yes
命令行界面与图形化界面切换
# 命令行
systemctl set-default multi-user.target
# 图形化
sudo systemctl set-default graphical.target
# 修改之后重启
查看系统版本信息
[root@localhost ~]# uname -a
Linux localhost.localdomain 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]#
[root@localhost ~]# cat /etc/centos-release()
CentOS Linux release 7.9.2009 (Core)
[root@localhost ~]#
[root@localhost ~]# rpm -q centos-release
centos-release-7-9.2009.0.el7.centos.x86_64
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 9.4M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/mapper/centos-root 50G 5.5G 45G 11% /
/dev/sda2 1014M 170M 845M 17% /boot
/dev/sda1 200M 12M 189M 6% /boot/efi
/dev/mapper/centos-home 1.8T 267M 1.8T 1% /home
tmpfs 1.6G 0 1.6G 0% /run/user/0
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# cat /proc/cpuinfo | grep name
model name : Intel(R) Xeon(R) E-2224 CPU @ 3.40GHz
model name : Intel(R) Xeon(R) E-2224 CPU @ 3.40GHz
model name : Intel(R) Xeon(R) E-2224 CPU @ 3.40GHz
model name : Intel(R) Xeon(R) E-2224 CPU @ 3.40GHz
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# cat /proc/meminfo | head -4
MemTotal: 16203316 kB
MemFree: 13868640 kB
MemAvailable: 14129668 kB
Buffers: 2256 kB
运行环境安装
java8安装
当运行jar包报错,可知,没有安装java环境
[root@localhost project]# java -jar serve.jar
-bash: java: command not found
yml安装
yum install java-1.8.0-openjdk* -y
验证如下即为成功
[root@localhost project]# java -version
openjdk version "1.8.0_372"
OpenJDK Runtime Environment (build 1.8.0_372-b07)
OpenJDK 64-Bit Server VM (build 25.372-b07, mixed mode)
MySQL5.7 数据库安装
卸载默认安装的mariadb:(有的话,否则安装失败)
yum search mysql
yum remove mariadb.x86_64
# 下载安装包
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpmrpm -ivh mysql57-community-release-el7-11.noarch.rpm
# 启动
service mysqld start
# 重启
service mysqld restart
# 关闭
service mysqld stop
# 查询默认登录密码
cat /var/log/mysqld.log | grep password
# 登录
mysql -uroot -p密码
# 修改密码
SET PASSWORD = PASSWORD(' xxxx ');
# 更新远程连接权限
update user set host="%" where Host='localhost' and user = "root";
# 刷新用户权限
flush privileges;
nginx安装
yam ram形式安装
# 下载nginx包
wget https://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 建立nginx的yum仓库
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 查看nginx信息
yum info nginx
# 查看yum源仓库中nginx版本
yum --showduplicates list nginx | expand
# 安装nginx,默认安装最新的稳定版本
yum install nginx
# 配置文件位置
/etc/nginx/nginx.conf
# 启动目录位置
/usr/sbin/nginx
nginx.conf配置文件(绝对简化)
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
server_tokens off;
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;
keepalive_timeout 65;
server {
listen 9811;
server_name visual;
location / {
root /home/project/vue_c/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
安装服务
Java
创建目录
/home/project/java_c/start.sh
启动,重启,停止
# nginx执行文件存放目录
cd /usr/sbin/
# 启动
./nginx
# 重启
./nginx -s reload
# 停止
./nginx -s stop
以上也可根据绝对路径执行
# 启动
/usr/sbin/nginx
# 重启
/usr/sbin/nginx -s reload
# 停止
/usr/sbin/nginx -s stop
nginx 配置文件位置
# 原配置文件位置
/etc/nginx/nginx.conf
软连接配置文件位置
/home/nginx/nginx.conf
Linux开启自动动脚本配置
在linux各项服务启动完毕之后,会运行/etc/rc.d/rc.local这个文件,所以把我们需要运行的脚本放在这里面就行了。
(ps:/etc/rc.local和/etc/rc.d/rc.local是同一个文件,软链接而已)
(ps:启动配置需要配合文件执行权限使用,详见涉及问题)
现已经配置:
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
# 配置启动nginx
/usr/sbin/nginx
# 配置启动java服务
/home/project/java_c/start.sh
涉及问题
修改mysql5.7默认端口启动失败
修改端口类型
sudo semanage port -a -t mysqld_port_t -p tcp 13306
错误日志如下
--
-- The result is failed.
5月 30 14:28:59 localhost.localdomain systemd[1]: Unit mysqld.service entered failed state.
5月 30 14:28:59 localhost.localdomain systemd[1]: mysqld.service failed.
5月 30 14:28:59 localhost.localdomain setroubleshoot[5480]: SELinux is preventing /usr/sbin/mysqld from name_bind access on the tcp_socket port 11536. For complete SELinux messages run: sealert -l 69e1b461
5月 30 14:28:59 localhost.localdomain python[5480]: SELinux is preventing /usr/sbin/mysqld from name_bind access on the tcp_socket port 11536.
***** Plugin bind_ports (92.2 confidence) suggests ************************
If you want to allow /usr/sbin/mysqld to bind to network port 11536
Then you need to modify the port type.
Do
# semanage port -a -t PORT_TYPE -p tcp 11536
where PORT_TYPE is one of the following: mysqld_port_t, tram_port_t.
***** Plugin catchall_boolean (7.83 confidence) suggests ******************
If you want to allow nis to enabled
Then you must tell SELinux about this by enabling the 'nis_enabled' boolean.
Do
setsebool -P nis_enabled 1
***** Plugin catchall (1.41 confidence) suggests **************************
If you believe that mysqld should be allowed name_bind access on the port 11536 tcp_socket by default.
Then you should report this as a bug.
You can generate a local policy module to allow this access.
Do
allow this access for now by executing:
# ausearch -c 'mysqld' --raw | audit2allow -M my-mysqld
# semodule -i my-mysqld.pp
5月 30 14:28:59 localhost.localdomain systemd[1]: mysqld.service holdoff time over, scheduling restart.
5月 30 14:28:59 localhost.localdomain systemd[1]: Stopped MySQL Server.
-- Subject: Unit mysqld.service has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mysqld.service has finished shutting down.
5月 30 14:28:59 localhost.localdomain systemd[1]: Starting MySQL Server...
-- Subject: Unit mysqld.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit mysqld.service has begun starting up.
mysql5.7 REVOKE ALL PRIVILEGES ON报错
提示信息
Your password does not satisfy the current policy requirements
原因
通过 SELECT user, host FROM mysql.user; 查询出 'xyadmin'@'localhost';
通过 SHOW GRANTS FOR 'xyadmin'; 查询出 GRANT ALL PRIVILEGES ON `xyadmin`.* TO 'xyadmin'@'%'
两边不一致导致报错提示
修改,使得两边信息一致
UPDATE mysql.user SET host = '%' WHERE user = 'xyadmin';
执行一下语句撤销成功
REVOKE ALL PRIVILEGES ON xyadmin.* FROM 'xyadmin'@'%';
启动命令失效-rc.local文件中头部
在**/etc/rc.d/rc.local**文件中头部的有这如下内容,他表示该脚本将使用 Bash 解释器来执行
#!/bin/bash
错误的使用成如下内容导致开启启动脚本失败
#bin/bash
情景描述
使用命令查看启动运行日志
sudo journalctl -u rc-local.service
得到如下结果
[root@localhost log]# sudo journalctl -u rc-local.service
-- Logs begin at 二 2023-05-30 10:16:14 CST, end at 二 2023-05-30 10:22:58 CST. --
5月 30 10:16:30 localhost.localdomain systemd[1]: Starting /etc/rc.d/rc.local Compatibility...
5月 30 10:16:30 localhost.localdomain systemd[1]: rc-local.service: control process exited, code=exited status=203
5月 30 10:16:30 localhost.localdomain systemd[1]: Failed to start /etc/rc.d/rc.local Compatibility.
5月 30 10:16:30 localhost.localdomain systemd[1]: Unit rc-local.service entered failed state.
5月 30 10:16:30 localhost.localdomain systemd[1]: rc-local.service failed.
启动命令失效-执行文件权限未设置
该部分需要满足:
-
/etc/rc.d/rc.local 文件有执行权限
-
脚本文件有执行权限例如:user/start.sh
查看是否有执行权限
查看文件是否有执行命令
ls -l 文件,如下
ls -l /usr/sbin/nginx
有执行权限
[root@localhost nginx]# ls -l /usr/sbin/nginx
-rwx--x--x. 1 root root 1399232 4月 12 01:22 /usr/sbin/nginx
增加文件的可执行权限
增加执行权限:chmod +x 文件
chmod +x /mnt/Autorun_script.sh
chmod +x /etc/rc.d/rc.local
# 查看文件执行权限
ls -l /etc/rc.d/rc.local
启动命令只有nginx无法启动
(132条消息) rc.local配置nginx开机自启动遇到的一些问题_rc.local nginx无法启动_信椿哥的博客-CSDN博客
报上错误的原因有两个种情况:
-
端口号小于1024(未验证):
运行的账号不是root就会报这个错误,需要切换到root,即开机启动项目配置改为:
touch /var/lock/subsys/local sudo /usr/sbin/nginx
-
端口号大于1024(关了selinux实测符合该情况):
检查一下selinux是否开启了。如果开启了关闭selinux试试,如果关了selinux能正常说明端口可能与selinux的端口冲突了或http_port_t中没有开放对应的端口,检查http_port_t:
[root@localhost ~]# semanage port -l | grep http_port_t
http_port_t tcp 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
nginx配置里面的9080段端口确实没有开放,增加端口:
semanage port -a -t http_port_t -p tcp 9080
如果添加端口报如下错误:
ValueError: 已定义端口 tcp/9080
但是semanage port -l | grep http_port_t 检查的却看不到该端口存在,应该就是另一个服务具有TCP端口9080的已定义状态;
此时就需要使用修改端口命令:
semanage port -m -t http_port_t -p tcp 8080
如果没有semanage命令,安装:https://blog.csdn.net/runsnail2018/article/details/81185653
再次检查端口:
[root@localhost ~]# semanage port -l | grep http_port_t
http_port_t tcp 9080 80, 81, 443, 488, 8008, 8009, 8443, 9000
pegasus_http_port_t tcp 5988
重启系统,nginx自启动成功。
非登录情况下进行页面调用nginx不跳转且显示404
增加
try_files $uri $uri/ /index.html;
以及
location = /50x.html {
root html;
}
总体如下,完整见nginx.conf(绝对简化)
location / {
root /home/project/communication/vue_c/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
失败的软件包是…密钥配置为…
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
导入 GPG key 0x5072E1F5:
用户ID : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
指纹 : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
软件包 : mysql57-community-release-el7-11.noarch (installed)
来自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
是否继续?[y/N]:y
mysql-community-common-5.7.42-1.el7.x86_64.rpm 的公钥尚未安装
失败的软件包是:mysql-community-common-5.7.42-1.el7.x86_64
GPG 密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
输入
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
附录说明
文件执行权限
有执行权限
当一个文件具有执行权限时,权限标记中的相应位置会显示为 x
。以下是不同权限组合的示例:
-
仅所有者具有执行权限:
-rwx------
这表示文件的所有者具有读取、写入和执行权限,而其他用户没有任何权限。
-
所有者和所属组具有执行权限:
-rwxr-x---
这表示文件的所有者具有读取、写入和执行权限,所属组成员具有读取和执行权限,其他用户没有任何权限。
-
所有者、所属组和其他用户都具有执行权限:
-rwxrwxrwx
这表示文件的所有者、所属组成员和其他用户都具有读取、写入和执行权限。
请注意,上述示例仅表示权限标记,实际上是否可以执行文件还受到目录权限的限制。用户需要在拥有执行文件的目录中具有执行权限,才能进入该目录并执行文件。
没有执行权限
当一个文件没有执行权限时,权限标记中的相应位置会显示为 -
,而不是 x
。以下是不同权限组合的示例:
-
仅所有者没有执行权限:
-rw-r--r--
这表示文件的所有者具有读取和写入权限,但没有执行权限。所属组成员和其他用户具有读取权限。
-
所有者和所属组没有执行权限:
-rw-rw----
这表示文件的所有者和所属组成员都具有读取和写入权限,但没有执行权限。其他用户没有任何权限。
-
所有者、所属组和其他用户都没有执行权限:
-rw-rw-rw-
这表示文件的所有者、所属组成员和其他用户都具有读取和写入权限,但没有执行权限。
请注意,即使文件没有执行权限,用户仍然可以查看和编辑文件内容,但不能直接执行它作为可执行程序。