[java][server] 开发用服务器简单配置
(腾讯云 CentOS 7.3 64位 端口全开)
安装系统常用支持库
[yum install -y gcc gdb strace gcc-c++ autoconf libjpeg libjpeg-devel pcre pcre-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs patch e2fsprogs-devel krb5-devel libidn libidn-devel openldap-devel nss_ldap openldap-clients openldap-servers libevent-devel libevent uuid-devel uuid mysql-devel openssl openssl-devel]
修改Shell命令行提示
在[/etc/profile]文件最末尾添加[export PS1='[\u][$PWD] '], 执行[source /etc/profile]应用修改
修改SSH连接不自动断开
执行[echo $TMOUT], 如果输出空或0表示不超时,大于0的数字n表示n秒没有收入则超时
如需修改则在[/etc/profile]文件最末尾添加[export TMOUT=0], 执行[source /etc/profile]应用修改
备份[执行 cp sshd_config sshd_config.bak]来备份[sshd_config]文件
修改[/etc/ssh/sshd_config]文件, [ClientAliveInterval 60]和[ClientAliveCountMax 3], 并去掉其参数前面的[#]注释
执行[diff sshd_config sshd_config.bak]可以比较两个文件内容的差异
执行[service sshd reload]应用修改
新建目录
[/application]:存放安装的软件
[/application/resource]:存放安装包等资源
[/application/tomcat]:存放Tomcat服务器, 包括有test等tomcat
安装Oracle JDK
执行[java -version]或[rpm -qa | grep java]来检查有无安装OpenJDK, 有的话需要卸载(自行百度)
下载JDK(例如:jdk-7u80-linux-x64.tar.gz)到[/application]目录, 网址[http://www.oracle.com/technetwork/java/archive-139210.html], 在[/application/resource]目录下存一份
在[/application]目录下执行[tar -zxvf jdk-7u80-linux-x64.tar.gz]来解压JDK, 得到[jdk1.7.0_80]目录, 删除安装包
执行[mv jdk1.7.0_80 jdk-1.7.0.80]来重命名JDK包目录为[jdk-1.7.0.80]
在[/etc/profile]文件最末尾添加如下内容
[export JAVA_HOME=/application/jdk-1.7.0.80]
[export PATH=$JAVA_HOME/bin:$PATH]
[export CLASSPARH=.:$JAVA_HOME/lib]
执行[source /etc/profile]应用修改
大幅缩短Tomcat启动时间
修改[/application/jdk-1.7.0.80/jre/lib/security/java.security]文件, 把[securerandom.source=file:/dev/urandom]替换成[securerandom.source=file:/dev/./urandom]
解除Oracle JDK安全策略限制
根据JDK版本下载对应的策略文件, 解压, 把[local_policy.jar]和[US_export_policy.jar]拷贝到[/application/jdk-1.7.0.80/jre/lib/security]目录下覆盖原有的策略文件
7:[https://files.cnblogs.com/files/mrathena/jce_policy-7.zip], 8:[https://files.cnblogs.com/files/mrathena/jce_policy-8.zip]
安装Tomcat
下载Tomcat(例如:apache-tomcat-7.0.81.tar.gz)到[/application]目录, 在[/application/resource]目录下存一份
在[/application/tomcat]目录下执行[tar -zxvf apache-tomcat-7.0.81.tar.gz]来解压Tomcat, 得到[apache-tomcat-7.0.81]目录, 删除压缩包
删除[apache-tomcat-7.0.81/webapps]下的所有项目
修改[apache-tomcat-7.0.81/conf/server.xml], 在8080端口的那里添加[URIEncoding="UTF-8"]配置
该Tomcat作为标准容器, 供以后添加服务器的时候复制使用, 复制[apache-tomcat-7.0.81]到[test]
服务器与端口对应规则:[test]->[10000,20000,30000]
执行[ln -s /application/tomcat/test/bin/startup.sh tomcat.test]来创建[test]项目的服务器快捷方式
安装Nginx
下载Nginx(例如:nginx-1.13.5.tar.gz)到[/application]目录, 在[/application/resource]目录下存一份
在[/application]目录下执行[tar -zxvf nginx-1.13.5.tar.gz]来解压Nginx, 得到[nginx-1.13.5]目录
执行[mv nginx-1.13.5 nginx]来重命名Nginx包目录为[nginx]
执行[mkdir nginx-1.13.5]来创建[nginx-1.13.5]目录
在[nginx]目录下执行[./configure --prefix=/application/nginx-1.13.5 --with-http_ssl_module]来配置Nginx的安装
在[nginx]目录下执行[make(把各种语言写的源码文件,变成可执行文件和各种库文件)]和[make install(把这些编译出来的可执行文件和库文件复制到合适的地方)]
安装成功后删除[nginx]目录
执行[ln -s /application/nginx-1.13.5/sbin/nginx /application/nginx]来创建Ngixn的快捷方式
以上过程中报错则需要自行安装各种依赖包
配置Nginx
编辑[nginx-1.13.5/conf/nginx.conf], 内容如下
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 60;
charset UTF-8;
# 上传文件限制
client_max_body_size 100m;
# 跨域支持(有用?)
add_header Access-Control-Allow-Origin *;
server {
listen 80;
server_name mrathena.com;
location /test {
proxy_pass http://localhost:10000/test;
proxy_redirect off;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
...
location / {
root html;
index index.html index.htm;
}
}
}
如果需要配置HTTPS证书(SSL, 苹果需求之一), 把下载的域名证书[1_mrathena.com_bundle.crt, 2_mrathena.com.key]放到[nginx/conf]目录下, 并更新server结点(注: 该配置只是Nginx端是https, Tomcat端还是http)
server {
listen 80;
server_name mrathena.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name mrathena.com;
ssl on;
ssl_certificate 1_mrathena.com_bundle.crt; #证书公钥
ssl_certificate_key 2_mrathena.com.key; #证书私钥
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;#按照这个套件配置
ssl_prefer_server_ciphers on;
location /test {
proxy_pass http://localhost:10000/test;
proxy_redirect off;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
...
location / {
root html;
index index.html index.htm;
}
}
如果需要配置IPV6(苹果需求之一), 添加原有解析对应的AAAA解析, 具体如图所示, IPV4转IPV6方法如下所示

/**
* 转自: http://wendy1018.iteye.com/blog/1606850#comments
* 描述:
* <p>
* ip转换byte数组管理类。
* </p>
* 创建日期:2012-7-25 下午3:34:00<br>
*
* @author:Wendy<br>
* @update:$Date: 2012-07-25 18:14:40 +0800 (Wed, 25 Jul 2012) $<br>
* @version:$Revision: 779 $<br>
* @since 1.0.0
*/
public class IPConvert {
private static final int IPV6Length = 8; // IPV6地址的分段
private static final int IPV4Length = 4; // IPV6地址分段
private static final int IPV4ParmLength = 2; // 一个IPV4分段占的长度
private static final int IPV6ParmLength = 4; // 一个IPV6分段占的长
public static void main(String[] args) {
String ip = "182.254.222.202";
System.out.println(buildKey(ip));
}
/**
* IPV6、IPV4转化为十六进制串
*
* @param ipAddress
* @return
*/
private static String buildKey(String ipAddress) {
String Key = "";
// ipv4标识 。判断是否是ipv4地址
int dotFlag = ipAddress.indexOf(".");
// ipv6标识 。判断是否是ipv6地址
int colonFlag = ipAddress.indexOf(":");
// ipv6标识 。判断是否是简写的ipv6地址
int dColonFlag = ipAddress.indexOf("::");
// 将v6或v4的分隔符用&代替
ipAddress = ipAddress.replace(".", "&");
ipAddress = ipAddress.replace(":", "&");
// ipv4 address。将ipv4地址转换成16进制的形式
if (dotFlag != -1 && colonFlag == -1) {
String[] arr = ipAddress.split("&");
// 1、 ipv4转ipv6,前4组数补0或f
for (int i = 0; i < IPV6Length - IPV4ParmLength; i++) {
// 根据v4转v6的形式,除第4组数补ffff外,前3组数补0000
if (i == IPV6Length - IPV4ParmLength - 1) {
Key += "ffff";
} else {
Key += "0000";
}
}
// 2、将ipv4地址转成16进制
for (int j = 0; j < IPV4Length; j++) {
// 1)将每组ipv4地址转换成16进制
arr[j] = Integer.toHexString(Integer.parseInt(arr[j]));
// 2) 位数不足补0,ipv4地址中一组可转换成一个十六进制,两组数即可标识ipv6中的一组,v6中的一组数不足4位补0
for (int k = 0; k < (IPV4ParmLength - arr[j].length()); k++) {
Key += "0";
}
Key += arr[j];
}
}
// Mixed address with ipv4 and ipv6。将v4与v6的混合地址转换成16进制的形式
if (dotFlag != -1 && colonFlag != -1 && dColonFlag == -1) {
String[] arr = ipAddress.split("&");
for (int i = 0; i < IPV6Length - IPV4ParmLength; i++) {
// 将ip地址中每组不足4位的补0
for (int k = 0; k < (IPV6ParmLength - arr[i].length()); k++) {
Key += "0";
}
Key += arr[i];
}
for (int j = 0; j < IPV4Length; j++) {
arr[j] = Integer.toHexString(Integer.parseInt(arr[j]));
for (int k = 0; k < (IPV4ParmLength - arr[j].length()); k++) {
Key += "0";
}
Key += arr[j];
}
}
// Mixed address with ipv4 and ipv6,and there are more than one
// '0'。将v4与v6的混合地址(如::32:dc:192.168.62.174)转换成16进制的形式
// address param
if (dColonFlag != -1 && dotFlag != -1) {
String[] arr = ipAddress.split("&");
// 存放16进制的形式
String[] arrParams = new String[IPV6Length + IPV4ParmLength];
int indexFlag = 0;
int pFlag = 0;
// 1、将简写的ip地址补0
// 如果ip地址中前面部分采用简写,做如下处理
if ("".equals(arr[0])) {
// 1)如果ip地址采用简写形式,不足位置补0,存放到arrParams中
for (int j = 0; j < (IPV6Length + IPV4ParmLength - (arr.length - 2)); j++) {
arrParams[j] = "0000";
indexFlag++;
}
// 2)将已有值的部分(如32:dc:192.168.62.174)存放到arrParams中
for (int i = 2; i < arr.length; i++) {
arrParams[indexFlag] = arr[i];
indexFlag++;
}
} else {
for (int i = 0; i < arr.length; i++) {
if ("".equals(arr[i])) {
for (int j = 0; j < (IPV6Length + IPV4ParmLength - arr.length + 1); j++) {
arrParams[indexFlag] = "0000";
indexFlag++;
}
} else {
arrParams[indexFlag] = arr[i];
indexFlag++;
}
}
}
// 2、ip(去除ipv4的部分)中采用4位十六进制数表示一组数,将不足4位的十六进制数补0
for (int i = 0; i < IPV6Length - IPV4ParmLength; i++) {
// 如果arrParams[i]组数据不足4位,前补0
for (int k = 0; k < (IPV6ParmLength - arrParams[i].length()); k++) {
Key += "0";
}
Key += arrParams[i];
// pFlag用于标识位置,主要用来标识ipv4地址的起始位
pFlag++;
}
// 3、将ipv4地址转成16进制
for (int j = 0; j < IPV4Length; j++) {
// 1)将每组ipv4地址转换成16进制
arrParams[pFlag] = Integer.toHexString(Integer.parseInt(arrParams[pFlag]));
// 2)位数不足补0,ipv4地址中一组可转换成一个十六进制,两组数即可标识ipv6中的一组,v6中的一组数不足4位补0
for (int k = 0; k < (IPV4ParmLength - arrParams[pFlag].length()); k++) {
Key += "0";
}
Key += arrParams[pFlag];
pFlag++;
}
}
// ipv6 address。将ipv6地址转换成16进制
if (dColonFlag == -1 && dotFlag == -1 && colonFlag != -1) {
String[] arrParams = ipAddress.split("&");
// 将v6地址转成十六进制
for (int i = 0; i < IPV6Length; i++) {
// 将ipv6地址中每组不足4位的补0
for (int k = 0; k < (IPV6ParmLength - arrParams[i].length()); k++) {
Key += "0";
}
Key += arrParams[i];
}
}
if (dColonFlag != -1 && dotFlag == -1) {
String[] arr = ipAddress.split("&");
String[] arrParams = new String[IPV6Length];
int indexFlag = 0;
if ("".equals(arr[0])) {
for (int j = 0; j < (IPV6Length - (arr.length - 2)); j++) {
arrParams[j] = "0000";
indexFlag++;
}
for (int i = 2; i < arr.length; i++) {
arrParams[indexFlag] = arr[i];
i++;
indexFlag++;
}
} else {
for (int i = 0; i < arr.length; i++) {
if ("".equals(arr[i])) {
for (int j = 0; j < (IPV6Length - arr.length + 1); j++) {
arrParams[indexFlag] = "0000";
indexFlag++;
}
} else {
arrParams[indexFlag] = arr[i];
indexFlag++;
}
}
}
for (int i = 0; i < IPV6Length; i++) {
for (int k = 0; k < (IPV6ParmLength - arrParams[i].length()); k++) {
Key += "0";
}
Key += arrParams[i];
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < Key.length(); i++) {
if (i % 4 == 0) {
sb.append(":").append(Key.substring(i, i + 4));
}
}
return sb.toString().substring(1);
}
/**
* 十六进制串转化为IP地址
*
* @param key
* @return
*/
private static String splitKey(String key) {
String IPV6Address = "";
String IPAddress = "";
String strKey = "";
String ip1 = key.substring(0, 24);
String tIP1 = ip1.replace("0000", "").trim();
if (!"".equals(tIP1) && !"FFFF".equals(tIP1)) {
// 将ip按:分隔
while (!"".equals(key)) {
strKey = key.substring(0, 4);
key = key.substring(4);
if ("".equals(IPV6Address)) {
IPV6Address = strKey;
} else {
IPV6Address += ":" + strKey;
}
}
IPAddress = IPV6Address;
}
return IPAddress;
}
/**
* 将ip地址都转成16个字节的数组。先将v6地址存以":"分隔存放到数组中,再将数组中的每两位取存到长度为16的字符串数组中,
* 再将这两位十六进制数转成十进制,再转成byte类型存放到16个字的数组中。
*
* @param ip
* @return
*/
public static byte[] toByte(String ip) {
// 将ip地址转换成16进制
String Key = buildKey(ip);
// 将16进制转换成ip地址
String ip6 = splitKey(Key);
// 将v6f地址存以":"分隔存放到数组中
String[] ip6Str = ip6.split(":");
String[] ipStr = new String[16];
byte[] ip6Byte = new byte[16];
// 将数组中的每两位取存到长度为16的字符串数组中
for (int j = 0, i = 0; i < ip6Str.length; j = j + 2, i++) {
ipStr[j] = ip6Str[i].substring(0, 2);
ipStr[j + 1] = ip6Str[i].substring(2, 4);
}
// 将ipStr中的十六进制数转成十进制,再转成byte类型存放到16个字的数组中
for (int i = 0; i < ip6Byte.length; i++) {
ip6Byte[i] = (byte) Integer.parseInt(ipStr[i], 16);
}
return ip6Byte;
}
}
在[/application]目录下
Nginx测试配置是否正确[./nginx -t]
Nginx启动[./nginx]
Nginx重启[./nginx -s reload]
Nginx停止[./nginx -s stop]
安装MySQL (这是Windows 5.7.x版的)
1. 解压MySQL压缩包, [D:\develop\mysql-5.7.18]
将解压目录下默认文件 my-default.ini 拷贝一份,改名 my.ini
复制下面的配置信息到 my.ini 保存(需要按情况修改basedir和datadir)
#如果没有my-default.ini,可自己新建my.ini
#########################################################
[client]
port=3306
default-character-set=utf8
[mysqld]
port=3306
character_set_server=utf8
basedir=D:\develop\mysql-5.7.18
datadir=D:\develop\mysql-5.7.18\data
default-storage-engine=INNODB
#注:实践得出,上述文件应保存AscII格式,否则会报以下错误
#error: Found option without preceding group in config file:....
#########################################################
2. 添加环境变量:MYSQL_HOME, path添加[%MYSQL_HOME%\bin;]
3. 管理员CMD, cd到mysql的bin目录,
执行[mysqld --initialize-insecure],这个命令会生成无密码的root用户。而命令[mysqld --initialize]自动生成带随机密码的root用户
执行[mysqld install]安装MySQL服务
#执行这几步,是因为在MySQL5.7.x中没有data文件夹,需要用这几个命令产生data文件夹
#移除服务命令为:[mysqld remove]
4. 启动和停止服务:[net start mysql], [net stop mysql]
5. 登录:[mysql -uroot -p]
更新root用户密码:[update mysql.user set authentication_string=PASSWORD('root') where user='root';]
授权root用户远程登录权限:[GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;]
刷新权限:[flush privileges;]
退出登录:[quit]

浙公网安备 33010602011771号