AWS基础部署咨询:Node.js/PHP应用及数据库环境配置需求
Hey there! As someone new to AWS, let's walk through your deployment needs clearly—no confusing jargon, just practical steps you can follow for testing, plus answers to your instance count and cPanel questions.
一、 Testing Stage: Basic Deployment Plan
1. Recommended Instance Count (Testing Environment)
For initial testing, 2 EC2 instances are more than enough:
- Instance 1: Host your Node.js (Express) app + PHP web/API apps. Testing-stage resource demands are low, so combining these saves costs while keeping things simple.
- Instance 2: Run MySQL database separately. Separating databases from apps is a best practice—even in testing, this mimics production architecture and avoids later migration headaches.
- If you're super budget-tight, you could temporarily put MySQL on the same instance as apps, but I don't recommend it. Performance conflicts and migration hassle aren't worth the short-term savings.
2. Step-by-Step Deployment for Each Component
Node.js (Express) Setup
- Pick an EC2 instance with Amazon Linux 2 or Ubuntu (official images are stable and well-supported).
- Log into the instance, then install Node.js:
# For Ubuntu sudo apt update && sudo apt install nodejs npm # For Amazon Linux 2 sudo yum install nodejs npm - Upload your Express code (use SCP or Git clone—whichever you're comfortable with).
- Install dependencies and start the app:
cd your-project-folder npm install node app.js # For testing, you can run this directly; use pm2 for process management later - Don't forget to open port 3000 (Express default) in your EC2 security group, or set up Nginx as a reverse proxy to use standard 80/443 ports.
MySQL Setup
- Use a separate EC2 instance (or Amazon RDS if you want managed database features, but EC2 is cheaper and more flexible for testing).
- Install MySQL:
# For CentOS sudo yum install mysql-server sudo systemctl start mysqld sudo systemctl enable mysqld # For Ubuntu sudo apt install mysql-server sudo systemctl start mysql sudo systemctl enable mysql - Run the security setup script to harden your database:
Set a root password, remove anonymous users, and disable remote root access for safety.sudo mysql_secure_installation - In your security group, open port 3306 only for your app instance's IP—never expose MySQL to the public internet.
- Create a database, a dedicated user, and grant that user access from your app instance's IP.
PHP Web/API Setup
- On your app instance, install a LAMP (Apache + MySQL + PHP) or LNMP (Nginx + MySQL + PHP) stack:
# LAMP for Ubuntu sudo apt install apache2 php libapache2-mod-php php-mysql # LAMP for CentOS sudo yum install httpd php php-mysqlnd sudo systemctl start httpd && sudo systemctl enable httpd - Upload your PHP code to the default web root:
/var/www/htmlfor Apache, or Nginx's configured root directory. - Set up virtual hosts if you have multiple PHP apps, and open ports 80/443 in your security group.
二、 CentOS + cPanel: Hosting MySQL & PHP Apps
cPanel is a great choice for beginners thanks to its visual interface. Here's how to set it up:
Launch an EC2 Instance
- Choose a CentOS 7 image (cPanel has the best compatibility with CentOS 7; avoid newer versions for now).
- Pick at least a t2.medium instance type—cPanel requires minimum resources, and t2.micro will likely struggle.
- Configure your security group to open these ports: 22 (SSH), 80 (HTTP), 443 (HTTPS), 2082 (cPanel HTTP login), 2083 (cPanel HTTPS login), and 3306 (MySQL—restrict to only necessary IPs).
Install cPanel
- Log into your instance, set a valid hostname first:
hostnamectl set-hostname your-domain.com # Replace with your actual domain - Disable firewall and SELinux (cPanel requires this for installation):
sudo systemctl stop firewalld && sudo systemctl disable firewalld sudo setenforce 0 sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config - Reboot the instance, log back in, and run the cPanel installation script:
cd /home && curl -o latest -L https://securedownloads.cpanel.net/latest && sh latest - Installation takes 30-60 minutes—grab a coffee while you wait!
- Log into your instance, set a valid hostname first:
Configure cPanel
- After installation, log in via
https://your-instance-public-ip:2083(or your domain if you've already set up DNS). - Set your cPanel admin password first.
- Host PHP Apps:
- Use the "File Manager" to upload your PHP code to the
public_htmldirectory. - Use the "Domains" tool to add main or subdomains, mapping them to your code directories.
- Use "PHP Version" to select the exact PHP version your apps require (e.g., 7.4, 8.0).
- Use the "File Manager" to upload your PHP code to the
- Host MySQL Databases:
- Use the "MySQL Databases" tool to create a database and a dedicated user.
- Assign full permissions to the user for the database, then note down the database name, username, and password for your PHP apps to connect.
- cPanel handles MySQL configuration automatically—just make sure your security group restricts 3306 access to trusted IPs.
- After installation, log in via
Bonus: Deploy Node.js on cPanel
- cPanel supports Node.js too! Use the "Setup Node.js App" tool to upload your Express code, specify your startup file (e.g.,
app.js), set a port, and launch the app. You can even configure a reverse proxy to map the app to a domain.
- cPanel supports Node.js too! Use the "Setup Node.js App" tool to upload your Express code, specify your startup file (e.g.,
三、 Migrating from Testing to Production
Once your testing environment is stable, here's how to scale to production:
- Replace your EC2 MySQL instance with Amazon RDS—it's a managed database service with automatic backups, high availability, and scaling options.
- Use Auto Scaling Groups + Elastic Load Balancer (ELB) for your app instances to handle traffic spikes and ensure uptime.
- Store static assets in S3 and use CloudFront as a CDN to speed up content delivery.
- Set up free SSL certificates with AWS Certificate Manager to secure all your apps.
内容的提问来源于stack exchange,提问作者Accrete InfoSolution Technolog




