Setting Up an AWS Ubuntu Server for Magento 2 from Scratch
Ubuntu with Nginx is a solid base for Magento 2 on AWS. This walkthrough covers every layer, from the web server to the final install command.
1. Nginx and MySQL
sudo apt update sudo apt install nginx sudo apt install mysql-server
2. Secure MySQL and create a database user
sudo mysql SELECT user,authentication_string,plugin,host FROM mysql.user; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YOUR PASSWORD HERE'; FLUSH PRIVILEGES; SELECT user,authentication_string,plugin,host FROM mysql.user;
3. PHP-FPM and extensions
Match the PHP version to your Magento release (for example PHP 8.2 or 8.3 for current 2.4.x versions):
sudo add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get install awscli php7.4-fpm php7.4-mysql php7.4-bcmath php7.4-curl php7.4-gd php7.4-common php7.4-intl php7.4-mbstring php7.4-soap php7.4-xsl php7.4-zip php7.4-xml
4. A deployment user in the web server group
sudo adduser apacheuser sudo passwd apacheuser sudo usermod -g www-data apacheuser sudo usermod -aG sudo,adm,root,ubuntu apacheuser sudo chsh apacheuser -s /bin/bash
5. SFTP access for that user
sudo nano /etc/ssh/sshd_config comment line PasswordAuthentication no sudo /etc/init.d/ssh restart
Prefer SSH keys over passwords where possible.
6. Composer
cd ~ sudo curl -sS https://getcomposer.org/installer | sudo php sudo mv composer.phar /usr/local/bin/composer sudo ln -s /usr/local/bin/composer /usr/bin/composer
7. Search engine
Magento 2.4 needs Elasticsearch or OpenSearch. Elasticsearch:
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo gpg --dearmor -o /usr/share/keyrings/elasticsearch-keyring.gpg sudo apt-get install apt-transport-https echo "deb [signed-by=/usr/share/keyrings/elasticsearch-keyring.gpg] https://artifacts.elastic.co/packages/8.x/apt stable main" | sudo tee /etc/apt/sources.list.d/elastic-8.x.list sudo apt-get update && sudo apt-get install elasticsearch sudo service elasticsearch start // only if needed to reset password cd /usr/share/elasticsearch bin/elasticsearch-reset-password -u elastic //test elasticsearch curl -X GET --user user:password "http://localhost:9200/_cluster/health?pretty"
Or OpenSearch:
curl -o- https://artifacts.opensearch.org/publickeys/opensearch.pgp | sudo apt-key add - echo "deb https://artifacts.opensearch.org/releases/bundle/opensearch/2.x/apt stable main" | sudo tee /etc/apt/sources.list.d/opensearch-2.x.list sudo apt update sudo apt list -a opensearch sudo apt -y install opensearch=2.5.0 sudo systemctl enable opensearch sudo systemctl start opensearch Check opensearch port. curl -X GET https://localhost:9200 -u 'admin:admin' --insecure curl -X GET https://localhost:9200/_cat/plugins?v -u 'admin:admin' --insecure
8. Download Magento
composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition
9. Virtual host and ownership
sudo chown -R apacheuser:www-data /var/www/ sudo chown -R apacheuser:www-data /etc/nginx/sites-available/ sudo nano /etc/nginx/sites-available/sitename sudo ln -s /etc/nginx/sites-available/sitename /etc/nginx/sites-enabled/ sudo service nginx restart
10. Free SSL
sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d domain.com
11. Install Magento
bin/magento setup:install \ --base-url=https://domain.com/ \ --db-host=localhost \ --db-name=dbname \ --db-user=dbuser \ --db-password=dbpassword \ --admin-firstname=brush \ --admin-lastname=brush \ --admin-email=admin@example.com \ --admin-user=brush \ --admin-password=password \ --language=en_US \ --currency=USD \ --timezone=America/Chicago \ --use-rewrites=1 \ --elasticsearch-host=localhost \ --elasticsearch-port=9200 \ --elasticsearch-enable-auth=1 \ --elasticsearch-username=elastic \ --elasticsearch-password="password" // incase of opensearch bin/magento setup:install \ --base-url=https://domain.com/ \ --db-host=localhost \ --db-name=dbname \ --db-user=dbuser \ --db-password=dbpassword \ --admin-firstname=brush \ --admin-lastname=brush \ --admin-email=admin@example.com \ --admin-user=brush \ --admin-password=password \ --language=en_US \ --currency=USD \ --timezone=America/Chicago \ --use-rewrites=1 \ --search-engine=opensearch \ --opensearch-host=localhost:9200 \ --opensearch-port=9200 \ --opensearch-enable-auth=1 \ --opensearch-username=admin \ --opensearch-password=admin
Finish by setting up cron, Redis, Varnish and backups before the store takes real orders.
Need a second pair of eyes on this? Our team handles Magento development every week. See how we can help.