centos7最小版本安装nginx+tomcat+java+mysql运行环境

时间:2021-10-31 15:02:41

  最近项目从windows搬到linux,由于项目组成员有限并且有其它紧急的任务需要处理,因而这个任务就落到我的头上了。下面记录下centos最小版本安装nginx+tomcat+mysql+java的运行环境。

  • centos7 minimal安装

  参考:http://www.tecmint.com/centos-7-installation/

  • 安装java

  下载Java,可到oracle官网获取相应的连接

wget http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x64.tar.gz?AuthParam=1456375809_50db5985ac01ede6091b467add0b89b2
# 改名:
mv jdk-7u79-linux-x64.tar.gz?AuthParam=1456375809_50db5985ac01ede6091b467add0b89b2 jdk-7u79-linux-x64.tar.gz
# 解压:
tar -zxvf jdk-7u79-linux-x64.tar.gz
# 更换目录:
mv ./jdk1..0_79 /usr/local/ jdk1..0_79

  添加到环境变量

root@localhost java]# vi /etc/profile
# 在export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL下添加 export JAVA_HOME=/usr/local/jdk1..0_79
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

  执行

source /etc/profile

  使环境变量生效

  查看是否安装成功

[root@localhost jdk1..0_79]# java -version
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) -Bit Server VM (build 24.79-b02, mixed mode)
  • 安装tomcat
# 创建用户
groupadd tomcat
useradd -s /bin/bash -g tomcat tomcat # 下载:
wget http://apache.opencas.org/tomcat/tomcat-7/v7.0.68/bin/apache-tomcat-7.0.68.tar.gz
# 解压:
tar -zxvf apache-tomcat-7.0..tar.gz
# 复制:
cp -R ./apache-tomcat-7.0. /usr/local
cd /usr/local/apache-tomcat-7.0.
# 修改权限:
chown -R tomcat:tomcat apache-tomcat-7.0.
# 启动:
sh ./bin/startup.sh

  将8080端口添加到防火墙例外并重启

firewall-cmd --zone=public --add-port=/tcp --permanent
firewall-cmd --reload

  访问:http://IP:8080/,熟悉的感觉

centos7最小版本安装nginx+tomcat+java+mysql运行环境

  • 安装Mysql
# 创建用户
groupadd mysql
useradd -g mysql -s /sbin/nologin -d /dev/null mysql
# 查看mysql用户
id mysql
# 下载:
wget http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz
tar -zxvf mysql-5.7.-linux-glibc2.-x86_64.tar.gz
mv mysql-5.7.-linux-glibc2.-x86_64 /usr/local/mysql
chown -R mysql:mysql mysql
cd /usr/local/mysql
mkdir /data
cd /data
mkdir mysql
cd mysql
mkdir -p mysql3306/{logs,tmp,data}
cp /usr/local/mysql/support-files/my-default.cnf /data/mysql/mysql3306/my.cnf vi /data/mysql/mysql3306/my.cnf
# 修改
# 指定data目录
datadir=/data/mysql/mysql3306/data/
# 指定监听端口
port=3306
# 表名忽略大小写
lower_case_table_names=

  初始化mysql,(注意获取初始化密码),命令安装也可参考:centos7 安装mysql5.7.11注意事项

