Deploy Magento 2
Install Magento Version 2.4.7 + Elasticsearch
Magento 2.4.7
Just deployed
/var/www
Elasticsearch
Just deployed
/esdata
Just deployed
/var/lib/mysql
Deploy and Host Magento 2 on Railway
Magento 2 is an open-source e-commerce platform built on PHP. It powers online stores with catalog management, checkout, multi-store support, and a rich ecosystem of extensions. This template deploys Magento 2.4 with Nginx and PHP-FPM, using MySQL and OpenSearch/Elasticsearch as separate Railway services.
About Hosting Magento 2
Hosting Magento 2 involves running a PHP application (Nginx + PHP-FPM), a MySQL-compatible database for catalog and orders, and a search engine (Elasticsearch/OpenSearch) for product search. This Railway setup uses a single Dockerfile that downloads the official Magento 2.4 release from GitHub, runs Composer install, and configures Nginx to listen on the port Railway provides. On first run, the entrypoint detects an empty database and runs setup:install automatically, then deploys static content and sets production mode. Optional sample data can be installed via an environment variable.
Common Use Cases
- E-commerce stores: Run a full Magento 2 storefront and admin with MySQL and search on Railway.
- Staging or demos: Spin up a complete Magento stack (web + MySQL + Elasticsearch) from a template for testing or demos.
- Headless or custom frontends: Use Magento as the backend (REST/GraphQL) while scaling the web service and database independently on Railway.
Dependencies for Magento 2 Hosting
- MySQL 8: Required for Magento 2.4; run as a separate Railway service (or use a MySQL template).
- Elasticsearch/OpenSearch: Required for catalog search; run as a separate Railway service (e.g. Railway Elasticsearch template) and reference its host/credentials from the Magento service.
- PHP 8.2: With extensions
bcmath,intl,pdo_mysql,soap,xsl,zip,gd,opcache,sockets,pcntl. - Nginx: Serves the Magento
pub/docroot and proxies PHP to PHP-FPM; port is set at runtime from Railway’sPORT.
Deployment Dependencies
- Magento 2 on GitHub — source for the core application (default tag:
2.4.7-p3). - [MySQL 8] — run MySQL as a separate service; use
${{MySQL.RAILWAY_PRIVATE_DOMAIN}}and${{MySQL.MYSQL_PASSWORD}}(or equivalent) so Magento connects to the same database. - [Railway Elasticsearch] — optional reference for running Elasticsearch as a sibling service with a volume and generated password.
Implementation Details
The app listens on the port Railway injects (PORT, default 8080). The entrypoint updates Nginx config and runs PHP-FPM in the background, then either copies Magento from the image into the volume (if empty) or skips. If the database has no Magento tables, it runs setup:install with MySQL and OpenSearch, then reindex, production mode, and static content deploy.
Required environment variables for the Magento service:
| Variable | Description |
|---|---|
MYSQL_HOST | MySQL server host |
MYSQL_PORT | MySQL port |
MYSQL_USER | MySQL user |
MYSQL_PASSWORD | MySQL password |
MYSQL_DATABASE | Database name |
ELASTICSEARCH_HOST | OpenSearch/Elasticsearch host (e.g. private domain of Elasticsearch service) |
ELASTICSEARCH_PORT | OpenSearch/Elasticsearch port (e.g. 9200) |
ELASTICSEARCH_PASSWORD | OpenSearch/Elasticsearch password (e.g. ${{Elasticsearch.ELASTIC_PASSWORD}}) |
ADMIN_PASSWORD | Magento admin password |
BASE_URL | Store base URL (e.g. https://your-app.up.railway.app/) |
ADMIN_EMAIL | Admin user email |
ADMIN_USER | Admin username |
Sample data: To install the Magento sample catalog (categories, products, media), set INSTALL_SAMPLE_DATA=true (or yes / 1) in the Magento service environment. When enabled, the entrypoint clones magento2-sample-data (branch 2.4), runs the sample data build, setup:upgrade, static content deploy, and copies sample media into pub/media. Omit or set to false for a clean install without sample data.
Health check: Nginx serves GET /health with 200 OK once the server is up (used by Railway for readiness).
Why Deploy Magento 2 on Railway?
Railway is a singular platform to deploy your infrastructure stack. Railway will host your infrastructure so you don't have to deal with configuration, while allowing you to vertically and horizontally scale it.
By deploying Magento 2 on Railway, you are one step closer to supporting a complete full-stack application with minimal burden. Host your servers, databases, AI agents, and more on Railway.
Template Content
Magento 2.4.7
feliperosenek/mangeto2-railwayADMIN_USER
Admin User Magento
ADMIN_EMAIL
Magento admin e-email
ADMIN_LASTNAME
Magento admin lastname
ADMIN_PASSWORD
Magento admin password
ADMIN_FIRSTNAME
Magento admin first name
Elasticsearch
feliperosenek/elasticsearch-railway