If you use WP-cli command on a multisite it be default will only run on the mainsite. But often you want to change a setting for all the sites. In my case I wanted to set the timezone to Amsterdam for the whole network. That’s not hard:
wp option set timezone_string 'Europe/Amsterdam'
On a multisite this is a bit more difficult. But the script below will do the same for each site in a multisite.
wp site list --field=url | xargs -I % sh -c 'printf "SITE: %\n"; wp option set timezone_string 'Europe/Amsterdam' --url=%'
It consists of 3 parts. First create a list of all site url’s
wp site list --field=url
Secondly we pass that on to xargs. xargs is a very powerfull tool. One that I hardly understand and should go into deeper one day. This is the best tutorial I found if you want to start with xargs.
The only thing important now is the -I %. This sets the variable to %. But the most important thing here is that inside the '***' You can run any command. like normal.
xargs -I % sh -c '***'
Which brings us to the final part. First print the site url on a line, then do the actual command we want to do on each sub site. As you can see we pass on the --url=% where we set the variable given in xargs.
printf "SITE: %\n"; wp option set timezone_string 'Europe/Amsterdam' --url=%
As of the release of VVV 3.0 this can all be done by adding the following in the vvv-custom.yml
# Backup the databases to the database/backups subfolder on halt/suspend/destroy, set to false to disable
# Import the databases if they're missing from backups
Pre VVV 3.0
VVV is great but if you have 20+ sites in it most of which are quite big doing a reload or halt can be quite slow. Disabling it isn’t the easiest. In vagrant-root/config/homebin/ create these 3 files:
I wish there was a way to easily disable these. The vvv-custom.yml would be great for this. Also adding a way to exclude specific databases.
Only useful for simple onelines. You’re very likely to be better of putting code in a php file and run that script like:
Run PHP with WordPress loaded.
Of course your good old friend wp-cli can help. It can run code with wp fully loaded. So if you add things in the init action or even after the wp_loaded, those plugin/theme functions, posttypes and such are all available.
First off the plain php code execution, with wp eval
Note: this post call also be used to install php7.0, php7.1, php7.2 or php7.3, just change every reference to 7.x. And skip right to “Actual install php“
Default Rasbian still ships with php7.3, not that weird as at time of writing 7.4 is only released for about a month. We will install the latest php7.4. But because this is not the default we will need to do a few extra tweaks.
Add newer source
We are going to create a new source for php. We do this with the following 3 commands
When done check it with php -v it should show a php 7.4.0 or higher. Now we need to add a few fpm things for nginx to work properly. Create a extra config file sudo nano /etc/php/7.4/fpm/conf.d/90-pi-custom.ini. And add:
In this step we are going to install a webserver (nginx). For those who are bit familiar with webservers you might wonder why we are not using apache? I myself are far more familiar with Apache but Nginx just has better performance. And on the not so powerful pi that is important.
To start we just plainly install nginx.
sudo apt install -y nginx
Check the version nginx -v it should be version nginx/1.14.2 or higher
To test if it works enter the IP address in the browser. You should see this page.
If you see an error most likely IPv6 isn’t supported. Open the nginx config file sudo nano /etc/nginx/sites-enabled/default. At the top you should see these lines
First we will do some global settings open: sudo nano /etc/nginx/conf.d/90-pi-custom..conf Add the following line, we increase the allowed upload sizes.
Note: for the example I use example.local replace that with the url you want to use. If you don’t have the url setup yet you can add it to you own host file. Or instead of the url use the IP-address of the Pi.
We will create our own vhost files. PS I’ll be using example.local but use whatever you intend to use. Create the vhost file: sudo nano /etc/nginx/sites-enabled/example.local.conf
So far we use sudo for pretty much everything. This is fine. After the initial setup most of the work has to be done in /var/www/*. These files are owned by the www-data user. This also is normal and fine. We can change Nginx to use the pi user.
That way you can avoid the sudo for every small change. This is a security risk of course. The big upside is no more sudo and changing owners of files. The downside is that every file pi can access Nginx also can access.
Tips when using the default Nginx user.
Files created by Nginx will have owner and group www-data
The default pi user can’t edit these. When editing use sudo If you are changing a lot you want want too temporarily change the entire folder to the pi user.
Change user to PI sudo chown pi:pi -R /var/www/PATH
Change user back to the Nginx default. sudo chown www-data:www-data -R /var/www/PATH
There is a separate user for changing the user Nginx so it won’t use www-data. This is a security risk, so I don’t recommend it, The rest of the guide(s) acts as if you didn’t do this.
This is the followup of Opening a Raspberry Pi to the outside world On the internet bots constantly try to hack servers. The Raspberry Pi is popular target. That’s why by default ssh is disabled nowadays. To make this more secure we will only allow login by ssh key instead of a password. Ssh keys are a lot safer and you also won’t need to type/paste the password on login.
Open file: sudo nano /etc/ssh/sshd_config
Look for PasswordAuthentication remove the # at the front and change the value to no.
As a fall-back we will allow login in from the home network. So at the bottom of the file add. (the 4 spaces in front of the line matter)
Match address 192.168.*.*
Restart the ssh service. sudo service ssh restart
Generate keys on the raspberry pi: ssh-keygen -t rsa -b 4096 -C "pi-webserver" Just press enter for both the location and password. Add your local key. Form your own machine mac/linux/windows.
Open File: sudo nano .ssh/authorized_keys
Paste your public key in this file. For linux/mac you can find your public key with: cat ~/.ssh/id_rsa.pub
In a new terminal tab/window, try to login again. This way you can change things in the current tab. If something went wrong. You should login without needing to type the password. To check if the password is disabled you should try login in from a different computer and a different ip.
This guide will help you set up a Raspberry Pi with Rasbian. Ready to do pretty much whatever you want.
What hardware do you need?
First off a Raspberry Pi of course, this tutorial uses a 4B 4GB. And should also work on a Model 2B and 3B.
Second I have a network cable. Wifi is available on since the 3B but wire always has a more stable connection.
A USB-C power source. This can get a bit tricky. Recommended is at least 2A with 5V. A normal smartphone adapter won’t give that. Using less will slow down your Raspberry. If you’re using a 2B or 3B you need micro USB instead of USB-c
A micro SD card, at least class 10 for speed and with 8GB or more. The SD card will serve as the main harddrive for the pi. An micro SD adapter is also needed to install the image on the SD card.
A Case, simply a case to better protect your Raspberry.
First off download Raspbian Buster Lite from the Raspberry pi website. When It’s done flash the image on the SD card. There is an official guide that explains it better then what I could do.
Flashing the image can take a while. You could set up all the wires and put things in place.
After the SD card is done
When the writing of the SD card is done. Put a file with the name (of empty folder) named ssh in the root of the boot partition.
Also in the boot partition is a file called cmdline.txt. Open that, and at the end of the line add the IP you want to give the Pi
If you need wifi place a file named wpa_supplicant.conf also in the boot partition. And add the following:
Then plug in the SD card and powerup. Do wait 2~5minutes before actually trying. The Raspberry has to setup stuff for the first time. If after 20max you still can’t find an IP. The flashing of the SD card has gone wrong and you’ll have to restart.
Go to your own computer and open the terminal (or Putty for the windows users) Login with the command ssh email@example.com.*.* Use the IP address you noted before. If it asks if you want to continue choose yes. and use the default password: raspberry Putty again will work a bit different.
Now you are logged-in remote on the raspberry pi. Time to configure some stuff with the command sudo raspi-config. On this menu we can configure some basic settings. Let’s change some settings.
Change User Password CHANGE THE PASSWORD. Longer is better and safer
Network options N1 Hostname the name of your Raspberry in your network. I named mine pi-webserver. Not required. It’s just a label. But useful if you got multiple Raspberry’s running.
Boot options B1 Desktop / CLI Chose the ‘Console Autologin’ or ‘Console’ option. Don’t choose auto login if someone else might have physical access to the pi. Desktop will just waste power. B2 Wait for Network at Boot Just turn this off to be safe.
Localisation Options Here you can change the timezone to your current one. You could change the language of the PI and the keyboard layout if you need to. Keep in mind I will be using English.
Interfacing Options we don’t need this
Overclock Only for the Pi 2B, Set this to the highest setting. It won’t hurt and will make the Pi a faster.
Advanced Options This will be a bit more. A1 Expend Filesystem Run this. No need to reboot immediately. A3 Memory Split Set this to 16. Because we don’t have a interface we won’t need memory for that.
Update Run this is should not hurt. It’s only to update the raspi-config tool.
When this is finished the pi might download some language packs. It it doesn’t reboot. you have to do it yourself. sudo reboot now.
Wait about a minute and login in again like before. But with the new password
This might take a while but first we are going to update.
sudo apt update && sudo apt upgrade -y
Now we update all default installed software. If the image is old or hasn’t been updated in a while it might take a long time. The date of the image is in the file name of the downloaded image. At times It might appear to hang. It Unless it’s stuck on one thing 15 minutes it’s probably fine. Give it an 15 to 30 minutes. Else pull the plug.