linux服务器搭建

时间:2024-05-02 23:37:14

centos7 java web项目环境搭配

2018年07月19日 17:20:21

阅读数:25

首先进行系统安装,此处不进行详细介绍,自行百度安装

一、配置ip地址信息

1、进入/etc/sysconfig/network-scripts/

2ls查看文件夹内文件

3、编写 ifcfg-ens****文件(本机为ifcfg-ens33

  1. cd /etc/sysconfig/network-scripts/
  2. ls
  3. vi ifcfg-ens33

4、配置ip信息(按i进入输入模式,按esc退出输入,输入:wq 进行保存退出,其他请自行百度学习vi操作)

  1. TYPE=Ethernet
  2. PROXY_METHOD=none
  3. BROWSER_ONLY=no
  4. BOOTPROTO=static    #静态ip
  5. DEFROUTE=yes
  6. IPV4_FAILURE_FATAL=no
  7. IPV6INIT=yes
  8. IPV6_AUTOCONF=yes

10. IPV6_DEFROUTE=yes

11. IPV6_FAILURE_FATAL=no

12. IPV6_ADDR_GEN_MODE=stable-privacy

13. NAME=ens33

14. UUID=3ae4be2f-7000-4147-8ac2-c5a4fa848f31

15. DEVICE=ens33

16. ONBOOT=yes

17. IPADDR=192.168.1.100   #ip地址

18. NETMASK=255.255.255.0  #子网掩码

19. GATEWAY=192.168.1.1    #网关

20. DNS1=202.99.160.68     #dns

21. DNS2=202.99.166.4

22. ZONE=public

5、重启网络,并进行测试 ping(按ctrl+c

  1. service network restart
  2. ping www.baidu.com

详细页面

二、开启防火墙

1、查看防火墙是否打开

firewall-cmd --state

2、打开防火墙

systemctl start firewalld

3、设置防火墙开机启动

systemctl enable firewalld

4、添加端口并重启防火墙(开放常用端口,如:2280330680804436379等)

  1. firewall-cmd --zone=public --add-port=3306/tcp --permanent
  2. firewall-cmd --reload

5、查看开放端口状况

firewall-cmd --list-all

三、安装SSH服务(远程连接使用)

1、查看是否安装了相关软件

rpm -qa|grep -E "openssh"

2、安装缺失的软件:

sudo yum install openssh*

3、注册使用服务:

  1. sudo systemctl enable sshd
  2. service sshd start

4、开启防火墙的22端口(如果已开启请跳过此部)

  1. firewall-cmd --zone=public --add-port=22/tcp --permanent
  2. firewall-cmd --reload

5、使用ssh连接服务器(如软件xshellputty

四、更新阿里云yum源(建议更改,加快速度)

1、安装wget服务

yum -y install wget

2、下载repo文件 

wget http://mirrors.aliyun.com/repo/Centos-7.repo

3、备份并替换系统的repo文件 

  1. cp Centos-7.repo /etc/yum.repos.d/
  2. rm -rf Centos-7.repo
  3. cd /etc/yum.repos.d/
  4. mv CentOS-Base.repo CentOS-Base.repo.bak
  5. mv Centos-7.repo CentOS-Base.repo

4、执行yum源更新命令(执行最后一步假如报错正在尝试其他映像,请按ctrl+c,然后再次执行最后一步)

  1. yum clean all
  2. yum makecache
  3. yum update

5、安装vim

yum -y install vim*

五、jdk安装

1、进入/usr/local目录下载

  1. cd /usr/local/
  2. wget http://download.oracle.com/otn-pub/java/jdk/8u181-b13/96a7b8442fe848ef90c96a2fad6ed6d1/jdk-8u181-linux-x64.tar.gz?AuthParam=1531973178_bfda57895bf56fc4e64f754387fe008d

2、解压jdk并删除源文件

  1. tar  -zxvf jdk-8u181-linux-x64.tar.gz\?AuthParam\=1531973178_bfda57895bf56fc4e64f754387fe008d
  2. rm -rf jdk-8u181-linux-x64.tar.gz\?AuthParam\=1531973178_bfda57895bf56fc4e64f754387fe008d
  3. mv jdk1.8.0_181/ jdk1.8

3、修改环境变量(用vim编辑器来编辑profile文件,在文件末尾添加一下内容(按“i”进入编辑))

vim /etc/profile

  1. export JAVA_HOME=/usr/local/jdk1.8
  2. export JRE_HOME=${JAVA_HOME}/jre
  3. export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
  4. export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
  5. export PATH=$PATH:${JAVA_PATH}

4、跟新配置并测试

  1. source /etc/profile
  2. java

出现以下情况成功

六、tomcat8.0安装

1、下载安装包

  1. cd /usr/local/
  2. wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v8.0.53/bin/apache-tomcat-8.0.53.tar.gz

2、解压并删除源文件

  1. tar  -zxvf apache-tomcat-8.0.53.tar.gz
  2. rm -rf apache-tomcat-8.0.53.tar.gz
  3. mv apache-tomcat-8.0.53/ tomcat8

3、创建快捷键

  1. cd /root
  2. ln -s /usr/local/tomcat8/ tomcat8

4、配置脚本

vim /etc/init.d/tomcat8

  1. #!/bin/bash
  2. #
  3. ## description: Tomcat Start Stop Restart
  4. # processname: tomcat
  5. # chkconfig: 234 20 80
  6. export JAVA_HOME=/usr/local/jdk1.8
  7. export PATH=$JAVA_HOME/bin:$PATH

10. CATALINA_HOME=/usr/local/tomcat8

  1. 11.

12. case $1 in

  1. 13.

14. start)

  1. 15.

16. sh $CATALINA_HOME/bin/startup.sh

  1. 17.

18. ;;

  1. 19.

20. stop)

  1. 21.

22. sh $CATALINA_HOME/bin/shutdown.sh

  1. 23.

24. ;;

  1. 25.

26. restart)

  1. 27.

