Skip to content

Commit

Permalink
Fix setup (#1)
Browse files Browse the repository at this point in the history
* Automatically configure PHP

* Automatically configure NGINX

* Restart php script to prevent bad gateway

* Automated sendmail install

* Updated readme

* Added apache conf notes

* Added default name

* Removed hack and unused php config

* Documentation updates and restart every used php version

* Prevent vendor-expose

* Configure multiple php versions

* Added phpmyadmin to yaml example

* WIP custom site type with apache provision

* Set env variables script

* Working default werkbot site type
  • Loading branch information
tiller1010 committed Apr 9, 2024
1 parent d4c24a9 commit 385fe0f
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ wsl-init.log
.backup/*
.DS_Store
composer.lock

env.yaml
8 changes: 6 additions & 2 deletions Homestead.yaml.example
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
---
ip: "192.168.56.56"
memory: 2048
memory: 5000
cpus: 2
provider: virtualbox
name: 'homestead'

authorize: ~/.ssh/id_rsa.pub

Expand All @@ -14,9 +15,12 @@ folders:
to: /home/vagrant/code

sites:
- map: phpmyadmin.local
to: /home/vagrant/phpmyadmin
php: '8.0'
- map: homestead.test
to: /home/vagrant/code/public
php: 7.3
php: '7.3'

databases:
- LV_example
Expand Down
7 changes: 5 additions & 2 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ homesteadYamlPath = confDir + "/Homestead.yaml"
homesteadJsonPath = confDir + "/Homestead.json"
afterScriptPath = confDir + "/after.sh"
customizationScriptPath = confDir + "/user-customizations.sh"
restartPHPScriptPath = confDir + "/restart-php.sh"
aliasesPath = confDir + "/aliases"

envConf = YAML::load(File.read(confDir + "/env.yaml"))

require File.expand_path(File.dirname(__FILE__) + '/scripts/homestead.rb')

Vagrant.require_version '>= 2.4'
Expand All @@ -37,11 +40,11 @@ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
Homestead.configure(config, settings)

if File.exist? afterScriptPath then
config.vm.provision "Run after.sh", type: "shell", path: afterScriptPath, privileged: false, keep_color: true
config.vm.provision "Run after.sh", type: "shell", path: afterScriptPath, privileged: false, keep_color: true, env: {'MAILUSER' => envConf['MAILUSER'], 'MAILPASSWORD' => envConf['MAILPASSWORD']}
end

if File.exist? customizationScriptPath then
config.vm.provision "Run customize script", type: "shell", path: customizationScriptPath, privileged: false, keep_color: true
config.vm.provision "Run customize script", type: "shell", path: customizationScriptPath, privileged: false, keep_color: true, run: 'always'
end

if Vagrant.has_plugin?('vagrant-hostsupdater')
Expand Down
120 changes: 117 additions & 3 deletions after.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@



# Check If phpmyadmin Has Been Installed
# Check if phpmyadmin Has Been Installed
if [ -f /home/vagrant/.homestead-features/phpmyadmin ]
then
echo "phpmyadmin already installed."
Expand Down Expand Up @@ -51,7 +51,7 @@ fi



# Check If Google Chrome Has Been Installed
# Check if Google Chrome Has Been Installed
if [ -f /home/vagrant/.homestead-features/google-chrome ]
then
echo "Google Chrome already installed."
Expand All @@ -64,4 +64,118 @@ else
sudo apt install ./google-chrome-stable_current_amd64.deb
rm google-chrome-stable_current_amd64.deb
fi
# End Google Chrome
# End Google Chrome



# Check if PHP has been configured
if [ -f /home/vagrant/.homestead-features/php ]
then
echo "PHP already configured."
else
# PHP 7.3
touch /home/vagrant/.homestead-features/php
sudo chmod 777 /etc/php/7.3/fpm/php.ini
PHP_INI=$(cat /etc/php/7.3/fpm/php.ini)
PHP_INI=$(echo "$PHP_INI" | sed 's/upload_max_filesize =.*/upload_max_filesize = 0/')
PHP_INI=$(echo "$PHP_INI" | sed 's/post_max_size =.*/post_max_size = 0/')
PHP_INI=$(echo "$PHP_INI" | sed 's/max_execution_time =.*/max_execution_time = 0/')
PHP_INI=$(echo "$PHP_INI" | sed 's/max_input_time =.*/max_input_time = 0/')
# sendmail config
PHP_INI=$(echo "$PHP_INI" | sed 's/;mail.force_extra_parameters =.*/mail.force_extra_parameters = "-f %s"/')
PHP_INI=$(echo "$PHP_INI" | sed 's/mail.add_x_header = Off/mail.add_x_header = On/')
sudo echo "$PHP_INI" > /etc/php/7.3/fpm/php.ini
sudo service php7.3-fpm restart

# PHP 7.4
touch /home/vagrant/.homestead-features/php
sudo chmod 777 /etc/php/7.4/fpm/php.ini
PHP_INI=$(cat /etc/php/7.4/fpm/php.ini)
PHP_INI=$(echo "$PHP_INI" | sed 's/upload_max_filesize =.*/upload_max_filesize = 0/')
PHP_INI=$(echo "$PHP_INI" | sed 's/post_max_size =.*/post_max_size = 0/')
PHP_INI=$(echo "$PHP_INI" | sed 's/max_execution_time =.*/max_execution_time = 0/')
PHP_INI=$(echo "$PHP_INI" | sed 's/max_input_time =.*/max_input_time = 0/')
# sendmail config
PHP_INI=$(echo "$PHP_INI" | sed 's/;mail.force_extra_parameters =.*/mail.force_extra_parameters = "-f %s"/')
PHP_INI=$(echo "$PHP_INI" | sed 's/mail.add_x_header = Off/mail.add_x_header = On/')
sudo echo "$PHP_INI" > /etc/php/7.4/fpm/php.ini
sudo service php7.4-fpm restart

# PHP 8.0
touch /home/vagrant/.homestead-features/php
sudo chmod 777 /etc/php/8.0/fpm/php.ini
PHP_INI=$(cat /etc/php/8.0/fpm/php.ini)
PHP_INI=$(echo "$PHP_INI" | sed 's/upload_max_filesize =.*/upload_max_filesize = 0/')
PHP_INI=$(echo "$PHP_INI" | sed 's/post_max_size =.*/post_max_size = 0/')
PHP_INI=$(echo "$PHP_INI" | sed 's/max_execution_time =.*/max_execution_time = 0/')
PHP_INI=$(echo "$PHP_INI" | sed 's/max_input_time =.*/max_input_time = 0/')
# sendmail config
PHP_INI=$(echo "$PHP_INI" | sed 's/;mail.force_extra_parameters =.*/mail.force_extra_parameters = "-f %s"/')
PHP_INI=$(echo "$PHP_INI" | sed 's/mail.add_x_header = Off/mail.add_x_header = On/')
sudo echo "$PHP_INI" > /etc/php/8.0/fpm/php.ini
sudo service php8.0-fpm restart
fi
# End PHP



# Check if NGINX has been configured
if [ -f /home/vagrant/.homestead-features/nginx ]
then
echo "NGINX already configured."
else
touch /home/vagrant/.homestead-features/nginx
sudo chmod 777 /etc/nginx/nginx.conf
NGINX_CONF=$(cat /etc/nginx/nginx.conf)
NGINX_CONF=$(echo "$NGINX_CONF" | sed 's/http {/http {\nclient_max_body_size 0;/')
sudo echo "$NGINX_CONF" > /etc/nginx/nginx.conf
sudo systemctl restart nginx
fi
# End NGINX



# Check if sendmail has been installed and configured
if [ -f /home/vagrant/.homestead-features/sendmail ]
then
echo "sendmail already configured."
else
touch /home/vagrant/.homestead-features/sendmail
yes | sudo apt-get install sasl2-bin
yes | sudo apt-get install sendmail
yes | sudo sendmailconfig
sudo mkdir /etc/mail/authinfo
sudo chmod 777 /etc/mail/authinfo
sudo echo 'AuthInfo: "U:root" "I:'$MAILUSER'" "P:'$MAILPASSWORD'"' > /etc/mail/authinfo/gmail-auth
sudo makemap hash /etc/mail/authinfo/gmail-auth < /etc/mail/authinfo/gmail-auth
sudo chmod 777 /etc/mail/sendmail.mc
SENDMAILMC=$(cat /etc/mail/sendmail.mc)
NEWSENDMAILMCSETTINGS=$(echo -e "\ndefine(\`SMART_HOST',\`[smtp.gmail.com]')dnl\ndefine(\`RELAY_MAILER_ARGS', \`TCP \$h 587')dnl\ndefine(\`ESMTP_MAILER_ARGS', \`TCP \$h 587')dnl\ndefine(\`confAUTH_OPTIONS', \`A p')dnl\nTRUST_AUTH_MECH(\`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl\ndefine(\`confAUTH_MECHANISMS', \`EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl\nFEATURE(\`authinfo',\`hash -o /etc/mail/authinfo/gmail-auth.db')dnl")
SENDMAILMC="${SENDMAILMC}${NEWSENDMAILMCSETTINGS}"
SENDMAILMC=$(echo "$SENDMAILMC" | sed 's/MAILER_DEFINITIONS/dnl #/')
SENDMAILMC=$(echo "$SENDMAILMC" | sed 's/MAILER(`local.*/dnl #/')
SENDMAILMC=$(echo "$SENDMAILMC" | sed 's/MAILER(`smtp.*/dnl #/')
MAILERDEFINITIONS=$(echo -e "\nMAILER_DEFINITIONS\nMAILER(\`local')dnl\nMAILER(\`smtp')dnl")
SENDMAILMC="${SENDMAILMC}${MAILERDEFINITIONS}"
sudo echo "$SENDMAILMC" > /etc/mail/sendmail.mc
sudo make -C /etc/mail
sudo /etc/init.d/sendmail reload
yes | sudo sendmailconfig
sudo chmod 777 /etc/hosts
HOSTS=$(cat /etc/hosts)
HOSTS=$(echo "$HOSTS" | sed 's/localhost/localhost\.localdomain localhost homestead/')
sudo echo "$HOSTS" > /etc/hosts
sudo systemctl restart nginx
fi
# End sendmail


# Check if env variables have been configured
if [ -f /home/vagrant/.homestead-features/envvariables ]
then
echo "sendmail already configured."
else
touch /home/vagrant/.homestead-features/envvariables
echo "export SS_VENDOR_METHOD=none" >> /home/vagrant/.profile
echo "export SS_VENDOR_METHOD=none" >> /home/vagrant/.bashrc
fi
2 changes: 2 additions & 0 deletions env.yaml.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MAILUSER: 'developer@werkbot.com'
MAILPASSWORD: ''
91 changes: 33 additions & 58 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,39 @@ Map site in Homestead.yaml:

Add site to main machine hosts file

192.168.10.10 phpmyadmin.local
192.168.56.56 phpmyadmin.local

Login:
- user: homestead
- password: secret

### sendmail
- Not required for Laravel. Mail configuration is set in the project .env
- Send test email `echo "test message" | sendmail -v youremail@gmail.com` (this could take several minutes to receive)

### apache2
- Used for 301 redirects
- Disable nginx `sudo systemctl stop nginx`
- `sudo vi /etc/apache2/apache2.conf`
- Update `<Directory />` block

<Directory />
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>

- Add site conf `sudo vi /etc/apache2/sites-available/sitename.local.conf`

<VirtualHost *:80>
ServerAdmin email@email.com
DocumentRoot /home/vagrant/sitename/public
ServerName sitename.local
</VirtualHost>

- Add symlink to sites-enabled `sudo ln -s /etc/apache2/sites-available/sitename.local.conf /etc/apache2/sites-enabled/sitename.local.conf`
- Restart apache `sudo systemctl restart apache2`

## Config
### Homestead.yaml

Expand All @@ -73,74 +100,22 @@ Login:
sites:
- map: sitename.local
to: /home/vagrant/sitename/public
php: '8.0'
php: '7.3'
databases:
- sitename
features:
- mariadb: true

- SilverStripe sites will typically use php 7.3
- Laravel apps will typically use php 7.4

### Start Homestead
- Run `vagrant up --provision`
- Run `vagrant ssh`

### PHP
- Run `sudo vi /etc/php/8.0/fpm/php.ini`
- Change following values to 0:

upload_max_filesize 0
post_max_size 0
max_execution_time 0
max_input_time 0

### Nginx
- Run `sudo vi /etc/nginx/nginx.conf`
- Add `client_max_body_size 0;` in http brackets
- Run `sudo systemctl restart nginx`

### Sendmail
- Run `sudo apt-get install sendmail`
- Run `sudo sendmailconfig` and answer yes to all
- Run `sudo mkdir /etc/mail/authinfo/`
- Run `cd /etc/mail/authinfo/`
- Run `sudo vi gmail-auth`
- Paste and edit `AuthInfo: "U:root" "I:developer@werkbot.com" "P:PASSWORD"`
- Run `sudo makemap hash gmail-auth < gmail-auth`
- Run `cd /` to navigate back
- Run `sudo vi /etc/mail/sendmail.mc`
- Paste the following right above the MAILER definitions

define(`SMART_HOST',`[smtp.gmail.com]')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl
define(`confAUTH_OPTIONS', `A p')dnl
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl
FEATURE(`authinfo',`hash -o /etc/mail/authinfo/gmail-auth.db')dnl

- Run `sudo make -C /etc/mail`
- Run `sudo /etc/init.d/sendmail reload`
- Send test email `echo "test message" | sendmail -v youremail@gmail.com`
- Open the php.ini file for your project's php version. e.g. `sudo vi /etc/php/7.4/fpm/php.ini`
- Set `SMTP = smtp.gmail.com`
- Set `sendmail_from = developer@werkbot.com`
- Set `sendmail_path = /usr/sbin/sendmail`
- Run `sudo vi /etc/hosts`
- Change:

127.0.0.1 localhost
127.0.1.1 homestead homestead

- To:

127.0.0.1 localhost.localdomain localhost homestead
127.0.1.1 homestead homestead

- Run `sudo systemctl restart nginx`
- For SilverStripe, until I find a better solution, change vendor/swiftmailer/swiftmailer/lib/classes/Swift/MailTransport.php constructor default from `-f%s` to `-f %s`

## PHP Versions
### Composer
- Run composer with different versions of PHP `php7.4 /usr/local/bin/composer update`

### Artisan
- Run artisan with different versions of PHP `php7.4 artisan migrate`
- Run artisan with different versions of PHP `php7.4 artisan migrate`
6 changes: 6 additions & 0 deletions restart-php.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

sudo service php7.3-fpm restart
sudo service php7.4-fpm restart
sudo service php8.0-fpm restart
sudo service php8.1-fpm restart
2 changes: 1 addition & 1 deletion scripts/homestead.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def self.configure(config, settings)
end
end

type = site['type'] ||= 'laravel'
type = site['type'] ||= 'werkbot'
load_balancer = settings['load_balancer'] ||= false
http_port = load_balancer ? '8111' : '80'
https_port = load_balancer ? '8112' : '443'
Expand Down
Loading

0 comments on commit 385fe0f

Please sign in to comment.