How to Deploy Magento 2 on CentOS 7 with ECS and ApsaraDB

Prerequisite

adduser magento
usermod -aG wheel magento
passwd magento

Install Apache with PHP 7

sudo yum -y install epel-release yum-utils nano
sudo rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum-config-manager --enable remi-php71
; Maximum amount of memory a script may consume (128MB)
; http://php.net/memory-limit
memory_limit = 512M
...[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone =Asia/Kolkata
#Listen 12.34.56.78:80
Listen 8080
sudo systemctl restart httpd
sudo systemctl enable httpd

Install Magento

For Production using Composer

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/bin/composer
sudo chmod +x /usr/bin/composer
cd /var/www
sudo composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition magento2
sudo usermod -g apache magento
cd /var/www/magento2 && sudo find var vendor pub/static pub/media app/etc -type f -exec chmod g+w {} \; && sudo find var vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} \; && sudo chown -R magento:apache . && sudo chmod u+x bin/magento
sudo chown -R apache: generated && sudo chmod 775 generated

For Production using Installer Archive

sudo yum -y install unzip
sudo /home/magento/Magento-CE*.zip -d /var/www/magento2
sudo usermod -g apache magento
cd /var/www/magento2 && sudo find var vendor pub/static pub/media app/etc -type f -exec chmod g+w {} \; && sudo find var vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} \; && sudo chown -R magento:apache . && sudo chmod u+x bin/magento
sudo chown -R apache: generated && sudo chmod 775 generated

For Development using Git

sudo yum -y install git
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/bin/composer
sudo chmod +x /usr/bin/composer
cd /var/www
sudo git clone https://github.com/magento/magento2.git
sudo chown -R magento:magento /var/www/magento2
cd magento2
git checkout $(git tag -l | grep -v 'rc[0-9]*$' | sort -V | tail -n 1)
sudo usermod -g apache magento
cd /var/www/magento2 && sudo find var vendor pub/static pub/media app/etc -type f -exec chmod g+w {} \; && sudo find var vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} \; && sudo chown -R magento:apache . && sudo chmod u+x bin/magento
sudo chown -R apache: generated && sudo chmod 775 generated

Install Varnish Cache

VARNISH_LISTEN_PORT=80...# Backend storage specification, see Storage Types in the varnishd(5)
# man page for details.
VARNISH_STORAGE="malloc,256M"
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sudo systemctl restart varnish
sudo systemctl enable varnish
[magento@iZt4nh1b7ra871iww33mpqZ ~]$ curl -I http://127.0.0.1/
HTTP/1.1 403 Forbidden
Date: Sat, 02 Dec 2017 15:27:14 GMT
Server: Apache/2.4.6 (CentOS) PHP/7.1.12
Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT
ETag: "1321-5058a1e728280"
Accept-Ranges: bytes
Content-Length: 4897
Content-Type: text/html; charset=UTF-8
X-Varnish: 32770
Age: 0
Via: 1.1 varnish (Varnish/5.2)
Connection: keep-alive
<VirtualHost *:8080>
ServerName shop.example.com
DocumentRoot /var/www/magento2
<Directory /var/www/magento2>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
sudo systemctl restart httpd
sudo systemctl restart varnish

Setting up MySQL database on ApsaraDB RDS instance

Browser-based installation

Final Configurations

Enable Varnish Cache in Magento

Adding Cron Jobs

crontab -l > /var/www/magento2/cron
cat <<EOF > /var/www/magento2/cron
* * * * * /usr/bin/php /var/www/magento2/bin/magento cron:run | grep -v "Ran jobs by schedule" >> /var/www/magento2/var/log/magento.cron.log
* * * * * /usr/bin/php /var/www/magento2/update/cron.php >> /var/www/magento2/var/log/update.cron.log
* * * * * /usr/bin/php /var/www/magento2/bin/magento setup:cron:run >> /var/www/magento2/var/log/setup.cron.log
EOF
crontab /var/www/magento2/cron

Enable Production Mode

cd /var/www/magento2
bin/magento deploy:mode:set production

Conclusion

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Restic with GNOME Keyring

CSS Link Style 🎨

What is API and some common APIs

Exploit more memory than allocated with malloc(), why?

HackerRank Tests: Python

3 Useful Concepts in Domain Driven Design

Integrating Huawei Core Push Kit

How to set up HTTPS server

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Connect to Redis in Go

Understanding Different Exchanges of RabbitMQ

How To Store And Retrieve RedisJSON on Redis Cloud using Node.js

[Elasticsearch] Working with disjunction max query - dis_max