28. sh $CATALINA_HOME/bin/shutdown.sh

  1. 29.

30. sh $CATALINA_HOME/bin/startup.sh

  1. 31.

32. ;;

  1. 33.

34. esac

  1. 35.

36. exit 0

5、为tomcat8.service添加执行权限并开机启动

  1. chmod a+x /etc/init.d/tomcat8
  2. chkconfig tomcat8 on

6tomcat8服务

  1. 启动:service tomcat8 start
  2. 重启:service tomcat8 restart
  3. 关闭:service tomcat8 stop

7、进行访问(网址ip:8080,如本机192.168.1.150:8080

七、mysql安装

1、下载安装包

  1. cd /usr/local/
  2. wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

2、解压并删除源文件

  1. tar  -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
  2. rm -rf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
  3. mv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql

3、新建data目录

mkdir /usr/local/mysql/data

4、新建mysql用户、mysql用户组

  1. groupadd mysql
  2. useradd mysql -g mysql

5、将/usr/local/mysql的所有者及所属组改为mysql

chown -R mysql.mysql /usr/local/mysql

6、配置

/usr/local/mysql/bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data

  1. # 如果出现以下错误:
  2. 复制代码
  3. 2018-07-14 06:40:32 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
  4. 2018-07-14 06:40:32 [ERROR]   Child process: /usr/local/mysql/bin/mysqldterminated prematurely with errno= 32
  5. 2018-07-14 06:40:32 [ERROR]   Failed to execute /usr/local/mysql/bin/mysqld --bootstrap --datadir=/usr/local/mysql/data --lc-messages-dir=/usr/local/mysql/share --lc-messages=en_US --basedir=/usr/local/mysql
  6. -- server log begin --
  7. -- server log end --

10. 复制代码

11. # 则使用以下命令:

  1. 12.

13. /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize

14. # 如果出现以下错误:

  1. 15.

16. /usr/local/mysql/bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory

17. # 则执行以下命令:

  1. 18.

19. yum -y install numactl

  1. 20.

21. # 则执行以下命令:

  1. 22.

23. yum -y install numactl

  1. 24.

25. # 完成后继续安装:

  1. 26.

27. /usr/local/mysql/bin/mysqld --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --initialize

7、编辑配置文件

vim /etc/my.cnf

  1. [mysqld]
  2. datadir=/usr/local/mysql/data
  3. basedir=/usr/local/mysql
  4. socket=/tmp/mysql.sock
  5. user=mysql
  6. port=3306
  7. character-set-server=utf8
  8. # 取消密码验证
  9. skip-grant-tables

10. # Disabling symbolic-links is recommended to prevent assorted security risks

11. symbolic-links=0

12. # skip-grant-tables

13. [mysqld_safe]

14. log-error=/var/log/mysqld.log

15. pid-file=/var/run/mysqld/mysqld.pid

8、开启服务

# 将mysql加入服务

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

# 开机自启

chkconfig mysql on

# 开启

service mysql start

9、设置密码

# 登录(由于/etc/my.cnf中设置了取消密码验证,所以此处密码任意)

/usr/local/mysql/bin/mysql -u root -p

# 操作mysql数据库

>>use mysql;

# 修改密码

  1. >>update user set authentication_string=password('你的密码') where user='root';
  2. >>flush privileges;
  3. >>exit;

10、将/etc/my.cnf中的skip-grant-tables删除(注释掉 前边加上#)

vim /etc/my.cnf

11、登录再次设置密码

  1. /usr/local/mysql/bin/mysql -u root -p
  2. >>ALTER USER 'root'@'localhost' IDENTIFIED BY '修改后的密码';
  3. >>exit;

12、允许远程连接

  1. /usr/local/mysql/bin/mysql -u root -p
  2. >>use mysql;
  3. >>update user set host='%' where user = 'root';
  4. >>flush privileges;
  5. >>eixt;

13、添加快捷方式

ln -s /usr/local/mysql/bin/mysql /usr/bin

八、ngnix安装

1、安装运行库

yum install wget gcc gcc-c++ pcre-devel zlib-devel

2、下载ngnix

  1. cd /usr/local/
  2. wget http://nginx.org/download/nginx-1.14.0.tar.gz

3、解压,删除源文件、改名

  1. tar zxvf nginx-1.14.0.tar.gz
  2. rm -rf nginx-1.14.0.tar.gz
  3. mv nginx-1.14.0/ nginx1.14
  4. cd nginx1.14

4、检测系统配置, 生成make相关文件

./configure

5、编译并安装

make && make install

6、创建nginx启动命令脚本

vim /etc/init.d/nginx

  1. #! /bin/bash
  2. # Startup script for the nginx Web Server
  3. # chkconfig: - 85 15
  4. # description: nginx is a World Wide Web server. It is used to serve
  5. # HTML files and CGI.
  6. # processname: nginx
  7. PATH=/usr/local/nginx
  8. DESC="nginx daemon"
  9. NAME=nginx

10. DAEMON=$PATH/sbin/$NAME

11. CONFIGFILE=$PATH/conf/$NAME.conf

12. PIDFILE=$PATH/logs/$NAME.pid

13. SCRIPTNAME=/etc/init.d/$NAME

14. set -e

15. [ -x "$DAEMON" ] || exit 0

16. do_start() {

17. $DAEMON -c $CONFIGFILE || echo -n "nginx already running"

18. }

19. do_stop() {

20. $DAEMON -s stop || echo -n "nginx not running"

21. }

22. do_reload() {

23. $DAEMON -s reload || echo -n "nginx can't reload"

24. }

25. case "$1" in

26. start)

27. echo -n "Starting $DESC: $NAME"

28. do_start

29. echo "."

30. ;;

31. stop)

