Setting up a LEMP server on Debian 6 was the first post I ever wrote. The impetus for that post (and this blog) was to chronicle all the steps I took, so that I could go back later and find my answers here, instead of googling them up all over again. After I got that server up and running, I decided to automate the process of setting up a new site. I googled around until I found a shell script that was close to what I needed, then with some more googling I managed to modify it for my needs. This script is very specific to my setup, so I doubt anyone would be able to simply download it and use it, but it should provide the groundwork for someone to craft a similar script, suited to their own server.
Here is what the script does:
- Asks for a domain, user and password.
- Creates the needed directories.
- Builds a simple index page (or downloads WordPress, but more on that later)
- Copies my nginx vhost template to the sites-available directory.
- Creates the symlink to sites-enabled.
- Does a find and replace on the vhost config to insert the correct domain name.
- Copies my php pool config template to PHP’s pool.d directory.
- Does a find and replace on the pool config to insert the correct domain name and user.
- Creates the user. This user will have no home directory and no shell access besides SFTP.
- Changes the owner of the public_html directory to the new user.
- Restarts Nginx and PHP
- Copies my SFTP template to a temp directory, does a find and replace to insert the correct file path and user, then appends the contents of that file to the sshd_config.
- Restarts ssh
There are two optional arguments you can pass the script when you execute it. The first is “wp”, which stands for WordPress, and if you pass that argument there are two changes in the execution of the script:
- Instead of creating a simple index file, WordPress is downloaded and unpacked.
- A WordPress specific vhost config template is used.
The second argument is “i”, which stands for “import”. If I’m importing an existing WordPress site, then I don’t want it to download and unpack a new WordPress installation, but I do still want it to use the WordPress specific vhost template, and that’s what this argument will accomplish.
[note color=”#DDD”]newsite.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
#! /bin/bash # # ======================= # New Nginix Site Script 0.1 # Written by PeeDub # http://selfpwnt.com # You may use, modify, and redistribute this script freely # Released: April 2012 # ======================= # Function to make a php index file: function make_pindex { cat <<- _EOF_ <html> <head><title>$dname</title></head> <body><?php echo 'welcome to'; ?> $dname</body> </html> _EOF_ } # ======================= # header # ======================= clear echo "*** Site Setup ***" # ======================= # set domain name variable # ======================= echo -n "==> Enter new domain name (domain.com): " read dname echo -n "==> Enter new user for site: " read nuser echo -n "==> Enter password for ${nuser}: " read npass echo "Setting up files for $dname" # ======================= # create needed directories # ======================= mkdir -vp /var/sites/$dname/public_html mkdir -vp /var/sites/$dname/logs # ======================= # build index.php file unless we're setting up a wordpress site # ======================= newwp=no if [ $# -gt 0 ] && [ $1 == 'wp' ] then newwp=yes fi if [ $# -eq 2 ] then if [ $2 == 'i' ] then newwp=no fi fi if [ $# -gt 0 ] && [ $1 == 'wp' ] && [ $newwp == 'yes' ] then wget http://wordpress.org/latest.tar.gz -P /var/sites/$dname/public_html tar --strip-components=1 -xzvf /var/sites/$dname/public_html/latest.tar.gz -C /var/sites/$dname/public_html rm -f /var/sites/$dname/public_html/latest.tar.gz else make_pindex > /var/sites/$dname/public_html/index.php echo "created /var/sites/$dname/public_html/index.php" fi # ======================= # build vhost config file # ======================= if [ $# -gt 0 ] && [ $1 == 'wp' ] then cp /usr/local/share/nginx-vhost-templates/example-wp.com /etc/nginx/sites-available/$dname else cp /usr/local/share/nginx-vhost-templates/example.com /etc/nginx/sites-available/$dname fi echo "created /etc/nginx/sites-available/$dname" # ======================= # enable site #======================== ln -s /etc/nginx/sites-available/$dname /etc/nginx/sites-enabled/$dname echo "enabled $dname" # Find and replace in vhost config sed -i 's/example.com/'$dname'/g' /etc/nginx/sites-available/$dname #======================== # build php pool file #======================== cp /usr/local/share/php-fpm-pool-templates/example.com.conf /etc/php5/fpm/pool.d/${dname}.conf echo "created /etc/php5/fpm/pool.d/${dname}.conf" # Find and replace in php pool config sed -i 's/example.com/'$dname'/g' /etc/php5/fpm/pool.d/${dname}.conf sed -i 's/example/'$nuser'/g' /etc/php5/fpm/pool.d/${dname}.conf # ====================== # Create User # ====================== useradd -M -s /sbin/nologin $nuser -p `mkpasswd $npass` echo "created user: $nuser" # ======================= # Set owner of public_html directory # ======================= chown -R ${nuser}:${nuser} /var/sites/$dname/public_html echo "Set ownership of web directory to $nuser" # ======================= # Restart nginx and php # ======================= /etc/init.d/nginx reload echo "Done!" /etc/init.d/php5-fpm restart echo "Done!" # ======================= # Setting up SFTP # ======================= cp /usr/local/share/sftp_user /tmp sed -i 's/example.com/'$dname'/g' /tmp/sftp_user sed -i 's/example/'$nuser'/g' /tmp/sftp_user cat /tmp/sftp_user >> /etc/ssh/sshd_config echo "SFTP configuration complete" /etc/init.d/ssh restart # ======================= # exit # ======================= echo "*** Finished setting up hosting for $dname. Goodbye!" exit |