Automated Apache and FTP Setup for New Website

When adding a new website to an existing web server, the process of setting up Apache and creating FTP users is a bit tedious. However, it’s really easy to automate with a simple script. The following is an example Shell script that automatically:

  1. Creates /var/www/<domain>/html
  2. Creates an FTP user
  3. Adds that user’s  primary group to the apache user
  4. chown & chmod /var/www/<domain>/html
  5. Creates an Apache .conf for the site, including necessary aliases and logging
  6. Restarts Apache

The script assumes Apache directory locations typical for CentOS/RHEL/Fedora, but can be easily modified for others.  It also assumes the “sites-available” and “sites-enabled” setup, tied into httpd.conf.

# usage: setup.sh &lt;domain&gt; &lt;username&gt; &lt;password&gt;

mkdir -p /var/www/$1/html
chmod -R 755 /var/www/$1/html
useradd -d /var/www/$1/html $2
usermod -a -G $2 apache
echo $3 | passwd $2 --stdin
chown -R $2:$2 /var/www/$1/html

echo "&lt;VirtualHost *:80&gt;" &gt; /etc/httpd/sites-available/$1.conf
echo "    ServerName $1" &gt;&gt; /etc/httpd/sites-available/$1.conf
echo "    ServerAlias www.$1" &gt;&gt; /etc/httpd/sites-available/$1.conf
echo "    DocumentRoot /var/www/$1/html" &gt;&gt; /etc/httpd/sites-available/$1.conf
echo "    &lt;Directory /var/www/$1/html/&gt;" &gt;&gt; /etc/httpd/sites-available/$1.conf
echo "        AllowOverride All" &gt;&gt; /etc/httpd/sites-available/$1.conf
echo "    &lt;/Directory&gt;" &gt;&gt; /etc/httpd/sites-available/$1.conf
echo "    ErrorLog /var/www/$1/error.log" &gt;&gt; /etc/httpd/sites-available/$1.conf
echo "    CustomLog /var/www/$1/requests.log combined" &gt;&gt; /etc/httpd/sites-available/$1.conf
echo "&lt;/VirtualHost&gt;" &gt;&gt; /etc/httpd/sites-available/$1.conf

ln -s /etc/httpd/sites-available/$1.conf /etc/httpd/sites-enabled/$1.conf
systemctl restart httpd

If you’re interested in cheap, enterprise-grade web hosting, without the hassle of managing the setup, feel free to contact me!

Leave a Reply

Your email address will not be published. Required fields are marked *