32. echo -n "Stopping $DESC: $NAME"

33. do_stop

34. echo "."

35. ;;

36. reload|graceful)

37. echo -n "Reloading $DESC configuration..."

38. do_reload

39. echo "."

40. ;;

41. restart)

42. echo -n "Restarting $DESC: $NAME"

43. do_stop

44. do_start

45. echo "."

46. ;;

47. *)

48. echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2

49. exit 3

50. ;;

51. esac

52. exit 0

  1. 53.

54. echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2

55. exit 3

56. ;;

57. esac

58. exit 0

  1. 59.

7、开机自启

设置执行权限

chmod a+x /etc/init.d/nginx

注册成服务

chkconfig --add nginx

设置开机启动

chkconfig nginx on

重启, 查看nginx服务是否自动启动

  1. shutdown -h 0 -r
  2. netstat -apn|grep nginx

8、服务信息

  1. #启动nginx服务
  2. systemctl start nginx.service
  3. #停止nginx服务
  4. systemctl stop nginx.service
  5. #重启nginx服务
  6. systemctl restart nginx.service
  7. #重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
  8. systemctl reload nginx.service

九、Redis安装

1、下载redis

  1. cd /usr/local/
  2. wget http://download.redis.io/releases/redis-4.0.10.tar.gz

