{"id":223,"date":"2023-05-25T23:32:37","date_gmt":"2023-05-25T15:32:37","guid":{"rendered":"https:\/\/gri.my\/blog\/?p=223"},"modified":"2023-07-13T01:36:05","modified_gmt":"2023-07-12T17:36:05","slug":"setting-up-magento-on-docker-with-nginx-as-reverse-proxy","status":"publish","type":"post","link":"https:\/\/gri.my\/blog\/setting-up-magento-on-docker-with-nginx-as-reverse-proxy\/","title":{"rendered":"Setting up Magento on docker (with nginx as reverse proxy)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Download Bitnami&#8217;s Magento docker image<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\ncurl -sSL https:\/\/raw.githubusercontent.com\/bitnami\/containers\/main\/bitnami\/magento\/docker-compose.yml &gt; docker-compose.yml\ndocker-compose up -d\n<\/pre><\/div>\n\n\n<p>You may want to change the external port within the yml file since it defaults to port 80 and 443. Here we use port 1234 for HTTP and 1235 for HTTPS.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: yaml; title: ; notranslate\" title=\"\">\nversion: &#039;2&#039;\nservices:\n  mariadb:\n    image: docker.io\/bitnami\/mariadb:10.4\n    environment:\n      # ALLOW_EMPTY_PASSWORD is recommended only for development.\n      - ALLOW_EMPTY_PASSWORD=yes\n      - MARIADB_USER=bn_magento\n      - MARIADB_DATABASE=bitnami_magento\n    volumes:\n      - &#039;mariadb_data:\/bitnami\/mariadb&#039;\n  magento:\n    image: docker.io\/bitnami\/magento:2\n    ports:\n      - &#039;1234:8080&#039; # HTTP\n      - &#039;1235:8443&#039; # HTTPS\n    environment:\n      - MAGENTO_HOST=localhost\n      - MAGENTO_DATABASE_HOST=mariadb\n      - MAGENTO_DATABASE_PORT_NUMBER=3306\n      - MAGENTO_DATABASE_USER=bn_magento\n      - MAGENTO_DATABASE_NAME=bitnami_magento\n      - ELASTICSEARCH_HOST=elasticsearch\n      - ELASTICSEARCH_PORT_NUMBER=9200\n      # ALLOW_EMPTY_PASSWORD is recommended only for development.\n      - ALLOW_EMPTY_PASSWORD=yes\n    volumes:\n      - &#039;magento_data:\/bitnami\/magento&#039;\n    depends_on:\n      - mariadb\n      - elasticsearch\n  elasticsearch:\n    image: docker.io\/bitnami\/elasticsearch:7\n    volumes:\n      - &#039;elasticsearch_data:\/bitnami\/elasticsearch\/data&#039;\nvolumes:\n  mariadb_data:\n    driver: local\n  magento_data:\n    driver: local\n  elasticsearch_data:\n    driver: local\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Configure nginx<\/h2>\n\n\n\n<p>Sample nginx.conf that configures nginx to be a reverse proxy for docker with SSL certs from LetsEncrypt.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nhttp {\n\tserver {\n\t\tlisten       443 ssl;\n\t\tlisten &#x5B;::]:443 ssl;\n\t\tserver_name  yourdomain.com;\n\n\t\tssl_certificate      &quot;C:\\Certbot\\live\\yourdomain.com\\fullchain.pem&quot;;\n\t\tssl_certificate_key  &quot;C:\\Certbot\\live\\yourdomain.com\\privkey.pem&quot;;\n\n\t\tssl_session_cache    shared:SSL:1m;\n\t\tssl_session_timeout  5m;\n\n\t\tssl_ciphers  HIGH:!aNULL:!MD5;\n\t\tssl_prefer_server_ciphers  on;\n\n\t\tlocation \/ {\n\n\t\t\tproxy_pass         https:\/\/localhost:1235;\n\t\t\tproxy_set_header    Host               yourdomain.com;\n\n\t\t\tproxy_buffer_size          128k;\n\t\t\tproxy_buffers              4 256k;\n\t\t\tproxy_busy_buffers_size    256k;\n\t\t}\n\t}\n}\n<\/pre><\/div>\n\n\n<p>Remember to restart nginx for settings to apply<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configure Magento<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">Configure hostname<\/h4>\n\n\n\n<p>Within Docker Desktop, go to Containers and click on the Magento container. In the container&#8217;s terminal, enter these to set the hostname. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmagento setup:store-config:set --base-url=&quot;http:\/\/yourdomain.com\/&quot;\nmagento setup:store-config:set --base-url-secure=&quot;https:\/\/yourdomain.com\/&quot;\n<\/pre><\/div>\n\n\n<p>You need to do this because Magento uses full URLs for links within their pages, which breaks if you use the default localhost value.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Enable HTTPS for the admin panel<\/h4>\n\n\n\n<p>Within Docker Desktop, go to Containers and click on the mariadb container. In the container&#8217;s terminal..<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmysql -u bn_magento -p bitnami_magento\n<\/pre><\/div>\n\n\n<p>Press enter when prompted for password<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nupdate core_config_data set value = 1 where path = &#039;web\/secure\/use_in_adminhtml&#039;;\n<\/pre><\/div>\n\n\n<p>Restart the Magento container<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Make PHP tmp folder writeable<\/h4>\n\n\n\n<p>Within Docker Desktop, go to Containers and click on the Magento container. In the container&#8217;s terminal..<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nchmod 777 \/opt\/bitnami\/php\/tmp\n<\/pre><\/div>\n\n\n<p>This is so that you can upload media via the REST API without error.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Allow encoded slashes for Apache<\/h4>\n\n\n\n<p>Go to the Magento container and edit the file <code>\/opt\/bitnami\/apache\/conf\/vhosts\/magento-https-vhost.conf<\/code><\/p>\n\n\n\n<p>Add a line <code>AllowEncodedSlashes NoDecode<\/code> to the virtual host like so<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;VirtualHost 127.0.0.1:8443 _default_:8443&gt;\n  ServerName www.example.com\n  ServerAlias *\n  SSLEngine on\n  SSLCertificateFile &quot;\/opt\/bitnami\/apache\/conf\/bitnami\/certs\/server.crt&quot;\n  SSLCertificateKeyFile &quot;\/opt\/bitnami\/apache\/conf\/bitnami\/certs\/server.key&quot;\n  DocumentRoot \/opt\/bitnami\/magento\n  AllowEncodedSlashes NoDecode\n  &amp;lt;Directory &quot;\/opt\/bitnami\/magento&quot;&gt;\n    Options -Indexes +FollowSymLinks -MultiViews\n    AllowOverride All\n    Require all granted\n  &amp;lt;\/Directory&gt;\n&amp;lt;\/VirtualHost&gt;\n<\/pre><\/div>\n\n\n<p>This is to let the API passthrough paths that has the slash encoded in it. See <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/magento\/devdocs\/issues\/6689\" data-type=\"URL\" data-id=\"https:\/\/github.com\/magento\/devdocs\/issues\/6689\" target=\"_blank\">Github issue<\/a>.<\/p>\n\n\n\n<p>Then run<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\napachectl restart\n<\/pre><\/div>\n\n\n<p>to apply the directive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Download Bitnami&#8217;s Magento docker image You may want to change the external port within the yml file since it defaults to port 80 and 443. Here we use port 1234 for HTTP and 1235 for&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-223","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/comments?post=223"}],"version-history":[{"count":14,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":244,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/posts\/223\/revisions\/244"}],"wp:attachment":[{"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gri.my\/blog\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}