Skip to content

Commit b365c8d

Browse files
author
Daniel Hansson
authored
Merge pull request #5 from techandme/hostname
set hostname in startup script
2 parents 0931dfa + 1775462 commit b365c8d

File tree

5 files changed

+205
-47
lines changed

5 files changed

+205
-47
lines changed

static/ip.sh

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,30 @@ IFCONFIG="/sbin/ifconfig"
44
INTERFACES="/etc/network/interfaces"
55

66
IFACE=$(lshw -c network | grep "logical name" | awk '{print $3; exit}')
7-
ADDRESS=$($IFCONFIG | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
7+
ADDRESS=$(hostname -I | cut -d ' ' -f 1)
88
NETMASK=$($IFCONFIG | grep -w inet |grep -v 127.0.0.1| awk '{print $4}' | cut -d ":" -f 2)
99
GATEWAY=$(route -n|grep "UG"|grep -v "UGH"|cut -f 10 -d " ")
1010

1111
cat <<-IPCONFIG > "$INTERFACES"
1212
source /etc/network/interfaces.d/*
13-
1413
# The loopback network interface
15-
auto lo $IFACE
16-
iface lo inet loopback
17-
14+
auto lo $IFACE
15+
iface lo inet loopback
1816
# The primary network interface
19-
iface $IFACE inet static
20-
pre-up /sbin/ethtool -K $IFACE tso off
21-
pre-up /sbin/ethtool -K $IFACE gso off
22-
17+
iface $IFACE inet static
18+
pre-up /sbin/ethtool -K $IFACE tso off
19+
pre-up /sbin/ethtool -K $IFACE gso off
20+
# Fixes https://github.com/nextcloud/vm/issues/92:
21+
pre-up ip link set dev $IFACE mtu 1430
2322
# Best practice is to change the static address
2423
# to something outside your DHCP range.
25-
address $ADDRESS
26-
netmask $NETMASK
27-
gateway $GATEWAY
28-
24+
address $ADDRESS
25+
netmask $NETMASK
26+
gateway $GATEWAY
27+
# This is an autoconfigured IPv6 interface
28+
# iface $IFACE inet6 auto
2929
# Exit and save: [CTRL+X] + [Y] + [ENTER]
3030
# Exit without saving: [CTRL+X]
31-
3231
IPCONFIG
3332

3433
exit 0

static/ip2.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/sh
2+
3+
IFCONFIG="/sbin/ifconfig"
4+
INTERFACES="/etc/network/interfaces"
5+
6+
IFACE=$(lshw -c network | grep "logical name" | awk '{print $3; exit}')
7+
ADDRESS=$(hostname -I | cut -d ' ' -f 1)
8+
NETMASK=$($IFCONFIG | grep -w inet |grep -v 127.0.0.1| awk '{print $4}' | cut -d ":" -f 2)
9+
GATEWAY=$(route -n|grep "UG"|grep -v "UGH"|cut -f 10 -d " ")
10+
11+
cat <<-IPCONFIG > "$INTERFACES"
12+
source /etc/network/interfaces.d/*
13+
# The loopback network interface
14+
auto lo $IFACE
15+
iface lo inet loopback
16+
# The primary network interface
17+
iface $IFACE inet static
18+
pre-up /sbin/ethtool -K $IFACE tso off
19+
pre-up /sbin/ethtool -K $IFACE gso off
20+
# Fixes https://github.com/nextcloud/vm/issues/92:
21+
pre-up ip link set dev $IFACE mtu 1430
22+
# Best practice is to change the static address
23+
# to something outside your DHCP range.
24+
address $ADDRESS
25+
netmask $NETMASK
26+
gateway $GATEWAY
27+
# This is an autoconfigured IPv6 interface
28+
# iface $IFACE inet6 auto
29+
# Exit and save: [CTRL+X] + [Y] + [ENTER]
30+
# Exit without saving: [CTRL+X]
31+
IPCONFIG
32+
33+
exit 0

static/test_connection.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ WGET="/usr/bin/wget"
55
$WGET -q --tries=20 --timeout=10 http://www.google.com -O /tmp/google.idx &> /dev/null
66
if [ ! -s /tmp/google.idx ]
77
then
8-
echo -e "\e[31mNot Connected!"
8+
echo -e "\e[31mNot Connected!\e[0m"
99
else
10-
echo -e "\e[32mConnected! \o/"
10+
echo -e "Connected!"
1111
fi
12-
13-
exit 0

wordpress-startup-script.sh

Lines changed: 157 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
# Tech and Me - ©2017, https://www.techandme.se/
44

5+
# Check for errors + debug code and abort if something isn't right
6+
# 1 = ON
7+
# 0 = OFF
8+
DEBUG=0
9+
510
WWW_ROOT=/var/www/html
611
WPATH=$WWW_ROOT/wordpress
712
SCRIPTS=/var/scripts
@@ -13,44 +18,61 @@ PHPMYADMIN_CONF="/etc/apache2/conf-available/phpmyadmin.conf"
1318
STATIC="https://raw.githubusercontent.com/techandme/wordpress-vm/master/static"
1419
LETS_ENC="https://raw.githubusercontent.com/techandme/wordpress-vm/master/lets-encrypt"
1520

21+
# DEBUG mode
22+
if [ $DEBUG -eq 1 ]
23+
then
24+
set -e
25+
set -x
26+
else
27+
sleep 1
28+
fi
29+
1630
# Check if root
17-
if [ "$(whoami)" != "root" ]; then
18-
echo
19-
echo -e "\e[31mSorry, you are not root.\n\e[0mYou must type: \e[36msudo \e[0mbash $SCRIPTS/wordpress-startup-script.sh"
20-
echo
21-
exit 1
22-
fi
31+
if [ "$(whoami)" != "root" ]
32+
then
33+
echo
34+
echo -e "\e[31mSorry, you are not root.\n\e[0mYou must type: \e[36msudo \e[0mbash $SCRIPTS/wordpress-startup-script.sh"
35+
echo
36+
exit 1
37+
fi
2338

39+
# Check network
40+
echo "Testing if network is OK..."
41+
service networking restart
42+
curl -s http://github.com > /dev/null
43+
if [ $? -eq 0 ]
44+
then
45+
echo -e "\e[32mOnline!\e[0m"
46+
else
47+
echo "Setting correct interface..."
2448
# Set correct interface
2549
{ sed '/# The primary network interface/q' /etc/network/interfaces; printf 'auto %s\niface %s inet dhcp\n# This is an autoconfigured IPv6 interface\niface %s inet6 auto\n' "$IFACE" "$IFACE" "$IFACE"; } > /etc/network/interfaces.new
2650
mv /etc/network/interfaces.new /etc/network/interfaces
2751
service networking restart
52+
fi
2853

2954
# Check network
3055
echo "Testing if network is OK..."
31-
sleep 2
32-
sudo ifdown $IFACE && sudo ifup $IFACE
33-
wget -q --spider http://github.com
34-
if [ $? -eq 0 ]; then
35-
echo -e "\e[32mOnline!\e[0m"
36-
else
37-
echo
38-
echo "Network NOT OK. You must have a working Network connection to run this script."
39-
echo "Please report this to: https://github.com/techandme/wordpress-vm/issues/new"
40-
exit 1
41-
fi
56+
service networking restart
57+
curl -s http://github.com > /dev/null
58+
if [ $? -eq 0 ]
59+
then
60+
echo -e "\e[32mOnline!\e[0m"
61+
else
62+
echo
63+
echo "Network NOT OK. You must have a working Network connection to run this script."
64+
echo "Please report this issue here: https://github.com/techandme/wordpress-vm/issues/new"
65+
exit 1
66+
fi
4267

43-
# Locate the best mirrors
68+
# Get the best mirrors for Ubuntu based on location
4469
echo "Locating the best mirrors..."
45-
apt update -q2
46-
apt install python-pip -y
47-
pip install \
48-
--upgrade pip \
49-
apt-select
5070
apt-select
5171
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup && \
72+
if [ -f sources.list ]
73+
then
5274
sudo mv sources.list /etc/apt/
53-
clear
75+
fi
5476

5577
ADDRESS=$(hostname -I | cut -d ' ' -f 1)
5678

@@ -168,6 +190,117 @@ EOMSTART
168190
echo -e "\e[32m"
169191
read -p "Press any key to start the script..." -n1 -s
170192
echo -e "\e[0m"
193+
clear
194+
195+
# Set hostname and ServerName
196+
echo "Setting hostname..."
197+
FQN=$(host -TtA $(hostname -s)|grep "has address"|awk '{print $1}') ; \
198+
if [[ "$FQN" == "" ]]
199+
then
200+
FQN=$(hostname -s)
201+
fi
202+
sudo sh -c "echo 'ServerName $FQN' >> /etc/apache2/apache2.conf"
203+
sudo hostnamectl set-hostname $FQN
204+
service apache2 restart
205+
cat << ETCHOSTS > "/etc/hosts"
206+
127.0.1.1 $FQN.localdomain $FQN
207+
127.0.0.1 localhost
208+
209+
# The following lines are desirable for IPv6 capable hosts
210+
::1 localhost ip6-localhost ip6-loopback
211+
ff02::1 ip6-allnodes
212+
ff02::2 ip6-allrouters
213+
ETCHOSTS
214+
215+
# VPS?
216+
function ask_yes_or_no() {
217+
read -p "$1 ([y]es or [N]o): "
218+
case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
219+
y|yes) echo "yes" ;;
220+
*) echo "no" ;;
221+
esac
222+
}
223+
224+
if [[ "no" == $(ask_yes_or_no "Do you run this script on a *remote* VPS like DigitalOcean, HostGator or similar?") ]]
225+
then
226+
# Change IP
227+
echo -e "\e[0m"
228+
echo "OK, we assume you run this locally and we will now configure your IP to be static."
229+
echo -e "\e[1m"
230+
echo "Your internal IP is: $ADDRESS"
231+
echo -e "\e[0m"
232+
echo -e "Write this down, you will need it to set static IP"
233+
echo -e "in your router later. It's included in this guide:"
234+
echo -e "https://www.techandme.se/open-port-80-443/ (step 1 - 5)"
235+
echo -e "\e[32m"
236+
read -p "Press any key to set static IP..." -n1 -s
237+
echo -e "\e[0m"
238+
ifdown $IFACE
239+
sleep 1
240+
ifup $IFACE
241+
sleep 1
242+
bash $SCRIPTS/ip.sh
243+
if [ "$IFACE" = "" ]
244+
then
245+
echo "IFACE is an emtpy value. Trying to set IFACE with another method..."
246+
wget -q $STATIC/ip2.sh -P $SCRIPTS
247+
bash $SCRIPTS/ip2.sh
248+
rm $SCRIPTS/ip2.sh
249+
fi
250+
ifdown $IFACE
251+
sleep 1
252+
ifup $IFACE
253+
sleep 1
254+
echo
255+
echo "Testing if network is OK..."
256+
sleep 1
257+
echo
258+
CONTEST=$(bash $SCRIPTS/test_connection.sh)
259+
if [ "$CONTEST" == "Connected!" ]
260+
then
261+
# Connected!
262+
echo -e "\e[32mConnected!\e[0m"
263+
echo
264+
echo -e "We will use the DHCP IP: \e[32m$ADDRESS\e[0m. If you want to change it later then just edit the interfaces file:"
265+
echo "sudo nano /etc/network/interfaces"
266+
echo
267+
echo "If you experience any bugs, please report it here:"
268+
echo "https://github.com/techandme/wordpress-vm/issues/new"
269+
echo -e "\e[32m"
270+
read -p "Press any key to continue..." -n1 -s
271+
echo -e "\e[0m"
272+
else
273+
# Not connected!
274+
echo -e "\e[31mNot Connected\e[0m\nYou should change your settings manually in the next step."
275+
echo -e "\e[32m"
276+
read -p "Press any key to open /etc/network/interfaces..." -n1 -s
277+
echo -e "\e[0m"
278+
nano /etc/network/interfaces
279+
service networking restart
280+
clear
281+
echo "Testing if network is OK..."
282+
ifdown $IFACE
283+
sleep 1
284+
ifup $IFACE
285+
sleep 1
286+
bash $SCRIPTS/test_connection.sh
287+
sleep 1
288+
fi
289+
else
290+
echo "OK, then we will not set a static IP as your VPS provider already have setup the network for you..."
291+
sleep 5
292+
fi
293+
clear
294+
295+
# Set keyboard layout
296+
echo "Current keyboard layout is $(localectl status | grep "Layout" | awk '{print $3}')"
297+
echo "You must change keyboard layout to your language"
298+
echo -e "\e[32m"
299+
read -p "Press any key to change keyboard layout... " -n1 -s
300+
echo -e "\e[0m"
301+
dpkg-reconfigure keyboard-configuration
302+
echo
303+
clear
171304

172305
# Get new server keys
173306
rm -v /etc/ssh/ssh_host_*

wordpress_install.sh

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,6 @@ a2enmod rewrite \
195195
ssl \
196196
setenvif
197197

198-
# Set hostname and ServerName
199-
sudo sh -c "echo 'ServerName wordpress' >> /etc/apache2/apache2.conf"
200-
sudo hostnamectl set-hostname wordpress
201-
service apache2 restart
202-
203198
# Install PHP 7.0
204199
apt install -y \
205200
php \

0 commit comments

Comments
 (0)