2、解压,删除源文件、改名

  1. tar -zxvf redis-4.0.10.tar.gz
  2. rm -rf redis-4.0.10.tar.gz
  3. mv redis-4.0.10/ redis

3yum安装gcc依赖

yum install gcc tcl

4、安装

  1. cd redis
  2. make MALLOC=libc
  3. cd src && make install

5、测试是否成功

./redis-server

出现以下情况成功(按 ctrl + c可以关闭窗口)

6、修改配置文件

  1. cd /usr/local/redis/
  2. vim redis.conf
  3. 1、修改daemonize
  4. daemonize no #默认为no
  5. 修改为
  6. daemonize yes #后台进程方式改为yes
  7. 2、修改bind 配置

10. bind 127.0.0.1 #默认只有本机才能够连接

11. 修改为

12. bind 0.0.0.0 #所有ip都可以访问(也可修改为限定ip

  1. 13.

14. 3、修改protected-mode配置

15. 将

16. protected-mode yes #在默认保护模式下启用

17. 修改为

18. protected-mode no #禁用它,任何client不用认证即可连接

  1. 19.

20. 4、修改port端口号

21. 将

22. port 6379 #默认为6379端口

23. 修改为其他端口

24. port 6379  #可根据实际情况配置

  1. 25.

26. 5、设置密码

27. 将 requirepass foobared

28. 变为

29. requirepass 你的密码

  1. 30.
  2. 31.

7、再次启动

指定redis.conf文件启动

  1. cd /usr/local/redis/src/
  2. ./redis-server ../redis.conf

测试redis

  1. ./redis-cli -h 127.0.0.1 -p 6379 -a 你的密码
  2. 127.0.0.1:6379> set ko 'ok'
  3. OK
  4. 127.0.0.1:6379> keys *
  5. 1) "ko"
  6. 127.0.0.1:6379> get ko
  7. "ok"
  8. 127.0.0.1:6379> exit

关闭redis进程

首先使用ps -aux | grep redis查看redis进程,使用kill命令杀死进程

  1. ps -aux | grep redis
  2. root       2852  0.1  0.2 141832  2028 ?        Ssl  16:45   0:00 ./redis-server 127.0.0.1:6379
  3. root       2857  0.0  0.0 112704   976 pts/0    S+   16:46   0:00 grep --color=auto
  4. redis
  5. kill -9 2852

8、设置redis开机自启动

在/etc目录下新建redis目录

  1. cd /etc/
  2. mkdir redis

将/usr/local/redis/redis.conf 文件复制一份到/etc/redis目录下,并命名为6379.conf 

cp /usr/local/redis/redis.conf  /etc/redis/6379.conf

将redis的启动脚本复制一份放到/etc/init.d目录下

cp /usr/local/redis-4.0.10/utils/redis_init_script  /etc/init.d/redis

设置redis开机自启动 。先切换到/etc/init.d目录下,然后执行自启命令

  1. cd /etc/init.d/
  2. chkconfig redis on

service redis does not support chkconfig  
看结果是redisd不支持chkconfig 
解决方法:使用vim编辑redis文件,在第一行加入如下两行注释,保存退出

  1. # chkconfig:   2345 90 10
  2. # description:  Redis is a
    persistent key-value database

再次执行开机自启命令,成功

chkconfig redis on

9、服务

启动:

service redis start

关闭:

service redis stop

10、如果设置密码,关闭时会验证密码,需要更改启动脚本

cd /etc/init.d/

vi /etc/init.d/redis

然后

配置密码常量  PWD=你的密码

将关闭处代码更改为

  1. $CLIEXEC -a $PWD -p $REDISPORT shutdown
  2. $CLIEXEC -a "root" -p $REDISPORT shutdown

不更改会影响关闭效率。

MySQL如有错第二种做法

centos7
+ mysql5.7 tar包解压安装