[root@localhost mysql]# bin/mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --initialize
--26T10::.821897Z [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
--26T10::.822126Z [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
--26T10::.822138Z [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
--26T10::.311120Z [Warning] InnoDB: New log files created, LSN=
--26T10::.411026Z [Warning] InnoDB: Creating foreign key constraint system tables.
--26T10::.492963Z [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b671cd20-dc73-11e5-abe1-000c29c3b190.
--26T10::.495657Z [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
--26T10::.511137Z [Note] A temporary password is generated for root@localhost: fUVd&7p,qbf0
# 此处需要注意root用户生成的随机密码,后面要用到。

  启动mysql

 bin/mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --user=root

  设置root用户密码

[root@localhost mysql]# ./bin/mysqladmin -uroot -p password
Enter password:
New password:
Confirm new password:
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

  编辑/etc/profile,将mysql添加到环境变量(可参考java添加环境变量的方法)

export PATH=$PATH:/usr/local/mysql/bin/
  • 安装nginx

  安装依赖库

yum -y install gcc-c++
yum -y install pcre pcre-devel
yum -y install zlib zlib-devel
yum -y install openssl openssl-devel libssl-dev

  下载并解压

wget http://nginx.org/download/nginx-1.9.12.tar.gz
tar -zxvf nginx-1.9..tar.gz

  编译安装

./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module
make
make install

  如果没报错的话,查看是否安装成功

whereis nginx

  添加到防火墙例外

firewall-cmd --zone=public --add-port=/tcp --permanent
firewall-cmd --reload

  启动nginx

/usr/local/nginx/nginx

  访问:http://IP/,熟悉的味道

centos7最小版本安装nginx+tomcat+java+mysql运行环境

  • Nginx和tomcat整合

  tomcat配置

###### Tomcat配置
# 创建tomcat项目默认目录
mkdir /home/www/web/jsp/
# 配置tomcat默认项目
# ./conf/server.xml中的Host节点中添加
<Context path="" debug="" docBase="/home/www/web/jsp/" reloadable="true"></Context>
# 重启tomcat
/usr/local/apache-tomcat-7.0./bin/shutdown.sh
/usr/local/apache-tomcat-7.0./bin/startup.sh
# 创建index.jsp内容为“tomcat web project index page”

  http://IP:8080/

  centos7最小版本安装nginx+tomcat+java+mysql运行环境

  创建test.html,内容为“tomcat test html”

  http://192.168.222.128:8080/test.html

  centos7最小版本安装nginx+tomcat+java+mysql运行环境

  nginx配置

  创建nginx静态文件存放目录

mkdir /home/www/web/ROOT

  代理设置

[root@localhost nginx]# vi proxy.conf

#!nginx
# proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #获取真实ip
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #获取代理者的真实ip
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout ;
proxy_send_timeout ;
proxy_read_timeout ;
proxy_buffer_size 4k;
proxy_buffers 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

  配置nginx.conf

#运行nginx所在的用户名和用户组
#user www www; #启动进程数
worker_processes ;
#全局错误日志及PID文件
error_log /usr/local/nginx/logs/nginx_error.log crit; #pid /usr/local/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile ;
#工作模式及连接数上限
events
{
use epoll;
worker_connections ;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http
{
#设定mime类型
include mime.types;
default_type application/octet-stream;
include /usr/local/nginx/proxy.conf;
#charset gb2312;
#设定请求缓冲
server_names_hash_bucket_size ;
client_header_buffer_size 32k;
large_client_header_buffers 32k;
#client_max_body_size 8m; sendfile on;
tcp_nopush on; keepalive_timeout ; tcp_nodelay on; # fastcgi_connect_timeout ;
# fastcgi_send_timeout ;
# fastcgi_read_timeout ;
# fastcgi_buffer_size 64k;
# fastcgi_buffers 64k;
# fastcgi_busy_buffers_size 128k;
# fastcgi_temp_file_write_size 128k; # gzip on;
# gzip_min_length 1k;
# gzip_buffers 16k;
# gzip_http_version 1.0;
# gzip_comp_level ;
# gzip_types text/plain application/x-javascript text/css application/xml;
# gzip_vary on; #limit_zone crawler $binary_remote_addr 10m;
###禁止通过ip访问站点
#server{
# server_name _;
# return ;
#} server
{
listen ;
server_name localhost;
index index.html index.htm index.jsp;#设定访问的默认首页地址
root /home/www/web/ROOT;#设定网站的资源存放路径 #limit_conn crawler ; location / {
index index.html index.jsp;
} location ~ .*.jsp$ #所有jsp的页面均交由tomcat处理
{
index index.jsp;
proxy_pass http://192.168.222.128:8080;#转向tomcat处理
} location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #设定访问静态文件直接读取不经过tomcat
{
expires 30d;
} location ~ .*\.(js|css)?$
{
expires 1h;
} #定义访问日志的写入格式
#log_format access '$remote_addr - $remote_user [$time_local] "$request" '
#'$status $body_bytes_sent "$http_referer" '
#'"$http_user_agent" $http_x_forwarded_for';
#access_log /usr/local/nginx/logs/localhost.log access;#设定访问日志的存放路径 }
}

  检测配置文件

/usr/local/nginx/nginx -t
nginx: the configuration file /usr/local/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/nginx.conf test is successful

  平滑重启nginx

/usr/local/nginx/nginx -s reload

  /home/www/web/ROOT目录下创建test.html内容为“nginx static html”

  访问:http://IP/test.html

centos7最小版本安装nginx+tomcat+java+mysql运行环境

  此时访问的是nginx静态资源所在的目录

  http://IP/index.jsp

centos7最小版本安装nginx+tomcat+java+mysql运行环境

  

  此时html等静态文件访问的是nginx指定的静态文件目录,而jsp文件则分发到tomcat进行处理。

  • 服务自启动

  mysql自启动

./usr/lib/systemd/system目录下新建mysql.service
[Unit]
Description=mysql
After=syslog.target network.target remote-fs.target nss-lookup.target [Service]
Type=forking
#LimitNOFILE=max_open_files
PIDFile=/usr/local/mysql/mysql.pid
TimeoutSec=
PermissionsStartOnly=true
ExecStartPre=/usr/local/mysql/
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --user=mysql
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false
#Nice=nice_level
#LimitCore=core_file_limit
#Environment="LD_PRELOAD=/usr/local/mysql/lib"
Restart=on-failure
RestartPreventExitStatus= [Install]
WantedBy=multi-user.target

  允许mysql开机启动

systemctl enable mysql

  检查配置是否成功

systemctl is-enabled mysql

  mysql开启慢查询

/data/mysql/mysql3306/my.cnf中添加
# 慢日志
slow_query_log=
slow_query_log_file=/data/mysql/mysql3306/logs/localhost-slow.log
slow_launch_time=

  nginx和tomcat自启动(和mysql类似,不再阐述)