Skip to content

Instantly share code, notes, and snippets.

@tylers-username
Last active September 1, 2020 08:34
Show Gist options
  • Save tylers-username/48c6ec37d45a3f96da3d6549cbc67d00 to your computer and use it in GitHub Desktop.
Save tylers-username/48c6ec37d45a3f96da3d6549cbc67d00 to your computer and use it in GitHub Desktop.
Magento 2: Quick Lando Setup
#####
# Built & Tested against: v3.0.0-rc.16
#
# The first time that this project is launched, it will take Lando a while to get past "waiting until nginx service is ready"
# This is because Magento has not yet been setup and is 302-redirecting "/" to "/setup" while Lando is hoping for a "200"
# response. One the setup has completed Lando will show a ready-state much more quickly on the next start-up.
# Give this Lando project a unique name
name: m2-community
# Start with the default LEMP recipe
recipe: lemp
proxy:
# Set Magento URL
appserver_nginx:
- magento2.lndo.site
# Configure the LEMP recipe
config:
# PHP 7.2 for Magento 2.2.3 - adjust as needed
php: '7.2'
webroot: pub
database: mysql
# Optionally activate xdebug
xdebug: false
# Optionally mix in your own config files
#
# If ommitted this will use "sane defaults" for your recipe type
config:
# Relative to Magento project root
vhosts: lando.nginx.conf
#php: config/php.ini
services:
# Provide some custom options for the database service
database:
# Make a random port available to the host machine for your MySQL connection.
# This can be a number if you want to map a specific port
portforward: true
appserver:
# This will only run the first time `lando start` is executed or on `lando rebuild`
build_as_root:
# Install Magento-required xsl extension
- apt-get update -y -q && apt-get install -y libxslt-dev && docker-php-ext-install xsl
tooling:
magento:download:
description: Initialize Magento 2 project in current directory
cmd:
# Quoted to ensure the `cut` command is properly escaped
- "wget $(curl -s https://api.github.com/repos/magento/magento2/releases/latest | grep zipball_url | cut -d '\\\"' -f 4) -O magento_latest.zip"
# Keep `&&` vs multi-line to preserve `tmp` directory when Lando has not been started
- unzip magento_latest.zip -d /tmp/magento-extract && cp --verbose -R /tmp/magento-extract/magento*/. /app
- "rm magento_latest.zip && cp auth.json.sample auth.json && touch lando\\.nginx\\.conf"
# Provide setup instructions
- "echo \"\n Magento has been downloaded! Now...\n
\n\t 1. Add your Magento credentials to auth.json
\n\t\t Magento: https://marketplace.magento.com/customer/accessKeys/
\n\t\t GitHub: https://github.com/settings/tokens
\n\t 2. Add config to `lando.nginx.conf`
\n\t\t Refer to: https://gist.github.com/tylerssn/48c6ec37d45a3f96da3d6549cbc67d00#file-lando-nginx-conf
\n\t 3. `lando start && lando composer install && lando magento:setup:quick && lando magento cache:flush`
\n\n That's it! You will then be able to access your Magento store at https://magento2.lndo.site/ \n
\n\n Run `lando` to see available shortcuts such as `lando magento`! \n
\""
service: appserver
magento:setup:quick:
description: Automated process to quickly configure Magento for Lando. This command accepts `./bin/magento setup:install` arguments
cmd: /app/bin/magento setup:install --db-host database --db-name lemp --db-user lemp --db-password lemp --magento-init-params="MAGE_MODE=developer"
service: appserver
magento:
description: Execute ./bin/magento commands
cmd: /app/bin/magento
service: appserver
magento:setup:destroy:
description: Automated process to quickly reset Magento's database to empty.
cmd: /app/bin/magento setup:uninstall
service: appserver

Lando Magento 2 Template

THIS HAS BEEN MOVED TO improper/lando-magento2-template.

THIS GIST IS NO LONGER SUPPORTED

Important notes

  1. This setup expects Lando to be installed and ready to go on your machine
  2. This setup expects Magento's stock nginx.conf.sample to exist in Magento's project root. Of course you can modify this file as needed moving forward.
  3. You may experience performance gains by adding Redis, Elasticsearch, etc.. These services can easily be added following Lando's documentation.

Quick Setup

  1. Add this Gist's .lando.yml to an empty directory
  2. Run lando magento:download
  3. Add your Magento credentials to auth.json
  4. Copy config to lando.nginx.conf
  5. Launch your new store and deploy the database: lando start && lando composer install && lando magento:setup:quick && lando magento cache:flush
    • Optionally, you can pass the ----use-sample-data to the the lando magento:setup:quick command shown in this step.