#卸载系统自带的Mariadb
[root@hdp265dnsnfs ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
[root@hdp265dnsnfs ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
 
#删除etc目录下的my.cnf文件
 
[root@hdp265dnsnfs ~]# rm /etc/my.cnf
rm: cannot remove ?etc/my.cnf? No such file or directory
 
#检查mysql是否存在
[root@hdp265dnsnfs ~]# rpm -qa | grep mysql
[root@hdp265dnsnfs ~]# 
 
#检查mysql组和用户是否存在,如无创建
[root@hdp265dnsnfs ~]# cat /etc/group | grep mysql 
[root@hdp265dnsnfs ~]#  cat /etc/passwd | grep mysql
 
#创建mysql用户组
[root@hdp265dnsnfs ~]# groupadd mysql
#创建一个用户名为mysql的用户并加入mysql用户组
[root@hdp265dnsnfs ~]# useradd -g mysql mysql
#制定password 为111111
[root@hdp265dnsnfs ~]# passwd mysql
Changing password for user mysql.
New password: 
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.
 
#由于我的/usr/local空间不足,所以我安装到/var
[root@hdp265dnsnfs var]# tar -zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz 
[root@hdp265dnsnfs var]# mv mysql-5.7.18-linux-glibc2.5-x86_64/ mysql57
 
#更改所属的组和用户
[root@hdp265dnsnfs var]# chown -R mysql mysql57/
[root@hdp265dnsnfs var]# chgrp -R mysql mysql57/
[root@hdp265dnsnfs var]# cd mysql57/ [root@hdp265dnsnfs mysql57]# mkdir data [root@hdp265dnsnfs mysql57]# chown -R mysql:mysql data

在etc下新建配置文件my.cnf,并在该文件内添加以下配置

按 Ctrl+C 复制代码

按 Ctrl+C 复制代码

安装和初始化

[root@hdp265dnsnfs mysql57]# bin/mysql_install_db --user=mysql --basedir=/var/mysql57/ --datadir=/var/mysql57/data/
2017-04-17 17:40:02 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2017-04-17 17:40:05 [WARNING] The bootstrap log isn't empty:
2017-04-17 17:40:05 [WARNING] 2017-04-17T09:40:02.728710Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2017-04-17T09:40:02.729161Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2017-04-17T09:40:02.729167Z 0 [Warning] Changed limits: table_open_cache: 407 (requested 2000)
[root@hdp265dnsnfs mysql57]# cp ./support-files/mysql.server /etc/init.d/mysqld
[root@hdp265dnsnfs mysql57]# chown 777 /etc/my.cnf 
[root@hdp265dnsnfs mysql57]# chmod +x /etc/init.d/mysqld
[root@hdp265dnsnfs mysql57]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
 
#设置开机启动
 
[root@hdp265dnsnfs mysql57]# chkconfig --level 35 mysqld on
[root@hdp265dnsnfs mysql57]# chkconfig --list mysqld
 
[root@hdp265dnsnfs mysql57]# chmod +x /etc/rc.d/init.d/mysqld
[root@hdp265dnsnfs mysql57]# chkconfig --add mysqld
[root@hdp265dnsnfs mysql57]# chkconfig --list mysqld
[root@hdp265dnsnfs mysql57]# service mysqld status
 SUCCESS! MySQL running (4475)

etc/profile/

export PATH=$PATH:/var/mysql57/bin
 
[root@hdp265dnsnfs mysql57]# source /etc/profile

获得初始密码

[root@hdp265dnsnfs bin]# cat /root/.mysql_secret  
# Password set for user 'root@localhost' at 2017-04-17 17:40:02 
_pB*3VZl5T<6

修改密码

[root@hdp265dnsnfs bin]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.18
 
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
 
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> set PASSWORD = PASSWORD('111111');
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

添加远程访问权限

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
 
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
 
mysql> select host,user from user;
+-----------+-----------+
| host      | user      |
+-----------+-----------+
| %         | root      |
| localhost | mysql.sys |
+-----------+-----------+
2 rows in set (0.00 sec)


create user 'xxx'@'%' identified by '123';  这里 @‘%’ 表示在任何主机都可以登录

重启生效

/bin/systemctl restart  mysql.service
 
[root@hdp265dnsnfs bin]# /etc/init.d/mysqld restart 
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 

为了在任何目录下可以登录mysql

ln -s /var/mysql57/bin/mysql   /usr/bin/mysql