> 文章列表 > 阿里云ubuntu安装nginx+apache+tomcat+mysql并配置反向代理

阿里云ubuntu安装nginx+apache+tomcat+mysql并配置反向代理

阿里云ubuntu安装nginx+apache+tomcat+mysql并配置反向代理

安装之前关闭防火墙ufw disable,阿里云上安全组端口全开,配置完后防火墙和安全组放通端口。
一、安装nginx
更新软件包

sudo apt-get update

安装nginx

sudo apt-get install nginx

启动nginx服务

sudo systemctl start nginx

检查nginx服务是否已经启动

sudo systemctl status nginx

安装完后访问出现welcome to nginx!安装成功
二、安装mysql
安装mysql最新版8

sudo apt update
sudo apt install mysql-server
systemctl status mysql(默认安装完成之后启动)
mysql -u root -p输入root用户密码后登录mysql

为了以后需要,这里让其可以远程登录
编辑/etc/mysql/mysql.conf.d/mysqld.cnf将bind_address = 127.0.0.1注释掉
创建用户,并为其授权

CREATE USER 'newuser'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON . TO 'newuser'@'%' WITH GRANT OPTION;

这将创建一个名为 newuser 的新用户,并将其密码设置为 password'%' 表示允许从任何 IP 地址登录 MySQL 服务器。如果您只想允许该用户从特定 IP 地址登录,则将 '%' 替换为该 IP 地址。WITH GRANT OPTION 表示该用户可以授予其他用户访问权限。
修改默认端口
刚才的mysql配置文件中将port前面的#去掉,然后修改端口为33060,配置之前先关闭防火墙,阿里云安全组端口全开。
使用数据库连接工具并使用新创建的用户名和密码连接查看是否成功!
三、安装tomcat
先安装Java环境

sudo apt install openjdk-11-jdk
java -version查看是否安装成功

下载tomcat安装包

https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.8/bin/apache-tomcat-10.1.8.tar.gz
tar -xzvf apache-tomcat-10.1.8.tar.gz

进入到bin目录下,./startup.sh即可启动,默认8080端口,在/conf目录下修改server.xml文件将connector port处的8080改为8088端口
四、安装apache

apt install apache2
systemctl status apache2(查看apache是否已经启动,默认启动)

查看是否安装成功时为了不影响可以先暂停nginx服务,但是apache2和Nginx的根目录都是/var/www/html,所以看到的页面都是apache 的默认页面,不过nginx的是加载不出ubuntu的logo的。
五、配置nginx作为反向代理服务器
配置nginx作为方向代理服务器,将请求代理到apache和tomcat上。
修改apache2的默认端口

/etc/apache2/ports.conf

修改监听端口为81,Listen 81,同时修改apache2的sites-available目录下的000-xxx文件里面的端口的为81
修改后重启生效

systemctl restart apache2

在/etc/nginx/conf.d/ 目录下的配置文件中配置这类负载均衡和反向代理规则。
创建如下配置文件:

/etc/nginx/conf.d/default.conf
server {listen       80;server_name  www.doamin.com;# 服务器地址或绑定域名location / { # 访问80端口后的所有路径都转发到 proxy_pass 配置的ip中root   /var/www/html;index  index.html index.htm;proxy_pass http://www.domain:81; # 配置反向代理的ip地址和端口号 【注:url地址需加上http:// 或 https://】}
}server {listen       8070;server_name  www.doamin.com;# 服务器地址或绑定域名location / { # 访问80端口后的所有路径都转发到 proxy_pass 配置的ip中root   /usr/local/tomcat/apache-tomcat-10.1.8/webapps;index  index.html index.htm index.jsp;proxy_pass http://www.domain.com:8088; # 配置反向代理的ip地址和端口号 【注:url地址需加上http:// 或 https://】}
}

此时访问IP:8070,nginx会将请求代理到8088的tomcat,访问IP:80,请求会代理到81的apache,可以使用ufw来测试是否正常,比如关闭8088,访问IP:8070异常,开启后又正常显示tomcat的默认页面。

配置好后
sudo nginx -s reload(可以检查配置是否正确,配置错误nginx也启动不起来)
service nginx restart

当然,这种配置需要80,81,8070,8088都暴露在公网上,后续可以想办法只暴露80,8070。

【附nginx反向代理配置】
有其他需求的时候可能用得到。


//安装nginx并查看版本apt install nginx
nginx -v//修改nginx默认的配置文件cd /etc/nginx/sites-enabled
sudo vim default//将default文件内容替换如下server{
listen 443 ssl http2;
server_name www.xxx.tk;
root /var/www/https;
index index.html;
ssl_certificate /opt/zs/server.pem;
ssl_certificate_key /opt/zs/server.key;
}server{
listen 80;
server_name www.xxx.tk xxx.tk;
return 301 https://www.xxx.tk;
}//创建https新文件cd /var/www
mkdir https
cd https
vim index.html(文件内容hello,I am is https!)
//重启nginx配置
sudo nginx -s reload
//重启nginx服务
sudo systemctl restart nginx.servicevim /etc/nginx/nginx.conf//配置文件内容如下user www-data;worker_processes auto;pid /run/nginx.pid;include /etc/nginx/modules-enabled/*.conf;events {worker_connections 768;# multi_accept on;}http {server{listen 443 ssl;server_name aaa;root /var/www/https;index index.html;ssl_certificate /opt/zs/server.pem;ssl_certificate_key /opt/zs/server.key;ssl_session_timeout 5m;ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;ssl_prefer_server_ciphers on;location /aaaaaaaaa { # 限定 c2profile 请求地址proxy_pass https://127.0.0.1:9090/aaaaaaaaa;expires off;proxy_redirect off;proxy_set_header X-Forworded-For $proxy_add_x_forwarded_for;proxy_set_header X-Real-IP $remote_addr;}location /bbbbbbbbb {proxy_pass https://127.0.0.1:9090/bbbbbbbbb;expires off;proxy_redirect off;proxy_set_header X-Forworded-For $proxy_add_x_forwarded_for;proxy_set_header X-Real-IP $remote_addr;}}}#mail {#   # See sample authentication script at:#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript##   # auth_http localhost/auth.php;#   # pop3_capabilities "TOP" "USER";#   # imap_capabilities "IMAP4rev1" "UIDPLUS";##   server {#       listen     localhost:110;#       protocol   pop3;#       proxy      on;#   }##   server {#       listen     localhost:143;#       protocol   imap;#       proxy      on;#   }#}//重启nginx配置sudo nginx -s reload//重启nginx服务sudo systemctl restart nginx.service

美味小吃