Magento 2部署笔记

环境

php 7.1
Mysql 5.7
AppNode

获取Magento密令(用于Composer安装)

设置、获取密钥,这里username、pw分别对应公、私密钥。

安装 Composer

Composer安装是可以用来升级和增加模块,是官方推荐的安装方式

  • 安装地址
  • AppNode内安装:网站设置/组件管理,PHP内的Composer安装

安装

修改安装目录下的权限

  • 先到<magento install directory>
    find var generated vendor pub/static pub/media app/etc -type f -exec chmod g+w {} +
    find var generated vendor pub/static pub/media app/etc -type d -exec chmod g+ws {} +
    chown -R :www-data . # Ubuntu
    chmod u+x bin/magento
  • 文件夹授权为网站环境(php、mysql)用户和用户组
    • 可用网站管理-对应网站的操作箭头-“修复文件所有者”

AppNode网站设置

  • 基本(对应nginx.conf.sample注释)
    • 目录设置
      • 运行目录加上 pub(安装完成后再设置)
    • 默认文档
      • 只保留 index.php
    • 伪静态(对应nginx.conf.sample注释)
      # location / {
      #     try_files $uri $uri/ /index.php$is_args$args;
      # }

      把 try_files $uri $uri/ /index.php$is_args$args; 填入

  • 优化
    • 压缩(对应nginx.conf.sample注释)
      • 启用Gzip压缩
      • 允许压缩的最小文件大 1kb
      • 压缩比 6
      • Mime类型追加
        text/js
        application/xml+rss
    • 缓存和防盗链 关闭
    • 页面优化 关闭
  • PHP
    • PHP进程(重要,不然容易在安装时卡住)
      • memory_limit = 128M PHP 程序执行内存限制
      • max_execution_time = 18000 PHP程序最长执行时间限制
    • PHP安全
      • display_errors = off 在页面上显示 PHP 程序的错误信息
  • Mysql
    • 新建数据库
  • 原文(追加已处理过的nginx.conf.sample内容)
    • $MAGE_ROOT 值更新为项目目录
    • $PASS_UNIX 值改为网站内nginx.conf 中已有的fastcgi_pass 值
    • 修改 add_header "X-UA-Compatible" "IE=Edge,chrome=1";
    • 生产模式把 # expires max; 注释去掉
      
      ## Example configuration:
      # upstream fastcgi_backend {
      #    # use tcp connection
      #    # server  127.0.0.1:9000;
      #    # or socket
      #    server   unix:/var/run/php/php7.1-fpm.sock;
      # }
      # server {
      #    listen 80;
      #    server_name mage.dev;
      #    set $MAGE_ROOT /var/www/magento2;
      #    include /vagrant/magento2/nginx.conf.sample;
      # }
      #
      ## Optional override of deployment mode. We recommend you use the
      ## command 'bin/magento deploy:mode:set' to switch modes instead.
      ##
      ## set $MAGE_MODE default; # or production or developer
      ##
      ## If you set MAGE_MODE in server config, you must pass the variable into the
      ## PHP entry point blocks, which are indicated below. You can pass
      ## it in using:
      ##
      ## fastcgi_param  MAGE_MODE $MAGE_MODE;
      ##
      ## In production mode, you should uncomment the 'expires' directive in the /static/ location block

root $MAGE_ROOT/pub;

set $MAGE_ROOT /data/Ed500fmC/sites/mall.cosmetic.phantasm.top/www;
set $PASS_UNIX unix:/data/Ed500fmC/sites/mall.cosmetic.phantasm.top/php-pool/php-fpm.sock;

index index.php;
autoindex off;
charset UTF-8;
error_page 404 403 = /errors/404.php;

add_header "X-UA-Compatible" "IE=Edge";

PHP entry point for setup application

location ~* ^/setup($|/) {
root $MAGE_ROOT;
location ~ ^/setup/index.php {
fastcgi_pass $PASS_UNIX;

    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   $PASS_UNIX;
    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|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/(version[^/]+/)?(.*)$ /static/$2 last;
}

location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2|json)$ {
    add_header Cache-Control "public";
    add_header X-Frame-Options "SAMEORIGIN";
    expires +1y;

    if (!-f $request_filename) {
        rewrite ^/static/?(.*)$ /static.php?resource=$1 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/?(.*)$ /static.php?resource=$1 last;
    }
}
if (!-f $request_filename) {
    rewrite ^/static/?(.*)$ /static.php?resource=$1 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;
}

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 $PASS_UNIX;
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;

}

gzip on;

gzip_disable "msie6";

gzip_comp_level 6;

gzip_min_length 1100;

gzip_buffers 16 8k;
gzip_proxied any;

gzip_types

text/plain

text/css

text/js

text/xml

text/javascript

application/javascript

application/x-javascript

application/json

application/xml

application/xml+rss

image/svg+xml;

gzip_vary on;

Banned locations (only reached if the earlier PHP entry point regexes don’t match)

location ~* (.php$|.htaccess$|.git) {
deny all;
}


## php配置

; disable automatic session start
; before autoload was initialized

flag session.auto_start = off

; enable resulting html compression

zlib.output_compression = on

; disable user agent verification to not break multiple image upload

suhosin.session.cryptua = off

; PHP for some reason ignores this setting in system php.ini
; and disables mcrypt if this line is missing in local php.ini

