Skip to content

Commit

Permalink
✄ 添加[写给java开发的运维笔记]中级篇
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Sep 9, 2016
1 parent 6b6e308 commit d15d2c4
Show file tree
Hide file tree
Showing 5 changed files with 297 additions and 6 deletions.
12 changes: 10 additions & 2 deletions learn_server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@

对于Java开发人员,掌握基本的运维技能是必须的,如果你还不熟悉Linux环境,可以看[这个](https://github.com/judasn/Linux-Tutorial)教程。

## 初级篇

1. [在虚拟机里安装centos6](virtual-machine-install-centos6.md)
2. [初始化操作系统](init-os.md)
3. [安装jdk环境](install-jdk.md)
4. [安装tomcat](install-tomcat.md)
5. [安装mysql及配置](install-mysql.md)
6. [安装nginx](install-nginx.md)
7. [配置tomcat+nginx反向代理](config-nginx-proxy.md)
8. [优化nginx配置](optimization-nginx.md)

## 中级篇

1. [配置tomcat+nginx反向代理](config-nginx-proxy.md)
2. [优化tomcat配置](optimization-tomcat.md)
3. [配置多个tomcat](config-more-tomcat.md)
4. [优化nginx配置](optimization-nginx.md)
5. [优化mysql配置](optimization-mysql.md)
123 changes: 122 additions & 1 deletion learn_server/init-os.md
Original file line number Diff line number Diff line change
@@ -1 +1,122 @@
# 初始化操作系统
# 初始化操作系统

Ok,我们安装好了CentOS系统,可以使用SSH工具连接上去进行尝试了,我推荐使用 [XShell] 这款工具,支持中文,还有一些主题使用。

现在我们还不知道CentOS的IP是无法连接的,所以先在虚拟机中启动CentOS。

![](https://ooo.0o0.ooo/2016/09/09/57d225f429a16.png)

这里账户输入 `root` 密码是你在安装的时候设置的。

## 网络配置

这时候我们键入 `ifconfig` 查看ip

```bash
[root@localhost ~]# ifconfig

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
```

发现还没有,我们需要设置一下网卡配置。

```bash
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
```

使用 `vi` 命令编辑第一块网卡的配置

```bash
DEVICE=eth0
HWADDR=00:0C:29:50:58:BE
TYPE=Ethernet
UUID=58f93b51-314d-49bb-9db2-036bf91161fb
ONBOOT=no
NM_CONTROLLED=yes
BOOTPROTO=dhcp
```

只需要将 `ONBOOT` 修改为 `yes` ,然后保存。

```bash
[root@localhost ~]# service network restart
Shutting down interface eth0: [ OK ]
Shutting down loopback interface: [ OK ]
Bringing up loopback interface: [ OK ]
Bringing up interface eth0:
Determining IP information for eth0... done.
[ OK ]
```

这时候我们再查看一下ip

```bash
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:50:58:BE
inet addr:192.168.100.128 Bcast:192.168.100.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe50:58be/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:302 errors:0 dropped:0 overruns:0 frame:0
TX packets:189 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:33591 (32.8 KiB) TX bytes:29591 (28.8 KiB)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 b) TX bytes:0 (0.0 b)
```

网卡的配置已经被应用到了,我们CentOS的IP是 `192.168.100.128` 你的可能和我不一样,那这个192.168.100开头的网段是在哪里设置的呢?

![](https://ooo.0o0.ooo/2016/09/09/57d2293d28fe5.png)

点击虚拟网络编辑器

![](https://ooo.0o0.ooo/2016/09/09/57d229a2311f4.png)

如果你在执行 `service network restart` 的时候失败可以在这里修改一个网段试试。

此时你已经可以使用SSH工具连接到你的CenOS主机了。

关于更详细的网络设置大家可以参考这2篇文章:

- [虚拟机下CentOS 6.5配置IP地址的三种方法](http://www.centoscn.com/CentOS/config/2014/1112/4112.html)
- [Vmware安装Centos NAT方式设置静态IP](http://www.centoscn.com/CentosBug/osbug/2015/1224/6568.html)


## yum源设置

先安装 `wget` 工具,我们安装的操作系统mini版的,默认没有wget命令,执行以下命令:

```bash
yum install -y wget
```

然后设置yum源,我选择的是网易的源,你也可以设置阿里的或者其他。

```bash
cd /etc/yum.repos.d
mv CentOS-Base.repo bak-CentOS-Base.repo
wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
yum clean all
yum makecache
```

安全性的配置在这里先不讲解,我们先用 `root`账户来操作。

## links
* [目录](<README.md>)
* 上一节: [在虚拟机里安装centos6](<virtual-machine-install-centos6.md>)
* 下一节: [安装jdk环境](<install-jdk.md>)
68 changes: 67 additions & 1 deletion learn_server/install-jdk.md
Original file line number Diff line number Diff line change
@@ -1 +1,67 @@
# 安装jdk环境
# 安装jdk环境

服务器上如果不需要编码实际应该不安装JDK只安装JRE,我们考虑到以后可能安装其他软件就直接装JDK了。

## 下载JDK

[下载jdk](http://stackoverflow.com/questions/10268583/downloading-java-jdk-on-linux-via-wget-is-shown-license-page-instead)

上面的连接是stackoverflow有开发者写的不使用cookie下载jdk和jre的命令。

```bash
[root@localhost ~]# wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u102-b14/jdk-8u102-linux-x64.tar.gz
--2016-09-09 19:57:01-- http://download.oracle.com/otn-pub/java/jdk/8u102-b14/jdk-8u102-linux-x64.tar.gz
Resolving download.oracle.com... 23.4.240.57, 23.4.240.59
Connecting to download.oracle.com|23.4.240.57|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://120.52.72.24:80/download.oracle.com/c3pr90ntc0td/otn-pub/java/jdk/8u102-b14/jdk-8u102-linux-x64.tar.gz [following]
--2016-09-09 19:57:01-- http://120.52.72.24/download.oracle.com/c3pr90ntc0td/otn-pub/java/jdk/8u102-b14/jdk-8u102-linux-x64.tar.gz
Connecting to 120.52.72.24:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 181435897 (173M) [application/x-gzip]
Saving to: “jdk-8u102-linux-x64.tar.gz”

100%[==================================================================================================================================>] 181,435,897 2.07M/s in 85s

2016-09-09 19:58:26 (2.04 MB/s) - “jdk-8u102-linux-x64.tar.gz” saved [181435897/181435897]
```

## 解压

```bash
[root@localhost ~]# tar -zxvf jdk-8u102-linux-x64.tar.gz
[root@localhost ~]# mkdir /usr/local/java
[root@localhost ~]# mv jdk1.8.0_102/ /usr/local/java/
```

## 配置环境变量

```bash
[root@localhost ~]# vim /etc/profile
```

在最后一行添加

```bash
# java
export JAVA_HOME=/usr/local/java/jdk1.8.0_102
export JRE_HOME=/usr/local/java/jdk1.8.0_102/jre
export CLASSPATH=.:$JRE_HOME/lib/dt.jar:$JRE_HOME/lib/tools.jar
export PATH=$JRE_HOME/bin:$JRE_HOME/bin:$PATH
```

## 生效

```bash
[root@localhost ~]# source /etc/profile
[root@localhost ~]# java -version
java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
```
这里我安装的是最新版的JDK。

## links
* [目录](<README.md>)
* 上一节: [初始化操作系统](<init-os.md>)
* 下一节: [安装tomcat](<install-tomcat.md>)
93 changes: 92 additions & 1 deletion learn_server/install-tomcat.md
Original file line number Diff line number Diff line change
@@ -1 +1,92 @@
# 安装tomcat
# 安装tomcat

上一章节我们安装了JDK的环境,Tomcat运行的前提是要有JDK环境。

## 下载Tomcat

```bash
[root@localhost ~]# wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.5/bin/apache-tomcat-8.5.5.tar.gz
[root@localhost ~]# tar -zxvf apache-tomcat-8.5.5.tar.gz
[root@localhost ~]# mv apache-tomcat-8.5.5 /usr/local/tomcat8
```

## 启动tomcat

```bash
[root@localhost ~]# cd /usr/local/tomcat8/bin/
[root@localhost bin]# ./startup.sh
Using CATALINA_BASE: /usr/local/tomcat8
Using CATALINA_HOME: /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME: /usr/local/java/jdk1.8.0_102/jre
Using CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.
```

现在打开 [http://192.168.100.128:8080](http://192.168.100.128:8080) 应该就可以看到Tomcat的汤姆猫页面。
而事实是你看到这个:

![](https://ooo.0o0.ooo/2016/09/09/57d23ea8c353e.png)

哦草。。。为什么,机智的同学已经想到了,防火墙啊。对我们没有对防火墙进行任何配置,实际上8080端口是不对外开放的,
那么如何解决呢?

- 关闭防火墙
- 开放8080端口

## 配置防火墙

在CentOS上关闭防火墙是非常简单的

```bash
[root@localhost bin]# service iptables stop
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
```

这时候你再访问 [http://192.168.100.128:8080](http://192.168.100.128:8080) 就可以看到

![](https://ooo.0o0.ooo/2016/09/09/57d23f752bfce.png)

当然这种方式是简单粗暴的,我们在真实服务器上不可能这么做,怎么做呢?

```bash
[root@localhost bin]# vim /etc/sysconfig/iptables
```

我们看到 `iptables` 的默认配置是这样的:

```bash
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
```

只需要添加一行和 `22` 端口一样的配置:

```bash
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
```

这样既把8080的TCP端口对外开放了,然后重启防火墙

```bash
[root@localhost bin]# service iptables restart
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
```

可以达到同样的效果。
7 changes: 6 additions & 1 deletion learn_server/virtual-machine-install-centos6.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,9 @@

![](https://ooo.0o0.ooo/2016/09/09/57d21e687d0de.png)

看到这个界面你的CentOS就安装完成了,可以进行下一关了,上车!
看到这个界面你的CentOS就安装完成了,点击REBOOT即重启机器。
可以进行下一关了,上车!

## links
* [目录](<README.md>)
* 下一节: [初始化操作系统](<init-os.md>)

0 comments on commit d15d2c4

Please sign in to comment.