赞
踩
要在Linux系统中安装和配置Nginx以及管理Tomcat服务,可以按照以下步骤进行操作:
更新系统包索引:
- sudo apt update # Debian/Ubuntu
- sudo yum update # CentOS/RHEL
- sudo dnf update # Fedora
安装Nginx:
- sudo apt install nginx # Debian/Ubuntu
- sudo yum install nginx # CentOS/RHEL
- sudo dnf install nginx # Fedora
启动Nginx服务:
sudo systemctl start nginx
设置Nginx开机自启:
sudo systemctl enable nginx
确认Nginx正在运行:
sudo systemctl status nginx
此时,Nginx应该已经安装并开始运行,在浏览器访问服务器的公网IP或者域名,应该可以看到Nginx的默认欢迎页面。
安装Java(Tomcat需要Java运行环境):
- sudo apt install default-jdk # Debian/Ubuntu
- sudo yum install java-1.8.0-openjdk # CentOS/RHEL
- sudo dnf install java-1.8.0-openjdk # Fedora
添加Tomcat用户(出于安全性考虑,最好为Tomcat创建一个专用用户):
sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat
下载Tomcat(替换 x.y.z
为想安装的Tomcat版本号):
wget https://downloads.apache.org/tomcat/tomcat-9/vx.y.z/bin/apache-tomcat-x.y.z.tar.gz
解压缩Tomcat:
- sudo tar xf apache-tomcat-*.tar.gz -C /opt/tomcat
- sudo ln -s /opt/tomcat/apache-tomcat-x.y.z /opt/tomcat/latest
- sudo chown -R tomcat: /opt/tomcat
- sudo sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'
创建Systemd Unit文件 (/etc/systemd/system/tomcat.service
):
- [Unit]
- Description=Tomcat 9 servlet container
- After=network.target
-
- [Service]
- Type=forking
-
- User=tomcat
- Group=tomcat
-
- Environment="JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64"
- Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
- Environment="CATALINA_HOME=/opt/tomcat/latest"
- Environment="CATALINA_BASE=/opt/tomcat/latest"
- Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
-
- ExecStart=/opt/tomcat/latest/bin/startup.sh
- ExecStop=/opt/tomcat/latest/bin/shutdown.sh
-
- [Install]
- WantedBy=multi-user.target
注意:JAVA_HOME
路径根据实际Java安装位置确定。
启动Tomcat:
- sudo systemctl daemon-reload
- sudo systemctl start tomcat
- sudo systemctl enable tomcat
验证Tomcat是否正在运行:
sudo systemctl status tomcat
备份原始Nginx配置文件(建议操作):
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup
编辑Nginx配置文件 /etc/nginx/sites-available/default
(Debian/Ubuntu) 或在 /etc/nginx/nginx.conf
中加入以下内容(CentOS/RHEL/Fedora):
- server {
- listen 80;
-
- location / {
- proxy_pass http://localhost:8080;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto $scheme;
- }
- }
重启Nginx以应用配置:
sudo systemctl restart nginx
启动Tomcat服务:
sudo systemctl start tomcat
停止Tomcat服务:
sudo systemctl stop tomcat
重启Tomcat服务:
sudo systemctl restart tomcat
查看Tomcat服务状态:
sudo systemctl status tomcat
查看Tomcat日志(可能在不同安装中路径有所不同):
tail -f /opt/tomcat/latest/logs/catalina.out
遵循上述步骤可以在Linux系统上成功安装和配置Nginx,并管理Tomcat服务。这将实现一个稳定的环境,Nginx负责处理HTTP请求并将其代理到Tomcat服务器,后者负责运行Java应用程序。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。