extension=mcrypt.so

????


*       max_allowed_packet = 524288000
*       wait_timeout = 100

## web 安装流程
* 访问网站/setup
* step 4
    * 修改 时区、货币单位、语言
    * 去掉以下模块(国内安装时 Upgrading schema 会卡)
        * Amazon_Core
        * Amazon_Login
        * Amazon_Payment
        * Dotdigitalgroup_Email
        * Klarna_Core
        * Klarna_Kp
        * Klarna_Ordermanagement
* *安装报错后,推荐删掉数据库再重装

## 安装后配置
* 安装crontab https://devdocs.magento.com/guides/v2.3/install-gde/install/post-install-config.html
    php bin/magento cron:install
* 配置和运行 crontab https://devdocs.magento.com/guides/v2.3/config-guide/cli/config-cli-subcommands-cron.html
    运行默认的 php bin/magento cron:run --group default
* X-Frame-Options 防点击劫持漏洞 https://devdocs.magento.com/guides/v2.3/config-guide/secy/secy-xframe.html
* 常用执行执行
        php bin/magento setup:upgrade 重置缓存、更新模块、静态资源404时
        php bin/magento setup:di:compile
            memory_limit 太小,则修改 php.ini 配置,官方推荐756M或更高 https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors
        php bin/magento setup:static-content:deploy 针对生产模式下进行静态内容部署 
        sudo chmod 777 -R var
        安装示例数据 php bin/magento sampledata:deploy  
- [ ]           不要在生产模式里执行
        更改url后没有刷新缓存,则用 php bin/magento cache:flush

## Web Setup Wizard 修改模块
* 仅用Composer安装后,管理员账号登录后台后在system 中可见
* 如果没有,可使用 yourwebsite/admin_xxxx/admin/backendapp/redirect/app/setup/ 该地址结构进行访问
    * 需要修改 root /pub 为 /
* 可在system config中填写key绑定账号

## 语言包
* 繁体包 https://marketplace.magento.com/astralweb-zh-hant-tw.html
* 然后进  Web Setup Wizard
    * 安装 astralweb/zh_hant_tw

## 官方模块商城 https://marketplace.magento.com/extensions.html
* https://marketplace.magento.com/amasty-rgrid.html?edition[]=community
* https://marketplace.magento.com/listrak-listrak-remarketing.html?edition[]=community
    * 多个功能
    * 

## 模块(持续花费)
*  https://codecanyon.net/item/advanced-discount-coupons-for-magento-2/21708691?s_rank=4
    * 在多个活动中生成优惠券
    * 自动发送优惠券代码给客户
    * 易于定制的电子邮件模板
    * 受主题限制
* https://codecanyon.net/item/pre-order-for-magento-2/21895461?s_rank=22
    * 预购商品
* https://codecanyon.net/item/magento-2-mass-product-actions/21172564?s_rank=24
    * 批量处理商品价格等
* https://codecanyon.net/item/magento-2-advanced-product-widget/19477581?s_rank=25
    * 支援统计和展现
* https://codecanyon.net/item/magento-2-mass-order-actions/20920973?s_rank=33
    * 批量订单处理
* https://codecanyon.net/item/magento-2-deals/19540572?s_rank=42
    * 特价商品(有倒计时)
* https://codecanyon.net/item/magento-2-marketplace/20814648?s_rank=56
    * 供应商系统

## 中文化
https://www.mageplaza.com/magento-2-chinese-language-pack.html
1composer方式
composer require mageplaza/magento-2-chinese-language-pack:dev-master
*更新 composer update mageplaza/magento-2-chinese-language-pack:dev-master
php bin/magento setup:static-content:deploy zh_Hans_CN
php bin/magento indexer:reindex
php bin/magento cache:clean
php bin/magento cache:flush
2cmd方式
wget https://github.com/mageplaza/magento-2-chinese-language-pack/raw/master/zh_Hans_CN.csv
php bin/magento i18n:pack -m replace zh_Hans_CN.csv zh_Hans_CN
php bin/magento setup:static-content:deploy zh_Hans_CN
php bin/magento cache:flush

## 问题解决
1. 不要设置http访问会直接强行https访问,容易造成多次跳转访问错误 ERR_TOO_MANY_REDIRECTS
2. 不用www做了文件操作(更新等)后,对应文件会因为root操作而被修改成root权限,所以要把全站的权限再设定一次

## 可进行管理
https://lipstick.phantasm.top/setup/#/marketplace-credentials

## 部署静态视图文件(只能在生产模式有效)
项目目录下执行 bin/magento setup:static-content:deploy

## 当前是默认、开发、生产模式
bin/magento deploy:mode:show

## 更改模式
bin/magento deploy:mode:set {mode} [-s|--skip-compilation]
* {mode}是必须的; 它可以是default、developer或production
* --skip-compilation是一个可选参数,可用于在更改为生产模式时跳过代码编译。

切换模式时,会清掉以下目录的内容
var/cache
generated/metadata
generated/code
var/view_preprocessed
pub/static

## 从生产切至开发者模式
请先执行 rm -rf /generated/metadata/* /generated/code/*
在设置模式 bin/magento deploy:mode:set developer

## 资源站
https://www.mageplaza.com/

发表回复

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据