Cài đặt Magento với Nginx – Magento là một ứng dụng web thương mại điện tử mã nguồn mở rất phổ biến và có tính năng phong phú. Magento đáp ứng các yêu cầu của người dùng và cho phép bạn tạo một cửa hàng trực tuyến đầy đủ chức năng chỉ trong vài phút. Trong bài viết này, HOSTVN sẽ hướng dẫn các bạn cách cài đặt Magento trên VPS CentOS 7 với máy chủ web Nginx, PHP-FPM và MariaDB.
Cài đặt Magento với Nginx trên CentOS 7
1. Bước 1: Cài đặt LEMP (Nginx – MariaDB – PHP)
Để cài đặt LEMP các bạn làm theo hướng dẫn sau: Hướng dẫn cách cài đặt LEMP trên Centos 7
2. Bước 2: Tạo database
Để Magento có thể hoạt động, việc đầu tiên cần làm chính là tạo Database cho nó. Để làm điều này các bạn sử dụng lần lượt các lệnh dưới đây
mysql -u root -p
CREATE DATABASE magentodb; GRANT ALL PRIVILEGES ON magentodb . * TO magentouser@'localhost' IDENTIFIED BY 'YOUR-PASSWORD' WITH GRANT OPTION; FLUSH PRIVILEGES; exit;
3. Bước 3: Download Magento và cài đặt
- Tạo thư mục chứa code
Các bạn có thể tạo thư mục chứa code Magento bằng lệnh sau
mkdir -p /home/magentodomain.com/public_html
- Cài đặt magento
cd /home/magentodomain.com/public_html wget https://github.com/magento/magento2/archive/2.3.tar.gz tar -xvf 2.3.tar.gz
Di chuyển mã nguồn đã được giải nén ra public_html
mv magento2-2.3/{.,}* /home/magentodomain.com/public_html
Nhập A và nhấn Enter khi được hỏi
Xóa các thư mục không cần thiết
rm -rf 2.3.tar.gz magento2-2.3
Tiến hành cài đặt các thư viện cần thiết với composer. Nếu VPS của bạn chưa được cài đặt composer hãy xem Hướng dẫn cách cài đặt Composer trên CentOS 7.
composer install
Sau khi quá trình cài đặt thư viện hoàn tất các bạn tiến hành cài đặt Magento bằng lệnh sau
bin/magento setup:install --backend-frontname="adminlogin" \ --key="biY8vdWx4w8KV5Q59380Fejy36l6ssUb" \ --db-host="localhost" \ --db-name="magentodb" \ --db-user="magentouser" \ --db-password="MYSQL-PASSWORD" \ --language="en_US" \ --currency="USD" \ --timezone="Asia/Ho_Chi_Minh" \ --use-rewrites=1 \ --use-secure=0 \ --base-url="http://magentodomain.com" \ --base-url-secure="https://magentodomain.com" \ --admin-user=adminuser \ --admin-password=STRONG-PASSWORD \ --admin-email=admin@newmagento.com \ --admin-firstname=admin \ --admin-lastname=user \ --cleanup-database
Các bạn cần thay đổi các thông tin trong lệnh như sau:
- –backend-frontname: Link đăng nhập admin các bạn muốn (Ví dụ: https://magentodomain.com/adminlogin)
- key: Thay bằng một chuỗi ký tự bất kỳ
- db-name, db-user, db-password: Thay bằng các thông tin các bạn đã tạo ở Bước 2: Tạo database
- magentodomain.com : Thay bằng domain của các bạn
- admin-user, admin-password: Thay bằng thông tin đăng nhập admin các bạn muốn tạo. Lưu ý mật khẩu phải trên 8 ký tự bao gồm chữ hoa, chữ thường và số
- admin-email, admin-firstname, admin-lastname: Thay thế bằng thông tin của các bạn
Sau khi quá trình cài đặt hoàn tất các bạn tiến hành set owner để tránh các lỗi về phân quyền
chown -R nginx:nginx /home/magentodomain.com/public_html
4. Bước 4: Tạo Virtualhost
Tạo file /etc/nginx/conf.d/magentodomain.com.conf
nano /etc/nginx/conf.d/magentodomain.com.conf
Dán nội dung dưới đây vào
upstream fastcgi_backend { server unix:/var/run/php-fpm.sock; } server { listen 80; server_name www.magentoodomain.com magentodomain.com; set $MAGE_ROOT /home/magentodomain.com/public_html; set $MAGE_DEBUG_SHOW_ARGS 1; root $MAGE_ROOT/pub; index index.html index.htm index.php; autoindex off; charset UTF-8; error_page 404 403 = /errors/404.php; #add_header "X-UA-Compatible" "IE=Edge"; # Deny access to sensitive files location /.user.ini { deny all; } # PHP entry point for setup application location ~* ^/setup($|/) { root $MAGE_ROOT; location ~ ^/setup/index.php { fastcgi_pass fastcgi_backend; fastcgi_param PHP_FLAG "session.auto_start=off n suhosin.session.cryptua=off"; fastcgi_param PHP_VALUE "memory_limit=756M n max_execution_time=600"; fastcgi_read_timeout 600s; fastcgi_connect_timeout 600s; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ ^/setup/(?!pub/). { deny all; } location ~ ^/setup/pub/ { add_header X-Frame-Options "SAMEORIGIN"; } } # PHP entry point for update application location ~* ^/update($|/) { root $MAGE_ROOT; location ~ ^/update/index.php { fastcgi_split_path_info ^(/update/index.php)(/.+)$; fastcgi_pass fastcgi_backend; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; include fastcgi_params; } # Deny everything but index.php location ~ ^/update/(?!pub/). { deny all; } location ~ ^/update/pub/ { add_header X-Frame-Options "SAMEORIGIN"; } } location / { try_files $uri $uri/ /index.php$is_args$args; } location /pub/ { location ~ ^/pub/media/(downloadable|customer|import|custom_options|theme_customization/.*.xml) { deny all; } alias $MAGE_ROOT/pub/; add_header X-Frame-Options "SAMEORIGIN"; } location /static/ { # Uncomment the following line in production mode # expires max; # Remove signature of the static files that is used to overcome the browser cache location ~ ^/static/version { rewrite ^/static/(versiond*/)?(.*)$ /static/$2 last; } location ~* .(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2|html|json)$ { add_header Cache-Control "public"; add_header X-Frame-Options "SAMEORIGIN"; expires +1y; if (!-f $request_filename) { rewrite ^/static/(versiond*/)?(.*)$ /static.php?resource=$2 last; } } location ~* .(zip|gz|gzip|bz2|csv|xml)$ { add_header Cache-Control "no-store"; add_header X-Frame-Options "SAMEORIGIN"; expires off; if (!-f $request_filename) { rewrite ^/static/(versiond*/)?(.*)$ /static.php?resource=$2 last; } } if (!-f $request_filename) { rewrite ^/static/(versiond*/)?(.*)$ /static.php?resource=$2 last; } add_header X-Frame-Options "SAMEORIGIN"; } location /media/ { try_files $uri $uri/ /get.php$is_args$args; location ~ ^/media/theme_customization/.*.xml { deny all; } location ~* .(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ { add_header Cache-Control "public"; add_header X-Frame-Options "SAMEORIGIN"; expires +1y; try_files $uri $uri/ /get.php$is_args$args; } location ~* .(zip|gz|gzip|bz2|csv|xml)$ { add_header Cache-Control "no-store"; add_header X-Frame-Options "SAMEORIGIN"; expires off; try_files $uri $uri/ /get.php$is_args$args; } add_header X-Frame-Options "SAMEORIGIN"; } location /media/customer/ { deny all; } location /media/downloadable/ { deny all; } location /media/import/ { deny all; } location /media/custom_options/ { deny all; } location /errors/ { location ~* .xml$ { deny all; } } # PHP entry point for main application location ~ ^/(index|get|static|errors/report|errors/404|errors/503|health_check).php$ { try_files $uri =404; fastcgi_pass fastcgi_backend; fastcgi_buffers 1024 4k; fastcgi_param PHP_FLAG "session.auto_start=off n suhosin.session.cryptua=off"; fastcgi_param PHP_VALUE "memory_limit=756M n max_execution_time=18000"; fastcgi_read_timeout 600s; fastcgi_connect_timeout 600s; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # Banned locations (only reached if the earlier PHP entry point regexes don't match) location ~* (.php$|.phtml$|.htaccess$|.git) { deny all; } }
Bấm Ctrl + o và nhấn Enter để lưu file, Ctrl + x để thoát khỏi nano. Khởi động lại Nginx để load cấu hình
service nginx restart
5. Kiếm tra
Sau khi hoàn tất các bạn có thể truy cập tên miền của mình bằng trình duyệt web để kiểm tra.
6. Kết luận
Qua bài viết này HOSTVN đã hướng dẫn các bạn cách cài đặt Magento trên VPS CentOS 7 với máy chủ web Nginx, PHP-FPM và MariaDB. Nếu có bất kỳ ý kiến đóng góp nào các bạn có thể để lại bình luận ở bên dưới. Ngoài ra các bạn có thể xem thêm Hướng dẫn cài đặt Laravel trên CentOS 7.