You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CentOS 7.8下Web应用自定义非80端口配置方法咨询

Hey there! Let's walk through how to set this up on your CentOS 7.8 server—since you've got multiple apps needing different ports, using a proper web server like Nginx or Apache (with reverse proxy capabilities) is the way to go for production. I'll cover both options, plus a note on PHP if you need that later.

1. First: Open the Required Ports in Firewalld

Before anything else, make sure your server's firewall allows traffic on port 50502 (and 80, if it's not already open):

# Add port 50502 permanently
firewall-cmd --add-port=50502/tcp --permanent
# Reload firewall to apply changes
firewall-cmd --reload

If you haven't already opened port 80, run firewall-cmd --add-port=80/tcp --permanent before reloading.

Nginx is great for managing multiple apps on the same server. Here's how to configure it:

Step 1: Install Nginx (if not already installed)

yum install nginx -y
# Start and enable Nginx to run on boot
systemctl start nginx
systemctl enable nginx

Step 2: Create a Site Configuration File

Create a new config file for your app (replace /path/to/your/web/app with the actual directory where your HTML/PHP files live):

vi /etc/nginx/conf.d/your-custom-app.conf

Paste this configuration (adjust paths and server names as needed):

# Your app on port 50502
server {
    listen 50502;
    server_name www.example.com;

    # Path to your HTML/PHP files
    root /path/to/your/web/app;
    # Default index files (add index.php if you're using PHP)
    index index.html index.php;

    # Optional: Handle PHP requests (skip if you only have HTML)
    location ~ \.php$ {
        # Point to PHP-FPM's default port (9000)
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # Optional: Cache static files for better performance
    location ~* \.(html|css|js|png|jpg|gif)$ {
        expires 30d;
        add_header Cache-Control "public, no-transform";
    }
}

# Example: Another app running on port 80 (subdomain or same domain)
server {
    listen 80;
    server_name app.example.com; # Use a subdomain to distinguish apps

    root /path/to/your/other/app;
    index index.html;
}

Step 3: Test & Apply Configuration

# Check for config errors
nginx -t
# Restart Nginx if tests pass
systemctl restart nginx

Now you can access your app via www.example.com:50502, and the other app via app.example.com (no port needed, since it uses 80).

3. Alternative Setup: Apache HTTP Server

If you prefer Apache, here's how to configure multiple ports:

Step 1: Install Apache & PHP (if needed)

yum install httpd php php-fpm -y
# Start and enable Apache
systemctl start httpd
systemctl enable httpd
# Start PHP-FPM if using PHP
systemctl start php-fpm
systemctl enable php-fpm

Step 2: Add Port 50502 to Apache's Listen List

Edit the main Apache config file:

vi /etc/httpd/conf/httpd.conf

Find the line Listen 80 and add this below it:

Listen 50502

Step 3: Create Virtual Hosts

Create a new config file for your apps:

vi /etc/httpd/conf.d/your-apps.conf

Paste this configuration:

# Your app on port 50502
<VirtualHost *:50502>
    ServerName www.example.com
    DocumentRoot "/path/to/your/web/app"

    # Allow access to the directory
    <Directory "/path/to/your/web/app">
        AllowOverride All
        Require all granted
    </Directory>

    # Handle PHP requests (skip if only HTML)
    <FilesMatch \.php$>
        SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>
</VirtualHost>

# Another app on port 80
<VirtualHost *:80>
    ServerName app.example.com
    DocumentRoot "/path/to/your/other/app"
</VirtualHost>

Step 4: Test & Restart Apache

# Check for config errors
httpd -t
# Restart Apache if tests pass
systemctl restart httpd

If you're just testing and want a quick way without a full web server, you can use PHP's built-in development server. Navigate to your app directory and run:

php -S 0.0.0.0:50502

This lets you access your app via www.example.com:50502, but never use this in production—it's not secure or performant enough for real traffic.

Important Final Checks

  • SELinux Permissions: If you get "403 Forbidden" errors, fix SELinux permissions for your app directory:
    chcon -R -t httpd_sys_content_t /path/to/your/web/app
    
  • Domain DNS: Make sure www.example.com and any other subdomains point to your server's public IP address.

内容的提问来源于stack exchange,提问作者Emmyz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:32:41