diff --git a/.gitignore b/.gitignore index 795789d..86a39bd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,11 @@ /.vagrant /.idea +/.DS_Store /ssh /www /vagrant-ssh /config.yaml -/.DS_Store /extra/001-env.sh /extra/100-pre-build.sh -/extra/120-post-build.sh \ No newline at end of file +/extra/120-post-build.sh +/db-dump.sql.gz \ No newline at end of file diff --git a/Readme.md b/Readme.md index 16b18e3..7fe498f 100644 --- a/Readme.md +++ b/Readme.md @@ -3,7 +3,7 @@ [![vagrant](https://img.shields.io/badge/vagrant-debian:stretch-blue.svg?longCache=true&style=flat&label=vagrant&logo=vagrant)](https://app.vagrantup.com/debian/boxes/stretch64) [![dev-box](https://img.shields.io/badge/git/composer-blue.svg?longCache=true&style=flat&label=setup&logo=magento)](https://github.com/zepgram/magento2-fast-vm/blob/master/config.yaml.example) [![mount](https://img.shields.io/badge/nfs/rsync-blue.svg?longCache=true&style=flat&label=mount)](https://github.com/zepgram/magento2-fast-vm/releases) -[![release](https://img.shields.io/badge/release-v1.2.0-blue.svg?longCache=true&style=flat&label=release)](https://github.com/zepgram/magento2-fast-vm/releases) +[![release](https://img.shields.io/badge/release-v1.3.0-blue.svg?longCache=true&style=flat&label=release)](https://github.com/zepgram/magento2-fast-vm/releases) [![license](https://img.shields.io/badge/license-MIT-blue.svg?longCache=true&style=flat&label=license)](https://github.com/zepgram/magento2-fast-vm/blob/master/LICENSE) ![windows](https://img.shields.io/badge/windows-ok-green.svg?longCache=true&style=flat&label=windows&logo=windows) @@ -23,7 +23,7 @@ Stable version >= 5.2.0 1. Run the installer, choosing all of the default options. * Windows: Grant the installer access every time you receive a security prompt. * Mac: Enter your admin password. - * Linux: Enter your root password if prompted. + * Linux: Enter your user password if prompted. 1. Reboot your laptop if prompted to do so when installation completes. 1. Close the VirtualBox window if it pops up at the end of the install. @@ -35,26 +35,30 @@ Stable version >= 2.2.0 * [Vagrant download](https://www.vagrantup.com/downloads.html) 1. Reboot your laptop if prompted to do so when installation completes. -## Installation +## Configurations ### Related guide -- Made by Onilab for Windows 10: +- Made by Onilab for Windows 10:
https://onilab.com/blog/install-magento-2-on-localhost-a-windows-10-guide/ -### First installation +### Pre-installation ⚠ DO NOT USE SSH KEY WITH PASSPHRASE, this vagrant installation is non-interactive.
-If your ssh key has been created with a passphrase, please create an other one and add it to your git account. +If your ssh key has been created with a passphrase, please create an other one. +1. On Windows only: open UEFI BIOS and make sure virtualization is turned 'on' +1. On Windows only: open powershell as administrator and run: ``Add-MpPreference -ExclusionProcess winnfsd.exe`` +1. On Windows only: open ``C:\Windows\System32\drivers\etc\hosts`` as administrator then add ``network_ip`` and ``magento_url``
Default values would be: ``192.168.200.50 dev.magento.com`` +1. On Linux only: in order to install NFS, run ``sudo apt install nfs-kernel-server`` +1. On Linux/MacOS only: open ``/etc/hosts`` as sudo then add ``network_ip`` and ``magento_url``
Default values would be: ``192.168.200.50 dev.magento.com`` + +### Installation + 1. Clone this project: ``git clone git@github.com:zepgram/magento2-fast-vm.git`` -1. On linux only in order to install NFS, run: ``sudo apt install nfs-kernel-server`` -1. On windows only, make sur virtualization is turned 'on' in UEFI BIOS -1. Copy and past ``ssh.example``, rename it ``ssh`` and put your ``id_rsa`` and ``id_rsa.pub`` keys -1. Copy and past ``config.yaml.example``, rename it ``config.yaml`` and add your configurations according to [Yaml config overview](#yaml-config-overview) -1. As admin open your host file: ``C:\Windows\System32\drivers\etc\hosts`` for Windows or ``/etc/hosts``for Linux/macOS and add vm_conf[network_ip] and magento[url]
-Default values would be: ``192.168.200.50 dev.magento.com`` -1. On windows 10 start your terminal as administrator and uncomment option ``# v.gui=true`` in VagrantFile. You can disable it after first setup -1. Run: ``vagrant up`` in your terminal: setup start! (duration: ~20 minutes) -1. Once installation is done run: ``vagrant ssh`` to access to your guest machine +1. Copy/past: ``ssh.example`` rename it ``ssh`` then put your ``id_rsa`` and ``id_rsa.pub`` keys +1. Copy/past: ``config.yaml.example`` rename it ``config.yaml``
Then customize configurations according to [Yaml config overview](#yaml-config-overview) +1. If you want to import an existing database: create a compressed sql dump and name it ``db-dump.sql.gz``.
You must also fill ``crypt_key`` in config.yaml +1. To start install run: ``vagrant up`` (duration: ~20 minutes) +1. Finally run: ``vagrant ssh`` to access to your guest machine ### Yaml config overview * Vmconf @@ -88,13 +92,14 @@ Default values would be: ``192.168.200.50 dev.magento.com`` * edition: magento project edition, used only on composer source installation (community) * 'community' install magento community edition * 'enterprise' install magento enterprise edition - * version: set magento version and also define PHP version (2.3.*) + * version: set magento version and also define PHP version (2.3.3) * php_version: override the default required version by yours, for example '7.1' (default) * sample: install sample data, used only on composer source installation (true) * mode: magento mode (developer) * currency: set currency (USD) * language: set language (en_US) * time_zone: set time zone (Europe/London) + * crypt_key: crypt key under your app/etc/env.php (only required if db-dump.sql.gz exist) ### Path @@ -103,8 +108,8 @@ Default values would be: ``192.168.200.50 dev.magento.com`` ### Mount options -#### RSYNC - new (v1.2.0) -Only usefull on path set to ``root``.
+#### RSYNC +Only useful on path set to ``root``.
* Loss of performance is due to files generated on the fly, by excluding them you can mount the whole directory ``root`` and get performance equal to an ``app`` mount. * The ``vagrant rsync-auto`` is launched by default on vagrant up, even with that if you need to force an update you can run ``vagrant rsync``. Terminal should be kept open for rsync-auto: do not close it. * Rsync is unilateral, your host machine push files to guest but not the other way.
@@ -135,13 +140,9 @@ Those provisions will be executed on pre-defined sequences: ## Usage ### Permission -Magento file system owner is configured for ``magento`` user, it means all commands in magento project must be executed by this user.
-By default command line ``vagrant ssh`` will log you as magento user.
-* To logout and get back to vagrant user you can run ``exit`` -* To login as magento user you can run ``sudo su magento`` or ``bash`` -* To re-apply magento permission you can run ``permission`` in command line, used only for ``app`` path and ``default`` mount. - -FI: Password for magento user is ``magento`` +Magento file system owner is configured for ``vagrant`` user, it means all commands in magento project must be executed by this user.
+By default command line ``vagrant ssh`` will log you as vagrant user.
+* To re-apply magento permission you can run ``permission`` in command line: this is only applicable for ``app`` path or ``default`` mount configurations. ### Command line * magento (Magento CLI for your project) @@ -170,7 +171,8 @@ Disable cron: - gitflow - vim - mariadb -- apache2 +- nginx +- php-fpm - redis-server - composer - magento-cloud CLI @@ -184,11 +186,10 @@ Disable cron: ### Credentials * User bash terminal - * user: magento - * password: magento + * user: vagrant * Back-office * url: magento[url]/admin - * user: admin + * user: magento.admin * pass: admin123 * Database * user: vagrant diff --git a/Vagrantfile b/Vagrantfile index e136616..574deac 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -28,10 +28,10 @@ end # Mount directory option hostDirectory = "./www/#{projectName}" -guestDirectory = "/srv/#{projectName}" +guestDirectory = "/home/vagrant/#{projectName}" if vmconf['path'] == 'app' hostDirectory = "./www/#{projectName}/app" - guestDirectory = "/srv/#{projectName}/app" + guestDirectory = "/home/vagrant/#{projectName}/app" end # Vagrant configure @@ -63,7 +63,7 @@ Vagrant.configure(2) do |config| # Default options config.vm.synced_folder '.', '/vagrant', disabled: true config.bindfs.default_options = { - force_user: 'magento', + force_user: 'vagrant', force_group: 'www-data', perms: 'u=rwx:g=rwx:o=r' } @@ -103,10 +103,11 @@ Vagrant.configure(2) do |config| config.vm.provision 'file', source: './ssh/id_rsa', destination: '~/.ssh/id_rsa' config.vm.provision 'file', source: './ssh/id_rsa.pub', destination: '~/.ssh/id_rsa.pub' - # Extra provisionner + # Extra provision process_extra_file(config, 'extra/001-env.sh') process_extra_file(config, 'extra/100-pre-build.sh') process_extra_file(config, 'extra/120-post-build.sh') + process_extra_file(config, 'db-dump.sql.gz') # Environment provisioning config.vm.provision 'shell', path: 'provision/001-system-env.sh', run: 'always', keep_color: true, args: [ @@ -114,10 +115,10 @@ Vagrant.configure(2) do |config| git['name'], git['email'], git['host'], git['repository'], magento['url'], magento['php_version'], magento['source'], magento['edition'], magento['version'], magento['sample'], magento['mode'], magento['currency'], - magento['language'], magento['time_zone'], vmconf['mount'], vmconf['path'] + magento['language'], magento['time_zone'], magento['crypt_key'], vmconf['mount'], vmconf['path'] ] - # Shell provisioning + # Shell provisioning if vmconf['provision'] == 'all' config.vm.provision 'shell', path: 'provision/010-system-packages.sh', keep_color: true config.vm.provision 'shell', path: 'provision/020-system-services.sh', keep_color: true @@ -143,12 +144,12 @@ Vagrant.configure(2) do |config| " --------------------------------------------------------- Vagrant machine ready to use for #{git['name']} - type #{vmconf['mount']} + mount #{vmconf['mount']} path #{guestDirectory} - magento #{magento['url']} - phpinfo #{vmconf['network_ip']}/phpinfo - adminer #{vmconf['network_ip']}/adminer - mailcatcher #{vmconf['network_ip']}:1080 + magento http://#{magento['url']} + phpinfo http://#{vmconf['network_ip']}/php + adminer http://#{vmconf['network_ip']}/adminer + mailcatcher http://#{vmconf['network_ip']}:1080 " # Triggers diff --git a/config.yaml.example b/config.yaml.example index b576bae..e51b207 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -19,10 +19,11 @@ magento: url: 'dev.magento.com' source: 'composer' edition: 'community' - version: '2.3.2' + version: '2.3.3' php_version: 'default' sample: 'true' mode: 'developer' currency: 'USD' language: 'en_US' time_zone: 'America/New_York' + crypt_key: '' diff --git a/db-dump.sql.gz.example b/db-dump.sql.gz.example new file mode 100644 index 0000000..e69de29 diff --git a/dependency.rb b/dependency.rb index 0e13a36..7b7dd4a 100644 --- a/dependency.rb +++ b/dependency.rb @@ -35,7 +35,7 @@ def check_plugins(dependencies) if ['up', 'reload'].include?(ARGV[0]) && !skip_dependency_manager installed_dependencies = [] - puts "\033[0m" << "Checking dependencies..." << "\e[0m" + puts "\033[0m" << "==> Checking dependencies..." << "\e[0m" raw_output = `vagrant plugin list` raw_list = raw_output.split("\n") @@ -54,18 +54,18 @@ def check_plugins(dependencies) dependencies.each_with_index do |dependency, index| if not installed_dependencies.include? dependency dependencies_already_satisfied = false - puts "\033[0m" << " - Missing '#{dependency}'!" << "\e[0m" + puts "\033[0m" << "==> Missing '#{dependency}'!" << "\e[0m" if not system "vagrant plugin install #{dependency}" - puts "\n\033[0m" << " - Could not install plugin '#{dependency}'. " << "\e[0m\033[41m" <<"Stopped." << "\e[0m" + puts "\n\033[0m" << "==> Could not install plugin '#{dependency}'. " << "\e[0m\033[41m" <<"Stopped." << "\e[0m" exit -1 end end end if dependencies_already_satisfied - puts "\033[0m" << " - All dependencies are satisfied" << "\e[0m" + puts "\033[0m" << "==> All dependencies are satisfied" << "\e[0m" else - puts "\033[0m" << " - Dependencies installed" << "\e[0m" + puts "\033[0m" << "==> Dependencies installed" << "\e[0m" exec "vagrant " << "--skip-dependency-manager " << ARGV.join(" ") exit end diff --git a/provision/001-system-env.sh b/provision/001-system-env.sh index 4fb4254..2122dd1 100644 --- a/provision/001-system-env.sh +++ b/provision/001-system-env.sh @@ -15,7 +15,7 @@ echo '--- Environment variables ---' cat < /etc/profile.d/env.sh export PROJECT_NAME="${1}" export PROJECT_USER="${1}" -export PROJECT_PATH="/srv/${1}" +export PROJECT_PATH="/home/vagrant/${1}" export PROJECT_COMPOSER_USER="${2}" export PROJECT_COMPOSER_PASS="${3}" export PROJECT_GIT_USER="${4}" @@ -32,35 +32,21 @@ export PROJECT_MODE="${14}" export PROJECT_CURRENCY="${15}" export PROJECT_LANGUAGE="${16}" export PROJECT_TIME_ZONE="${17}" -export PROJECT_MOUNT="${18}" -export PROJECT_MOUNT_PATH="${19}" +export PROJECT_CRYPT_KEY="${18}" +export PROJECT_MOUNT="${19}" +export PROJECT_MOUNT_PATH="${20}" EOF source /etc/profile.d/env.sh -# Create magento user and password -if [[ ! -f "/root/.user-${PROJECT_USER}" ]]; then - useradd -m -p $(python -c "import crypt; print crypt.crypt(\"magento\", \"\$6\$$(> /home/"$PROJECT_USER"/.bashrc - -# Source env -source /etc/profile.d/env.sh -source /etc/profile.d/setup-owner.sh -EOF -fi - -# Log as magento user -if [[ -z $(grep "${PROJECT_USER}" "/home/vagrant/.bashrc") ]]; then -cat <> /home/vagrant/.bashrc -# Log as $PROJECT_USER user -cd $PROJECT_PATH && sudo su $PROJECT_USER; +# Project path +if [[ -z $(grep "${PROJECT_PATH}" "/home/vagrant/.bashrc") ]]; then +cat <<-EOF >> /home/vagrant/.bashrc +cd $PROJECT_PATH EOF fi # Patch extra files +sudo -u vagrant mkdir -p /home/vagrant/extra if [[ ! $(dpkg-query -l 'dos2unix') ]]; then sudo apt-get install -y dos2unix fi @@ -79,20 +65,6 @@ if [ -f /home/vagrant/extra/001-env.sh ]; then bash /home/vagrant/extra/001-env.sh fi -# Set project owner for setup -MOUNT_FULL_PATH=$PROJECT_PATH -if [ $PROJECT_MOUNT_PATH == "app" ]; then - MOUNT_FULL_PATH=$PROJECT_PATH/app -fi -SETUP_OWNER="$(ls -ld $MOUNT_FULL_PATH | awk 'NR==1 {print $3}')" -if [ $SETUP_OWNER != "magento" ]; then - SETUP_OWNER="vagrant" -fi -cat < /etc/profile.d/setup-owner.sh -export PROJECT_SETUP_OWNER="${SETUP_OWNER}" -EOF - # Source and display source /etc/profile -cat /etc/profile.d/setup-owner.sh cat /etc/profile.d/env.sh diff --git a/provision/010-system-packages.sh b/provision/010-system-packages.sh index aa5ed11..267e580 100644 --- a/provision/010-system-packages.sh +++ b/provision/010-system-packages.sh @@ -19,13 +19,19 @@ debconf-set-selections <<< "mysql-server mysql-server/root_password_again passwo debconf-set-selections <<< "postfix postfix/mailname string $PROJECT_URL" debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'" +# Configure default locale +cp /etc/locale.gen /etc/locale.gen.old +sed -i "s/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/g" /etc/locale.gen +/usr/sbin/locale-gen +export LANG=en_US.UTF-8* + # Required packages apt-get install -y \ curl graphviz htop net-tools rsync sudo tree wget unzip zip \ libsqlite3-dev libxml2-utils build-essential software-properties-common \ - postfix mailutils libsasl2-2 libsasl2-modules ca-certificates \ + postfix mailutils libsasl2-2 libsasl2-modules ca-certificates libnss3-tools \ apt-transport-https mysql-client mysql-server redis-server \ - openssl apache2 \ + openssl nginx \ g++ vim git git-flow # Sury Repository @@ -34,6 +40,9 @@ echo "deb https://packages.sury.org/php/ stretch main" | sudo tee /etc/apt/sourc # Set php version MAGENTO_PHP_VERSION='7.2'; +if $(dpkg --compare-versions "${PROJECT_VERSION}" "gt" "2.3.2-p1"); then + MAGENTO_PHP_VERSION='7.3'; +fi if $(dpkg --compare-versions "${PROJECT_VERSION}" "lt" "2.3"); then MAGENTO_PHP_VERSION='7.1'; fi @@ -54,7 +63,7 @@ apt-get update -y && apt-get install -y \ php${PROJECT_PHP_VERSION}-curl php${PROJECT_PHP_VERSION}-gd php${PROJECT_PHP_VERSION}-intl \ php${PROJECT_PHP_VERSION}-mbstring php${PROJECT_PHP_VERSION}-soap php${PROJECT_PHP_VERSION}-zip \ php${PROJECT_PHP_VERSION}-xml php${PROJECT_PHP_VERSION}-xml php${PROJECT_PHP_VERSION}-bcmath \ - php${PROJECT_PHP_VERSION}-mysql php${PROJECT_PHP_VERSION}-sqlite3 libapache2-mod-php${PROJECT_PHP_VERSION} \ + php${PROJECT_PHP_VERSION}-mysql php${PROJECT_PHP_VERSION}-sqlite3 php${PROJECT_PHP_VERSION}-fpm \ php${PROJECT_PHP_VERSION}-memcache php${PROJECT_PHP_VERSION}-redis php${PROJECT_PHP_VERSION}-opcache \ python ruby ruby-dev if $(dpkg --compare-versions "${PROJECT_PHP_VERSION}" "lt" "7.2"); then diff --git a/provision/020-system-services.sh b/provision/020-system-services.sh index f321401..60ec1f9 100644 --- a/provision/020-system-services.sh +++ b/provision/020-system-services.sh @@ -25,19 +25,16 @@ mysql -u root -ppassword -e "FLUSH PRIVILEGES;" # ----------------------------------------------------------------------------------------------------- +# Add binary entry +sudo ln -sfn /usr/sbin/sendmail /usr/local/bin/ + # First, remove old relayhost entry sed -i.bak '/relayhost/,/^/d' /etc/postfix/main.cf # Enter new information echo "relayhost = 127.0.0.1:1025 -myhostname = $PROJECT_URL mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128" | tee -a /etc/postfix/main.cf - -# Php.ini configuration -sed -i 's/smtp_port.*/smtp_port = 1025/' /etc/php/"$PROJECT_PHP_VERSION"/apache2/php.ini -cat <> /etc/php/"$PROJECT_PHP_VERSION"/apache2/php.ini -sendmail_path="LANG=en_US.UTF-8 /usr/bin/env catchmail -f vagrant@$PROJECT_URL" -EOF +sed -i "s/myhostname.*/myhostname = $PROJECT_URL" /etc/postfix/main.cf # Configuration on booting cat <<'EOF' > /etc/init.d/mailcatcher @@ -62,6 +59,12 @@ update-rc.d mailcatcher defaults lsof -ti :1025 | xargs --no-run-if-empty kill -9 mailcatcher --http-ip=0.0.0.0 +/usr/sbin/sendmail -t -i -f $PROJECT_GIT_EMAIL <> /etc/php/"$PROJECT_PHP_VERSION"/apache2/php.ini +cat <> /etc/php/"$PROJECT_PHP_VERSION"/fpm/php.ini opcache.file_cache=/tmp/php-opcache EOF cat <> /etc/php/"$PROJECT_PHP_VERSION"/cli/php.ini @@ -166,61 +176,32 @@ systemd-tmpfiles --create /etc/tmpfiles.d/php-cli-opcache.conf # ----------------------------------------------------------------------------------------------------- -mkdir -p /var/www/html -chown -R www-data:www-data /var/www/ +# SSL certificates +mkdir /home/vagrant/ssl +cd /home/vagrant/ssl && openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \ + -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.${PROJECT_URL}" \ + -keyout www.${PROJECT_URL}.key -out www.${PROJECT_URL}.crt +cp /home/vagrant/ssl/www.${PROJECT_URL}.crt /etc/ssl/certs/www.${PROJECT_URL}.crt +cp /home/vagrant/ssl/www.${PROJECT_URL}.key /etc/ssl/private/www.${PROJECT_URL}.key +rm -rf /home/vagrant/ssl && cd /home/vagrant -# Default -cat <<'EOF' > /etc/apache2/sites-available/000-default.conf - - ServerName default - DocumentRoot /var/www/html - ErrorLog /var/log/apache2/default.error.log - CustomLog /var/log/apache2/default.access.log combined - SetEnvIf X-Forwarded-Proto https HTTPS=on - -EOF -# SSL -cat <<'EOF' > /etc/apache2/sites-available/001-ssl.conf - - - SSLEngine on - SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH - SSLHonorCipherOrder On - SSLProtocol All -SSLv2 -SSLv3 - SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem - SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key - - - Order deny,allow - Allow from all - - - ProxyPass / http://localhost:80/ retry=0 - ProxyPassReverse / http://localhost:80/ - ProxyPreserveHost on - RequestHeader set X-Forwarded-Proto "https" early - - - SSLOptions +StdEnvVars - - - ErrorLog /var/log/apache2/default.error.log - CustomLog /var/log/apache2/default.access.log combined - - -EOF +# ----------------------------------------------------------------------------------------------------- -# Enable modules -a2dismod php7.0 php7.1 php7.2 -a2ensite 000-default -a2ensite 001-ssl -a2enmod deflate expires headers proxy proxy_http rewrite ssl php"$PROJECT_PHP_VERSION" -update-alternatives --set php /usr/bin/php"$PROJECT_PHP_VERSION" -update-alternatives --set phar /usr/bin/phar"$PROJECT_PHP_VERSION" -update-alternatives --set phar.phar /usr/bin/phar.phar"$PROJECT_PHP_VERSION" -usermod -a -G www-data vagrant -/etc/init.d/apache2 restart + +# Nginx default conf +sudo perl -ne 'if ( m|\#location.*php\$ \{| .. m|^\s*#\}| ) { s/#//g; } print' -i /etc/nginx/sites-available/default +sed -i "s|fastcgi_pass unix:/var/run/php/.*|fastcgi_pass unix:/var/run/php/php${PROJECT_PHP_VERSION}-fpm.sock;|" /etc/nginx/sites-available/default +sed -i "s/With php-.*//" /etc/nginx/sites-available/default +sed -i "s/fastcgi_pass 127.0.0.1:9000;//" /etc/nginx/sites-available/default +sed -i 's/index index.html index.htm index.nginx-debian.html;/index index.php index.html index.htm index.nginx-debian.html;/' /etc/nginx/sites-available/default + + +# ----------------------------------------------------------------------------------------------------- + + +# Server permissions +mkdir -p /var/www/html # Adminer in default mkdir -p /var/www/html/adminer @@ -228,8 +209,11 @@ curl -sL -o /var/www/html/adminer/index.php https://www.adminer.org/latest-en.ph # Add php info mkdir -p /var/www/html/php/ -cat < /var/www/html/php/index.php +cat <<-EOF > /var/www/html/php/index.php /etc/apache2/sites-available/010-$PROJECT_NAME.conf - - ServerName ${PROJECT_URL} - DocumentRoot "/var/www/${PROJECT_NAME}" - SetEnv VAGRANT 1 - ErrorLog /var/log/apache2/${PROJECT_NAME}.error.log - CustomLog /var/log/apache2/${PROJECT_NAME}.access.log combined - SetEnvIf X-Forwarded-Proto https HTTPS=on - - Order Deny,Allow - Allow from all - AllowOverride All - - +# Nginx project conf +cat <<-EOF > /etc/nginx/sites-available/010-$PROJECT_NAME +upstream fastcgi_backend { + server unix:/run/php/php${PROJECT_PHP_VERSION}-fpm.sock; +} + +server { + listen 80; + listen [::]:80; + listen 443 ssl http2; + listen [::]:443 ssl http2; + + server_name ${PROJECT_URL} www.${PROJECT_URL}; + set MAGE_ROOT /var/www/html/${PROJECT_NAME}; + include /home/vagrant/extra/${PROJECT_NAME}.nginx.conf; + + ssl_certificate /etc/ssl/certs/www.${PROJECT_URL}.crt; + ssl_certificate_key /etc/ssl/private/www.${PROJECT_URL}.key; + ssl_protocols TLSv1.2 TLSv1.1 TLSv1; + + access_log /var/log/nginx/${PROJECT_NAME}.access.log; + error_log /var/log/nginx/${PROJECT_NAME}.error.log error; +} EOF -a2ensite 010-"$PROJECT_NAME" -usermod -a -G www-data "$PROJECT_USER" +sed -i 's/MAGE_ROOT/$MAGE_ROOT/' /etc/nginx/sites-available/010-$PROJECT_NAME +ln -sfn /etc/nginx/sites-available/010-$PROJECT_NAME /etc/nginx/sites-enabled/010-$PROJECT_NAME # Permission script -cat < /home/vagrant/permission.bak +cat <<-EOF > /home/vagrant/permission.bak if [ "$PROJECT_MOUNT" != "nfs" ] || [ "$PROJECT_MOUNT_PATH" == "app" ]; then -echo 'Applying permissions to $PROJECT_PATH project' +echo 'Applying permissions to $PROJECT_PATH' cd "$PROJECT_PATH" \\ && 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 find ./var ./generated -type d -exec chmod 777 {} \; \\ && sudo chmod u+x bin/magento \\ -&& sudo chown -fR $PROJECT_USER:www-data . || : +&& sudo chown -fR :www-data . || : fi EOF grep '[^[:blank:]]' < /home/vagrant/permission.bak > /usr/local/bin/permission @@ -53,11 +62,10 @@ chmod +x /usr/local/bin/permission chmod 600 /home/vagrant/.ssh/id_rsa chmod 600 /home/vagrant/.ssh/id_rsa.pub rm -rf /home/vagrant/.ssh/known_hosts /home/vagrant/.ssh/config -rm -rf /home/"$PROJECT_USER"/.ssh/known_hosts /home/"$PROJECT_USER"/.ssh/config echo -e "StrictHostKeyChecking no\n" >> /home/vagrant/.ssh/config ssh-keyscan -t rsa ${PROJECT_HOST_REPOSITORY} >> /home/vagrant/.ssh/known_hosts mkdir -p /home/vagrant/.composer -cat < /home/vagrant/.composer/auth.json +cat <<-EOF > /home/vagrant/.composer/auth.json { "http-basic": { "repo.magento.com": { @@ -73,23 +81,22 @@ if [ "$PROJECT_SOURCE" != "composer" ]; then sudo -u vagrant git config --global user.name "$PROJECT_GIT_USER" sudo -u vagrant git config --global user.email "$PROJECT_GIT_EMAIL" sudo -u vagrant git config --global core.filemode false - cp /home/vagrant/.gitconfig /home/"$PROJECT_USER"/.gitconfig fi -# Copy credentials to project user +# Reapply rights for vagrant user chown -R vagrant:vagrant /home/vagrant -mkdir -p /home/"$PROJECT_USER"/.composer /home/"$PROJECT_USER"/.ssh -cp -r /home/vagrant/.composer/* /home/"$PROJECT_USER"/.composer/ -cp -r /home/vagrant/.ssh/* /home/"$PROJECT_USER"/.ssh/ -chown -R "$PROJECT_USER":"$PROJECT_USER" /home/"$PROJECT_USER" + +# Execute import sql +if [ -f /home/vagrant/extra/db-dump.sql.gz ]; then + rm -f /home/vagrant/extra/db-dump.sql + gunzip /home/vagrant/extra/db-dump.sql.gz +fi +if [ -f /home/vagrant/extra/db-dump.sql ]; then + echo '--- Magento db dump import ---' + mysql -u vagrant -pvagrant -e "USE ${PROJECT_NAME};SET FOREIGN_KEY_CHECKS = 0;source /home/vagrant/extra/db-dump.sql;SET FOREIGN_KEY_CHECKS = 1;" +fi # Extra pre-build if [ -f /home/vagrant/extra/100-pre-build.sh ]; then bash /home/vagrant/extra/100-pre-build.sh fi - -# Restart services -/etc/init.d/apache2 restart -/etc/init.d/mysql restart -/etc/init.d/redis-server restart -/etc/init.d/postfix restart diff --git a/provision/110-magento-app.sh b/provision/110-magento-app.sh index f66ace0..600f196 100644 --- a/provision/110-magento-app.sh +++ b/provision/110-magento-app.sh @@ -12,24 +12,24 @@ export DEBIAN_FRONTEND=noninteractive echo '--- Magento installation sequence ---' # Prepare directory -DIRECTORY_BUILD="/srv" +DIRECTORY_BUILD="/home/vagrant" if [ "$PROJECT_MOUNT_PATH" == "app" ]; then DIRECTORY_BUILD="/tmp" fi PROJECT_BUILD="$DIRECTORY_BUILD/$PROJECT_NAME" rm -rf "$PROJECT_BUILD" &> /dev/null -chmod -R 777 /srv /tmp +chmod -R 777 /tmp mkdir -p "$PROJECT_BUILD" -chown -fR "$PROJECT_SETUP_OWNER":"$PROJECT_SETUP_OWNER" "$PROJECT_BUILD" +chown -fR vagrant:vagrant "$PROJECT_BUILD" # Get installation files from source if [ "$PROJECT_SOURCE" == "composer" ]; then # Install from magento - sudo -u "$PROJECT_SETUP_OWNER" composer create-project --no-interaction --no-install --no-progress \ + sudo -u vagrant composer create-project --no-interaction --no-install --no-progress \ --repository=https://repo.magento.com/ magento/project-"$PROJECT_EDITION"-edition="$PROJECT_VERSION" "$PROJECT_NAME" -d "$DIRECTORY_BUILD" # Install sample data if [ "$PROJECT_SAMPLE" == "true" ]; then - sudo -u "$PROJECT_SETUP_OWNER" composer require -d "$PROJECT_BUILD" \ + sudo -u vagrant composer require -d "$PROJECT_BUILD" \ magento/module-bundle-sample-data magento/module-widget-sample-data \ magento/module-theme-sample-data magento/module-catalog-sample-data \ magento/module-customer-sample-data magento/module-cms-sample-data \ @@ -43,17 +43,12 @@ if [ "$PROJECT_SOURCE" == "composer" ]; then fi else # Install from git - sudo -u "$PROJECT_SETUP_OWNER" git clone "$PROJECT_REPOSITORY" "$PROJECT_BUILD" - cd "$PROJECT_BUILD"; sudo -u "$PROJECT_SETUP_OWNER" git fetch --all; git checkout "$PROJECT_SOURCE" --force; -fi - -# Apply basic rights on regular mount -if [ "$PROJECT_MOUNT" != "nfs" ] || [ "$PROJECT_MOUNT_PATH" == "app" ]; then - chown -fR "$PROJECT_SETUP_OWNER":www-data "$PROJECT_PATH" + sudo -u vagrant git clone "$PROJECT_REPOSITORY" "$PROJECT_BUILD" + cd "$PROJECT_BUILD"; sudo -u vagrant git fetch --all; git checkout "$PROJECT_SOURCE" --force; fi # Composer install -sudo -u "$PROJECT_SETUP_OWNER" composer install -d "$PROJECT_BUILD" --no-progress --no-interaction --no-suggest +sudo -u vagrant composer install -d "$PROJECT_BUILD" --no-progress --no-interaction --no-suggest # Rsync directory if [ "$PROJECT_BUILD" != "$PROJECT_PATH" ]; then @@ -61,23 +56,28 @@ if [ "$PROJECT_BUILD" != "$PROJECT_PATH" ]; then fi # Symlink -rm -rf /var/www/"$PROJECT_NAME" -ln -sfn /srv/"$PROJECT_NAME" /var/www/"$PROJECT_NAME" +rm -rf /var/www/html/"$PROJECT_NAME" +ln -sfn "$PROJECT_PATH" /var/www/html/"$PROJECT_NAME" + +# Apply basic rights on regular mount +if [ "$PROJECT_MOUNT" != "nfs" ] || [ "$PROJECT_MOUNT_PATH" == "app" ]; then + chown -fR :www-data "$PROJECT_PATH" +fi # Run install chmod +x "$PROJECT_PATH"/bin/magento -sudo -u "$PROJECT_SETUP_OWNER" "$PROJECT_PATH"/bin/magento setup:uninstall -n -q -sudo -u "$PROJECT_SETUP_OWNER" "$PROJECT_PATH"/bin/magento setup:install \ +sudo -u vagrant "$PROJECT_PATH"/bin/magento setup:uninstall -n -q +sudo -u vagrant "$PROJECT_PATH"/bin/magento setup:install \ --base-url="http://${PROJECT_URL}/" \ --base-url-secure="https://${PROJECT_URL}/" \ --db-host="localhost" \ --db-name="${PROJECT_NAME}" \ --db-user="vagrant" \ --db-password="vagrant" \ ---admin-firstname="admin" \ ---admin-lastname="admin" \ +--admin-firstname="magento.admin" \ +--admin-lastname="magento.admin" \ --admin-email="${PROJECT_GIT_EMAIL}" \ ---admin-user="admin" \ +--admin-user="magento.admin" \ --admin-password="admin123" \ --language="${PROJECT_LANGUAGE}" \ --currency="${PROJECT_CURRENCY}" \ diff --git a/provision/120-magento-post.sh b/provision/120-magento-post.sh index c0a5a8c..fd3775b 100644 --- a/provision/120-magento-post.sh +++ b/provision/120-magento-post.sh @@ -18,16 +18,23 @@ ln -sf "$PROJECT_PATH"/bin/magento /usr/local/bin/magento # Composer config if [ "$PROJECT_SOURCE" == "composer" ]; then # Enable php ini - if [ -f "${PROJECT_PATH}/php.ini.sample" ]; then - sudo -u "$PROJECT_SETUP_OWNER" cp "$PROJECT_PATH"/php.ini.sample "$PROJECT_PATH"/php.ini + if [ -f "${PROJECT_PATH}/php.ini.sample" ] && [ ! -f "${PROJECT_PATH}/php.ini" ]; then + sudo -u vagrant cp "$PROJECT_PATH"/php.ini.sample "$PROJECT_PATH"/php.ini fi # Enable npm - if [ -f "${PROJECT_PATH}/package.json.sample" ]; then - sudo -u "$PROJECT_SETUP_OWNER" cp "$PROJECT_PATH"/package.json.sample "$PROJECT_PATH"/package.json + if [ -f "${PROJECT_PATH}/package.json.sample" ] && [ ! -f "${PROJECT_PATH}/package.json" ]; then + sudo -u vagrant cp "$PROJECT_PATH"/package.json.sample "$PROJECT_PATH"/package.json fi # Enable grunt - if [ -f "${PROJECT_PATH}/Gruntfile.js.sample" ]; then - sudo -u "$PROJECT_SETUP_OWNER" cp "$PROJECT_PATH"/Gruntfile.js.sample "$PROJECT_PATH"/Gruntfile.js + if [ -f "${PROJECT_PATH}/Gruntfile.js.sample" ] && [ ! -f "${PROJECT_PATH}/Gruntfile.js" ]; then + sudo -u vagrant cp "$PROJECT_PATH"/Gruntfile.js.sample "$PROJECT_PATH"/Gruntfile.js + fi + # Enable nginx + if [ -f "${PROJECT_PATH}/nginx.conf.sample" ] && [ ! -f "${PROJECT_PATH}/nginx.conf" ]; then + sudo -u vagrant cp "$PROJECT_PATH"/nginx.conf.sample "$PROJECT_PATH"/nginx.conf + fi + if [ -f "$PROJECT_PATH/nginx.conf" ]; then + sudo -u vagrant cp "$PROJECT_PATH"/nginx.conf /home/vagrant/extra/${PROJECT_NAME}.nginx.conf; fi fi @@ -35,8 +42,8 @@ fi if [ -f "${PROJECT_PATH}/package.json" ] && [ -f "${PROJECT_PATH}/Gruntfile.js" ]; then cd "$PROJECT_PATH" \ && echo 'Executing npm install...' \ - && sudo -u "$PROJECT_SETUP_OWNER" npm install &> /dev/null \ - && sudo -u "$PROJECT_SETUP_OWNER" npm update + && sudo -u vagrant npm install &> /dev/null \ + && sudo -u vagrant npm update fi # Change materialization strategy for nfs @@ -44,17 +51,12 @@ if [ "$PROJECT_MOUNT" == "nfs" ] && [ "$PROJECT_MOUNT_PATH" != "app" ]; then if [ -f "${PROJECT_PATH}/.git/config" ]; then git --git-dir "$PROJECT_PATH"/.git update-index --assume-unchanged app/etc/di.xml fi - sudo -u "$PROJECT_SETUP_OWNER" sed -i 's/Magento\\\Framework\\\App\\\View\\\Asset\\\MaterializationStrategy\\\Symlink/Magento\\\Framework\\\App\\\View\\\Asset\\\MaterializationStrategy\\\Copy/' "$PROJECT_PATH"/app/etc/di.xml + sudo -u vagrant sed -i 's/Magento\\\Framework\\\App\\\View\\\Asset\\\MaterializationStrategy\\\Symlink/Magento\\\Framework\\\App\\\View\\\Asset\\\MaterializationStrategy\\\Copy/' "$PROJECT_PATH"/app/etc/di.xml fi -# Clean compiled files and cache -rm -rf "$PROJECT_PATH"/var/generation/ -rm -rf "$PROJECT_PATH"/generated/code/ -sudo -u "$PROJECT_SETUP_OWNER" "$PROJECT_PATH"/bin/magento cache:clean - # Magento config if $(dpkg --compare-versions "${PROJECT_VERSION}" "gt" "2.2"); then - sudo -u "$PROJECT_SETUP_OWNER" "$PROJECT_PATH"/bin/magento setup:config:set \ + sudo -u vagrant "$PROJECT_PATH"/bin/magento setup:config:set \ --cache-backend=redis \ --cache-backend-redis-server=127.0.0.1 \ --cache-backend-redis-port=6379 \ @@ -65,21 +67,28 @@ if $(dpkg --compare-versions "${PROJECT_VERSION}" "gt" "2.2"); then --page-cache-redis-db=1 \ --page-cache-redis-compress-data=1 - sudo -u "$PROJECT_SETUP_OWNER" "$PROJECT_PATH"/bin/magento setup:config:set \ + sudo -u vagrant "$PROJECT_PATH"/bin/magento setup:config:set \ --session-save=redis \ --session-save-redis-host=127.0.0.1 \ --session-save-redis-port=6379 \ --session-save-redis-db=2 - sudo -u "$PROJECT_SETUP_OWNER" "$PROJECT_PATH"/bin/magento config:set "admin/security/session_lifetime" "31536000" - sudo -u "$PROJECT_SETUP_OWNER" "$PROJECT_PATH"/bin/magento config:set "admin/security/lockout_threshold" "180" - sudo -u "$PROJECT_SETUP_OWNER" "$PROJECT_PATH"/bin/magento config:set "admin/security/password_lifetime" "" - sudo -u "$PROJECT_SETUP_OWNER" "$PROJECT_PATH"/bin/magento config:set "admin/security/password_is_forced" "0" - sudo -u "$PROJECT_SETUP_OWNER" "$PROJECT_PATH"/bin/magento config:set "web/secure/use_in_adminhtml" "1" + sudo -u vagrant "$PROJECT_PATH"/bin/magento config:set "admin/security/session_lifetime" "31536000" + sudo -u vagrant "$PROJECT_PATH"/bin/magento config:set "admin/security/lockout_threshold" "180" + sudo -u vagrant "$PROJECT_PATH"/bin/magento config:set "admin/security/password_lifetime" "" + sudo -u vagrant "$PROJECT_PATH"/bin/magento config:set "admin/security/password_is_forced" "0" + sudo -u vagrant "$PROJECT_PATH"/bin/magento config:set "web/secure/use_in_adminhtml" "1" + sudo -u vagrant "$PROJECT_PATH"/bin/magento config:set "web/secure/use_in_frontend" "1" + sudo -u vagrant "$PROJECT_PATH"/bin/magento config:set "web/secure/enable_hsts" "1" +else + # Force https on unsecure request for older versions + mysql -u vagrant -pvagrant -e "USE ${PROJECT_NAME}; UPDATE core_config_data set value='https://${PROJECT_URL}/' where path='web/unsecure/base_url';" fi -# Set https for unsecure request -mysql -u vagrant -pvagrant -e "USE ${PROJECT_NAME}; UPDATE core_config_data set value='https://${PROJECT_URL}/' where path='web/unsecure/base_url';" +# Set crypt key +if [ ! -z "$PROJECT_CRYPT_KEY" ] && [ -f /home/vagrant/extra/db-dump.sql ]; then + sudo -u vagrant bin/magento setup:config:set -n --key ${PROJECT_CRYPT_KEY} +fi # Extra post-build if [ -f /home/vagrant/extra/120-post-build.sh ]; then @@ -94,11 +103,14 @@ fi # Clean compiled files rm -rf "$PROJECT_PATH"/var/generation/ rm -rf "$PROJECT_PATH"/generated/code/ -sudo -u "$PROJECT_SETUP_OWNER" "$PROJECT_PATH"/bin/magento setup:upgrade -sudo -u "$PROJECT_SETUP_OWNER" "$PROJECT_PATH"/bin/magento deploy:mode:set "$PROJECT_MODE" -sudo -u "$PROJECT_SETUP_OWNER" "$PROJECT_PATH"/bin/magento cache:enable - -# Restart -/etc/init.d/apache2 restart +sudo -u vagrant "$PROJECT_PATH"/bin/magento setup:upgrade +sudo -u vagrant "$PROJECT_PATH"/bin/magento deploy:mode:set "$PROJECT_MODE" +sudo -u vagrant "$PROJECT_PATH"/bin/magento cache:enable + +# Restart services +/etc/init.d/php${PROJECT_PHP_VERSION}-fpm restart +/etc/init.d/nginx restart +/etc/init.d/mysql restart /etc/init.d/redis-server restart +/etc/init.d/postfix restart permission