That's it! You will then be able to access your Magento store at https://magento2.lndo.site/

HINT: Run lando to see available shortcuts such as lando magento!

lando magento:setup:quick is an alias for lando magento setup:install and is pre-configured to setup the database connection.

  • Defaults to developer mode.
  • See lando magento:setup:quick --help for additional configuration options.
  • For the sake of shortcuts, lando magento:setup:destroy is also available.

If you did not pass additional arguments to lando magento:setup:quick, you will probably want to know your admin URI and to create an admin user.

  • Fetch Admin URI: lando magento info:adminuri
  • Create admin user: lando magento admin:user:create
upstream fastcgi_backend {
server fpm:9000;
}
server {
listen 443 ssl;
listen 80;
listen [::]:80 default ipv6only=on;
server_name localhost;
ssl_certificate /certs/cert.crt;
ssl_certificate_key /certs/cert.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
set $MAGE_ROOT /app;
include /app/nginx.conf.sample;
location ~ \.php$ {
fastcgi_pass fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffers 256 128k;
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
include fastcgi_params;
}
}
@rhuffstedtler
Copy link

If you are using an older version of Lando, you may need to update in order for install_dependencies_as_root to be recognized

@rhuffstedtler
Copy link

I'm currently getting this error:

~/Sites/m2-demo$ lando magento:setup:quick && lando magento cache:flush
OCI runtime exec failed: exec failed: container_linux.go:344: starting container process caused "exec: "$LANDO_MOUNT/bin/magento": stat $LANDO_MOUNT/bin/magento: no such file or directory": unknown

/Sites/m2-demo/bin/magento exists, and if I lando ssh to the appserver and stat it, it gives the details on it:
www-data@7a088a230829:
$ stat $LANDO_MOUNT/bin/magento
File: /app/bin/magento
Size: 799 Blocks: 8 IO Block: 4096 regular file
Device: 53h/83d Inode: 8634726041 Links: 1
Access: (0755/-rwxr-xr-x) Uid: ( 501/www-data) Gid: ( 20/ dialout)
Access: 2019-05-23 01:55:24.000000000 +0000
Modify: 2019-05-22 21:15:42.000000000 +0000
Change: 2019-05-23 01:40:13.000000000 +0000

No more time to investigate tonight.

@rhuffstedtler
Copy link

I decided I had a little more time. Somewhere around RC2, they deprecated the $LANDO_MOUNT environment variable. replacing it with '.' seems to work reliably.

@tylers-username
Copy link
Author

Thought it worth sharing this comment here for anyone else having Lando version compatibility issues.

lando/lando#1553 (comment)

This was developed on an earlier RC - I think everything >=RC12 is intended to be much more stable. If I have some down time at work tomorrow I'll update it for you. I imagine the biggest issue is the outdated NGINX config for Lando.

@rhuffstedtler
Copy link

Yep - the nginx config was the last thing I was struggling with last night. I couldn't determine whether it was failing to read the config, or if it was reading the config, but the config wasn't right. At that point I went to bed and decided to try pivoting to getting it working on the lamp recipe instead of the lemp recipe with a few adjustments - haven't had enough time to get to the conclusion of that, yet.

@tylers-username
Copy link
Author

@rhuffstedtler - I have updated the gist.

  • The .lando.yml has been made compatible with the latest version of Lando.
  • The lando.nginx.conf has been updated so that it is compatible with the latest version of Lando.
  • I have added the lando magento:download command
    • This command will download the latest version of Magento to your current working directory and create the lando.nginx.conf file (you will still have to paste the content from this Gist for now into the file)
    • Once this command completes, it outputs the next recommended steps.
  • The readme has been updated.

I've tested this a couple of times today and it seems fairly stable. Within 3 or 4 steps you should have a functional empty Luma site up and running. However, you can pass the --use-sample-data to the lando magento:setup:quick command to include the sample data while also configuring the DB to work with Lando.

lando magento:setup:quick is actually an alias for bin/magento setup:install (with a few default arguments for Lando) so you can actually pass any bin/magento setup:install argument or option to this command.

I'm going to go out on a limb and say this should now be one of the easiest ways to get up and running on Magento.

I recommend running lando destroy -y before following the steps in the readme.

Lastly, this Lando config will work with any existing code base that you may already have. You'll just want to consider removing the setup/download commands and renaming the Lando app and proxy URL.

@tylers-username
Copy link
Author

@rhuffstedtler - Also, keep in mind that the lando.nginx.conf references Magento's distributed nginx.conf.sample. If you're applying this to an existing project then you will want to consider that.

@tylers-username
Copy link
Author

For a more quick and easy setup process, this Gist has been converted to a repository: improper/lando-magento2-template

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment