Created
December 31, 2015 00:48
-
-
Save tomcarlson/1ec63a5c3ec1105ba4fb to your computer and use it in GitHub Desktop.
Setup a new server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| STARTERDOMAIN="example.com" # name of 1st domain on this server | |
| SERVERNAME="example" # we'll set hostname to this | |
| ADMIN_NAME="admin" # will be set up as an admin user | |
| ADMIN_PASS="admin password" | |
| APACHE_VHOST="$STARTERDOMAIN" # we'll also use this for our DOMAINNAME for email, hostname -f, etc | |
| SSLPATH="/etc/apache2/ssl" | |
| ADMIN_EMAIL=${ADMIN_NAME}@${APACHE_VHOST} | |
| termtitle () { echo -e "\033]0;$1\007" ; } #change terminal title to show where we are in script | |
| # Set the hostname | |
| hostname $SERVERNAME | |
| echo $SERVERNAME > /etc/hostname | |
| # Set the Time Zone | |
| # creating this symlink sets the timezone. Pretty Cool! (was dpkg-reconfigure tzdata ) | |
| # http://serverfault.com/questions/94991/setting-the-timezone-with-an-automated-script | |
| TIMEZONE="America/Los_Angeles" # remember for php.ini, and use here too | |
| echo $TIMEZONE > /etc/timezone # I don't know if I need this, but let's fill it in | |
| rm /etc/localtime # This does the actual time setting | |
| #ln -s /usr/share/zoneinfo/${TIMEZONE} /etc/localtime #hmmm, maybe shouldn't symlink | |
| cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime #see serverfault discussion at link above | |
| #----------------- Set up Static Message of the Day ----------------------------# | |
| rm /etc/motd | |
| cat >> /etc/motd.static <<EOF | |
| `TERM=linux setterm -default -foreground blue -bold on` | |
| Welcome to the home of $STARTERDOMAIN | |
| `TERM=linux setterm -default -foreground green` | |
| edit this file at /etc/motd.static | |
| `TERM=linux setterm -default` | |
| EOF | |
| ln -s /etc/motd.static /etc/motd # doing it this way, or reboot will wipe out the message | |
| #----------------- INSTALL S3CFG ----------------------------# | |
| # Import S3tools signing key: | |
| wget -O- -q http://s3tools.org/repo/deb-all/stable/s3tools.key | sudo apt-key add - | |
| # Add the repo to sources.list: | |
| sudo wget -O/etc/apt/sources.list.d/s3tools.list http://s3tools.org/repo/deb-all/stable/s3tools.list | |
| # Refresh package cache and install the newest s3cmd: | |
| sudo apt-get update && sudo apt-get install s3cmd | |
| #----------------- INSTALL DNSUTILS ----------------------------# | |
| termtitle "Install DNSUTILS" | |
| aptitude install -y dnsutils | |
| #----------------- INSTALL SUDO --# | |
| aptitude install -y sudo | |
| #----------------- INSTALL LSB --# | |
| aptitude install -y lsb | |
| #----------------- UNISON AND RSYNC ------------------------# | |
| termtitle "aptitude install -y rsync unison" | |
| aptitude install -y rsync unison | |
| #----------------- DOS2UNIX and ZIP ------------------------# | |
| termtitle "aptitude install -y tofrodos zip" | |
| aptitude install -y tofrodos zip | |
| #----------------- SUBVERSION and GIT ----------------------# | |
| termtitle "aptitude -y install subversion git-core" | |
| aptitude -y install subversion git-core | |
| #----------------- CURL ------------------------------------# | |
| termtitle "aptitude -y install curl" | |
| aptitude -y install curl | |
| #----------------- ENABLE APACHE MODS ------------------------------------# | |
| a2enmod suexec | |
| #a2enmod fastcgi | |
| a2enmod actions | |
| a2enmod headers | |
| a2enmod include | |
| #----------------- SETUP /ETC/HOSTS and ETC/MAILNAME ----------------# | |
| termtitle "SETUP /ETC/HOSTS and ETC/MAILNAME" | |
| SLICENAME=`hostname` # remember the slicename | |
| HOSTPREFIX=$SLICENAME # we'll use the slicename as our prefix for our new hostname | |
| DOMAINNAME=$APACHE_VHOST # for this initial slice setup, we can just use the vhost domain name as our host's domain name | |
| # explode string into an array based on periods | |
| string=$DOMAINNAME | |
| a=($(echo $string | nawk -F"." '{$1=$1; print}')) | |
| arraylength=${#a[@]} #get length of array | |
| if [ $arraylength == 1 ]; then | |
| HOSTNAME="${HOSTPREFIX}.${DOMAINNAME}.com" # assume .com suffix | |
| else | |
| HOSTNAME="${HOSTPREFIX}.${a[arraylength-2]}.${a[arraylength-1]}" | |
| fi | |
| # at this point, $HOSTNAME contains is slicename.domainname.com. | |
| # This is the hostname I use in /etc/hosts and /etc/mailname | |
| #------------- fix up /etc/hosts ----------------- | |
| # if you modify /etc/hostname to look like this .. | |
| # traffic | |
| # then you must modify /etc/hosts must look like this ... including the 127.0.0.1 line, for sudo to work | |
| # 96.126.120.160 traffic.trafficsize.com traffic | |
| # 127.0.0.1 traffic.trafficsize.com traffic | |
| #IPADDRESS=`./ipaddress.php` | |
| IPADDRESS="$(ifconfig | egrep 'inet ' | sed -e 's/inet //' -e 's/addr://' -e 's/ Bcast.*//' -e 's/127.*//')" | |
| # Does ipaddress exist? | |
| if grep -q ${IPADDRESS} /etc/hosts | |
| then | |
| ./linereplace.php "${IPADDRESS}" "${IPADDRESS} ${HOSTNAME} ${SLICENAME}" /etc/hosts | |
| else | |
| # add line to beginning of file | |
| echo "${IPADDRESS} ${HOSTNAME} ${SLICENAME}"|cat - /etc/hosts > /tmp/out && mv /tmp/out /etc/hosts | |
| fi | |
| # Does 127.0.0.1 exist? | |
| if grep -q 127.0.0.1 /etc/hosts | |
| then | |
| ./linereplace.php "127.0.0.1" "127.0.0.1 ${HOSTNAME} ${SLICENAME" /etc/hosts | |
| else | |
| # add line to beginning of file | |
| echo "127.0.0.1 ${HOSTNAME} ${SLICENAME"|cat - /etc/hosts > /tmp/out && mv /tmp/out /etc/hosts | |
| fi | |
| # ---------- Linode hostname problems ----------------# | |
| # Linode explains pretty well how to change the hostname of your VPS. | |
| # But they do not mention that in the latest Ubuntu it is set via Linode`s own DHCP server. | |
| # So even if you set it via /etc/hostname and in hosts, it will still be overwritten by Linode`s own | |
| # hostname given to your server. | |
| # The solution is to kindly tell DHCPCD to *not* override the hostname you have set, open /etc/default/dhcpcd and alter the following: | |
| # SET_HOSTNAME='yes' | |
| # to | |
| # SET_HOSTNAME='no' | |
| sed -i "s/SET_HOSTNAME\='yes'/SET_HOSTNAME\='no'/g" /etc/default/dhcpcd | |
| #------------- create /etc/mailname ------------------# | |
| echo ${HOSTNAME} > /etc/mailname | |
| #----------------- MODIFY /ETC/SKEL SO NEW USERS GET SAME DIRECTORY STRUCTURE------# | |
| termtitle "SETUP /etc/skel directory for new users" | |
| mkdir /etc/skel/scripts | |
| mkdir /etc/skel/backup | |
| mkdir /etc/skel/log | |
| mkdir /etc/skel/htdocs | |
| mkdir /etc/skel/Maildir | |
| #------------------ .PROCMAILRC SKELETON -------------------- # | |
| cat > /etc/skel/.procmailrc <<ENDOFFILE | |
| # HELP!! Go Here--> http://partmaps.org/era/procmail/quickref.html | |
| # aliases file is in /etc/aliases , have root edit this for you if needed | |
| PATH=/bin:/usr/bin:/usr/local/bin | |
| MAILDIR=\$HOME/Maildir | |
| LOGFILE=\$MAILDIR/procmail.\`date +%m-%d-%y\` | |
| #LOGFILE=/dev/null | |
| # Add your rules here | |
| #If email is to this user, than forward | |
| :0 | |
| * ^TO_username* | |
| ! you@gmail983.com | |
| :0 | |
| * ^TO_user@example.com | |
| ! you@gmail983.com | |
| :0 | |
| * ^TO_support@example.com | |
| ! you@gmail983.com | |
| :0 | |
| * ^From.*xyz-list@xyz-domain.com | |
| .xyz-list/ | |
| # Perhaps process some emails with a PHP script ... | |
| :0 | |
| * ^From.*mailinglist | |
| | php5 \$HOME/htdocs/example.com/mail_in/mailprocess.php | |
| # # Catch-all rule for all unmatched email | |
| # :0 | |
| # ! you_spam@gmail983.com | |
| ENDOFFILE | |
| #------------------ .BASHRC SKELETON -------------------- # | |
| cat > /etc/skel/.bashrc <<ENDOFFILE | |
| # ~/.bashrc: executed by bash(1) for non-login shells. | |
| umask 022 # Set default creation file/directory permissions | |
| # Set xterm Title | |
| PROMPT_COMMAND='echo -ne "\033]0;\${USER}@\${HOSTNAME}: \${PWD}\007"' | |
| #Set Prompt | |
| PS1="\[\033[1;35m\]\\\$(/bin/pwd)\[\033[m\]\n\[\033[36m\]\u\[\033[m\]@\[\033[32m\]\h:\[\033[33;1m\]\$ " | |
| alias cd..='cd ..' | |
| alias type='cat' | |
| alias edit='pico' | |
| export EDITOR="pico" # needed by git, or it will bring up an editor you don't know | |
| alias del="rm" | |
| alias copy="cp" | |
| alias kill="kill -9" | |
| alias dir='ls -a -l -k --color=auto' | |
| alias ls='ls -a --color=auto' | |
| alias home='cd \$HOME' | |
| locate () { find / -name \$1; } | |
| termtitle () { PROMPT_COMMAND="echo -ne \"\033]0;\$1\007\""; } | |
| alias remotebackup='bash ~/scripts/remotebackup.sh' | |
| alias remoterestore='bash ~/scripts/remoterestore.sh' | |
| alias localbackup='bash ~/scripts/localbackup.sh' | |
| alias localrestore='bash ~/scripts/localrestore.sh' | |
| alias linereplace='php ~/scripts/linereplace.php' | |
| alias ipaddress='php ~/scripts/ipaddress.php' | |
| # make sure local/bin, /usr/local/bin, and /sbin are in our path | |
| export PATH="\$HOME/local/bin:/usr/local/bin:/sbin:\$PATH" | |
| ENDOFFILE | |
| #------------------ .MAILRC SKELETON TO USE MAILDIR FORMAT -------------------- # | |
| # this was the result of 2 days of googling and wasted time | |
| cat > /etc/skel/.mailrc <<ENDOFFILE | |
| set folder="maildir://Maildir/" | |
| set mailbox-type="maildir" | |
| set record="maildir://~/Maildir/sent-mail/" | |
| ENDOFFILE | |
| #----------------- ADD USER ---------------------------------# | |
| termtitle "Add New User $ADMIN_NAME" | |
| useradd -m -s /bin/bash $ADMIN_NAME | |
| echo -e "$ADMIN_PASS\n$ADMIN_PASS\n" | passwd $ADMIN_NAME | |
| cp /etc/sudoers /etc/newsudoers | |
| echo -e "\n$ADMIN_NAME ALL=(ALL) ALL\n" >> /etc/newsudoers | |
| mv -f /etc/newsudoers /etc/sudoers | |
| chmod 440 /etc/sudoers | |
| #----------------- GROUP MANAGMENT ---------------------------------# | |
| # We'll create a group for every user. Then put that user and wwww-data into | |
| # that group. That way, www-data (the webserver) will have access. | |
| # this won't be the users, or www-data's primary group. | |
| groupadd ${ADMIN_NAME}_GRP | |
| usermod -a -G ${ADMIN_NAME}_GRP $ADMIN_NAME # add user to his special group | |
| usermod -a -G ${ADMIN_NAME}_GRP www-data # add www-data to users special group | |
| #----------------- GIVE WEB FILES AND DIRECTORIES TO USER--# | |
| termtitle "Give user $ADMIN_NAME all of the files we've created for them" | |
| # Files at home level belong only to user | |
| chown -R $ADMIN_NAME:$ADMIN_NAME /home/$ADMIN_NAME | |
| # Files in web-accessable directories belong to user's special group | |
| # that www-data belongs to | |
| chown -R $ADMIN_NAME:${ADMIN_NAME}_GRP /home/$ADMIN_NAME/htdocs | |
| # chmod g+w make all directories group-writeable, so web-scripts can save | |
| # chmod g+s make all directories pass their group to their children when new children are created | |
| # This will recursively search your directory tree & chmod all directories only. | |
| find /home/$ADMIN_NAME/htdocs -type d -exec chmod g+w+s {} \; | |
| #actually, we need all files to be group writeable, for log files, etc. | |
| chmod -R g+w /home/$ADMIN_NAME/htdocs | |
| #----------------- SETUP SCRIPT TO RUN AT BOOT TIME TO MAIL ADMIN ----------------# | |
| cat > /etc/init.d/mail_admin.sh <<ENDOFFILE | |
| #! /bin/sh | |
| # /etc/init.d/mail_admin.sh | |
| # Send Email notification, so admin knows something caused a reboot | |
| echo "FYI" | mail -s "\`hostname\` - Restarted" "$ADMIN_NAME" | |
| ENDOFFILE | |
| chmod +x /etc/init.d/mail_admin.sh | |
| update-rc.d mail_admin.sh defaults |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment