centos7最小版本安装nginx+tomcat+java+mysql运行环境
最近项目从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.7.0_79 /usr/local/ jdk1.7.0_79
添加到环境变量
root@localhost java]# vi /etc/profile # 在export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL下添加export JAVA_HOME=/usr/local/jdk1.7.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.7.0_79]# java -version java version "1.7.0_79" Java(TM) SE Runtime Environment (build 1.7.0_79-b15) Java HotSpot(TM) 64-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.68.tar.gz复制:
cp -R ./apache-tomcat-7.0.68 /usr/local
cd /usr/local/apache-tomcat-7.0.68修改权限:
chown -R tomcat:tomcat apache-tomcat-7.0.68
启动:
sh ./bin/startup.sh
将8080端口添加到防火墙例外并重启
firewall-cmd --zone=public --add-port=8080/tcp --permanent firewall-cmd --reload
访问:http://IP:8080/,熟悉的感觉
- 安装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.11-linux-glibc2.5-x86_64.tar.gz mv mysql-5.7.11-linux-glibc2.5-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.cnfvi /data/mysql/mysql3306/my.cnf
修改
# 指定data目录datadir=/data/mysql/mysql3306/data/
指定监听端口
port=3306
# 表名忽略大小写lower_case_table_names=1
初始化mysql,(注意获取初始化密码),命令安装也可参考:centos7 安装mysql5.7.11注意事项
[root@localhost mysql]# bin/mysqld --defaults-file=/data/mysql/mysql3306/my.cnf --initialize 2016-02-26T10:28:43.821897Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-02-26T10:28:43.822126Z 0 [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. 2016-02-26T10:28:43.822138Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set. 2016-02-26T10:28:44.311120Z 0 [Warning] InnoDB: New log files created, LSN=45790 2016-02-26T10:28:44.411026Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2016-02-26T10:28:44.492963Z 0 [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. 2016-02-26T10:28:44.495657Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2016-02-26T10:28:44.511137Z 1 [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.12.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=80/tcp --permanent firewall-cmd --reload
启动nginx
/usr/local/nginx/nginx
访问:http://IP/,熟悉的味道
- Nginx和tomcat整合
tomcat配置
###### Tomcat配置 # 创建tomcat项目默认目录 mkdir /home/www/web/jsp/ # 配置tomcat默认项目 # ./conf/server.xml中的Host节点中添加 <Context path="" debug="0" docBase="/home/www/web/jsp/" reloadable="true"></Context> # 重启tomcat /usr/local/apache-tomcat-7.0.68/bin/shutdown.sh /usr/local/apache-tomcat-7.0.68/bin/startup.sh # 创建index.jsp内容为“tomcat web project index page”
http://IP:8080/
创建test.html,内容为“tomcat test html”
http://192.168.222.128:8080/test.html
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; #获取真实ipproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #获取代理者的真实ip
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
配置nginx.conf
#运行nginx所在的用户名和用户组
#user www www;
启动进程数
worker_processes 8;
全局错误日志及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 65535;
工作模式及连接数上限
events
{
use epoll;
worker_connections 65535;
}
设定http服务器,利用它的反向代理功能提供负载均衡支持
http
{
#设定mime类型
include mime.types;
default_type application/octet-stream;
include /usr/local/nginx/proxy.conf;
#charset gb2312;
#设定请求缓冲
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
#client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout </span><span style="color: #800080;">60</span><span style="color: #000000;">;
tcp_nodelay on;
# fastcgi_connect_timeout </span><span style="color: #800080;">300</span><span style="color: #000000;">;
# fastcgi_send_timeout </span><span style="color: #800080;">300</span><span style="color: #000000;">;
# fastcgi_read_timeout </span><span style="color: #800080;">300</span><span style="color: #000000;">;
# fastcgi_buffer_size 64k;
# fastcgi_buffers </span><span style="color: #800080;">4</span><span style="color: #000000;"> 64k;
# fastcgi_busy_buffers_size 128k;
# fastcgi_temp_file_write_size 128k;
# </span><span style="color: #0000ff;">gzip</span><span style="color: #000000;"> on;
# gzip_min_length 1k;
# gzip_buffers </span><span style="color: #800080;">4</span><span style="color: #000000;"> 16k;
# gzip_http_version </span><span style="color: #800080;">1.0</span><span style="color: #000000;">;
# gzip_comp_level </span><span style="color: #800080;">2</span><span style="color: #000000;">;
# gzip_types text</span>/plain application/x-javascript text/css application/<span style="color: #000000;">xml;
# gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
###禁止通过ip访问站点
#server{
# server_name _;
# return </span><span style="color: #800080;">404</span><span style="color: #000000;">;
#}
server
{
listen </span><span style="color: #800080;">80</span><span style="color: #000000;">;
server_name localhost;
index index.html index.htm index.jsp;#设定访问的默认首页地址
root </span>/home/www/web/<span style="color: #000000;">ROOT;#设定网站的资源存放路径
#limit_conn crawler </span><span style="color: #800080;">20</span><span style="color: #000000;">;
location </span>/<span style="color: #000000;"> {
index index.html index.jsp;
}
location </span>~ .*<span style="color: #000000;">.jsp$ #所有jsp的页面均交由tomcat处理
{
index index.jsp;
proxy_pass http:</span><span style="color: #008000;">//</span><span style="color: #008000;">192.168.222.128:8080;#转向tomcat处理</span>
}
location </span>~ .*\.(gif|jpg|jpeg|png|bmp|<span style="color: #000000;">swf)$ #设定访问静态文件直接读取不经过tomcat
{
expires 30d;
}
location </span>~ .*\.(js|css)?<span style="color: #000000;">$
{
expires 1h;
}
#定义访问日志的写入格式
#log_format access </span><span style="color: #800000;">'</span><span style="color: #800000;">$remote_addr - $remote_user [$time_local] "$request" </span><span style="color: #800000;">'</span><span style="color: #000000;">
#</span><span style="color: #800000;">'</span><span style="color: #800000;">$status $body_bytes_sent "$http_referer" </span><span style="color: #800000;">'</span><span style="color: #000000;">
#</span><span style="color: #800000;">'</span><span style="color: #800000;">"$http_user_agent" $http_x_forwarded_for</span><span style="color: #800000;">'</span><span style="color: #000000;">;
#access_log </span>/usr/local/nginx/logs/<span style="color: #000000;">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
此时访问的是nginx静态资源所在的目录
http://IP/index.jsp
此时html等静态文件访问的是nginx指定的静态文件目录,而jsp文件则分发到tomcat进行处理。
- 服务自启动
mysql自启动
1./usr/lib/systemd/system目录下新建mysql.service [Unit] Description=mysql After=syslog.target network.target remote-fs.target nss-lookup.target[Service]
Type=forkingLimitNOFILE=max_open_files
PIDFile=/usr/local/mysql/mysql.pid
TimeoutSec=0
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=falseNice=nice_level
LimitCore=core_file_limit
Environment="LD_PRELOAD=/usr/local/mysql/lib"
Restart=on-failure
RestartPreventExitStatus=1[Install]
WantedBy=multi-user.target
允许mysql开机启动
systemctl enable mysql
检查配置是否成功
systemctl is-enabled mysql
mysql开启慢查询
/data/mysql/mysql3306/my.cnf中添加 # 慢日志 slow_query_log=1 slow_query_log_file=/data/mysql/mysql3306/logs/localhost-slow.log slow_launch_time=1
nginx和tomcat自启动(和mysql类似,不再阐述)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步