Overview
Mis Compras uses different environment configurations for its PHP and Node.js backends. This guide covers the essential environment variables and configuration settings required for each component.
PHP Backend Configuration
The PHP backend uses a conexion.php file for database configuration instead of traditional .env files.
Database Connection File
Create or modify conexion.php in your PHP root directory:
<? php
$servername = "127.0.0.1" ;
$username = "root" ;
$password = "" ;
$database = "tienda_online" ;
$port = 522 ;
// Crear conexión
$conn = new mysqli ( $servername , $username , $password , $database , $port );
// Verificar conexión
if ( $conn -> connect_error ) {
die ( "Error de conexión: " . $conn -> connect_error );
}
?>
PHP Configuration Variables
Variable Default Value Description $servername127.0.0.1MySQL server host address $usernamerootDatabase user with access privileges $password""Database password (empty for development) $databasetienda_onlineTarget database name $port522MySQL server port
The default port 522 is non-standard. Most MySQL installations use port 3306. Verify your MySQL configuration and adjust accordingly.
PHP Environment Setup
Configure Database Connection
Update conexion.php with your local MySQL credentials: $servername = "127.0.0.1" ; // or "localhost"
$username = "your_db_user" ;
$password = "your_db_password" ;
$database = "tienda_online" ;
$port = 3306 ; // standard MySQL port
Verify PHP Configuration
Check your php.ini settings for MySQL support: extension =mysqli
extension =pdo_mysql
Test Connection
Create a test file to verify database connectivity: <? php
include 'conexion.php' ;
if ( $conn -> ping ()) {
echo "Database connection successful!" ;
} else {
echo "Database connection failed: " . $conn -> error ;
}
$conn -> close ();
?>
Node.js Backend Configuration
The Node.js backend uses db.js for database configuration and server.js for application settings.
Database Configuration
The db.js file configures the MySQL connection pool:
import mysql from "mysql2/promise" ;
export const db = await mysql . createPool ({
host: "localhost" ,
user: "root" ,
password: "" ,
database: "tienda_online" ,
waitForConnections: true ,
connectionLimit: 10 ,
queueLimit: 0 ,
});
Server Configuration
The server.js file defines the application port:
const PORT = 4000 ; // Backend server port
Node.js Configuration Variables
Variable Default Value Description hostlocalhostMySQL server hostname userrootDatabase username password""Database password databasetienda_onlineDatabase name connectionLimit10Maximum concurrent connections PORT4000Express server port
Creating a .env File (Recommended)
For better security and flexibility, create a .env file in the bakend/ directory:
.env (Development)
.env (Production)
# Database Configuration
DB_HOST = localhost
DB_USER = root
DB_PASSWORD =
DB_NAME = tienda_online
DB_CONNECTION_LIMIT = 10
# Server Configuration
PORT = 4000
NODE_ENV = development
# CORS Settings
CORS_ORIGIN = http://localhost:3000
Updating Node.js Configuration to Use .env
Install the dotenv package:
Update db.js to use environment variables:
import mysql from "mysql2/promise" ;
import dotenv from "dotenv" ;
dotenv . config ();
export const db = await mysql . createPool ({
host: process . env . DB_HOST || "localhost" ,
user: process . env . DB_USER || "root" ,
password: process . env . DB_PASSWORD || "" ,
database: process . env . DB_NAME || "tienda_online" ,
waitForConnections: true ,
connectionLimit: parseInt ( process . env . DB_CONNECTION_LIMIT ) || 10 ,
queueLimit: 0 ,
});
Update server.js:
import dotenv from "dotenv" ;
dotenv . config ();
const PORT = process . env . PORT || 4000 ;
Security Best Practices
Never commit sensitive credentials to version control. Always use .env files and add them to .gitignore.
.gitignore Configuration
Add these lines to your .gitignore:
# Environment files
.env
.env.local
.env.production
# PHP configuration
conexion.php
# Backup the template
!conexion.example.php
!.env.example
Creating Template Files
Create example configuration files for team members:
conexion.example.php
.env.example
<? php
$servername = "127.0.0.1" ;
$username = "your_username" ;
$password = "your_password" ;
$database = "tienda_online" ;
$port = 3306 ;
$conn = new mysqli ( $servername , $username , $password , $database , $port );
if ( $conn -> connect_error ) {
die ( "Error de conexión: " . $conn -> connect_error );
}
?>
Production Configuration
PHP Production Settings
<? php
$servername = getenv ( 'DB_HOST' ) ?: '127.0.0.1' ;
$username = getenv ( 'DB_USER' ) ?: 'root' ;
$password = getenv ( 'DB_PASSWORD' ) ?: '' ;
$database = getenv ( 'DB_NAME' ) ?: 'tienda_online' ;
$port = getenv ( 'DB_PORT' ) ?: 3306 ;
// Enable error logging instead of displaying
mysqli_report ( MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT );
try {
$conn = new mysqli ( $servername , $username , $password , $database , $port );
$conn -> set_charset ( "utf8mb4" );
} catch ( Exception $e ) {
error_log ( "Database connection failed: " . $e -> getMessage ());
die ( "Database connection error" );
}
?>
Node.js Production Settings
In production, use environment variables provided by your hosting platform (Heroku, AWS, DigitalOcean, etc.).
Update CORS configuration for production:
import cors from "cors" ;
const corsOptions = {
origin: process . env . CORS_ORIGIN || "http://localhost:3000" ,
credentials: true ,
optionsSuccessStatus: 200
};
app . use ( cors ( corsOptions ));
Environment-Specific Configuration
Development
Use localhost for database host
Empty or simple passwords acceptable
Detailed error messages enabled
CORS allows all origins
Staging
Use staging database credentials
Moderate security measures
Error logging enabled
CORS restricted to staging domains
Production
Use production database with strong passwords
Maximum security measures
Error logging to files (not displayed)
CORS restricted to production domain only
SSL/TLS connections enforced
Troubleshooting
Common Issues
Port Conflicts
# Check if port is already in use
lsof -i :4000 # Node.js
lsof -i :522 # PHP MySQL
Connection Refused
Verify MySQL is running
Check firewall settings
Confirm port numbers match MySQL configuration
Authentication Failed
Verify username and password
Check MySQL user privileges
Ensure password is properly escaped
Next Steps
After configuring environment variables:
Set up the PHP backend
Set up the Node.js backend
Test database connectivity