Reputation:
I'm trying to write a script to automatically create the files and configuration for an apache server website quickly, as it usually takes me around 5 minutes and is very boring.
The part I'm struggling with is this line...
# TODO: edit ServerName and DocumentRoot to use $1 and /var/www/$1/public
Here is the full script
sudo mkdir /var/www/$1
sudo mkdir /var/www/$1/public
sudo chown -R www-data:www-data /var/www/$1
sudo chmod -R g+w /var/www/$1
sudo usermod -aG www-data $USER
echo "<?php echo phpinfo(); ?>" > /var/www/$1/public/index.php
cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/$1.io.conf
# TODO: edit ServerName and DocumentRoot to use $1 and /var/www/$1/public
cp /etc/apache2/sites-available/$1.io.conf /etc/apache2/sites-enabled/$1.io.conf
sudo -- sh -c "echo $1.io 127.0.0.1 >> /etc/hosts"
sudo service apache2 restart
xdg-open $1.io
sleep 5
code /var/www/$1
How would I replace certain parts of a .conf apache file?
Upvotes: 1
Views: 121
Reputation: 21955
Assuming that your settings have the below format :
ServerName your.server.name
DocumentRoot '/some/path'
You could do something like below (with GNU sed
) :
cp /etc/apache2/sites-available/000-default.conf "/tmp/${1}.io.conf"
sed -Ei "s|^([[:blank:]]*)#?([[:blank:]]*ServerName).*$|\1\2 ${1}|;
s|^([[:blank:]]*)#?([[:blank:]]*DocumentRoot).*$|\1\2 '/var/www/${1}/public'|;" "/tmp/${1}.io.conf"
mv "/tmp/${1}.io.conf" /etc/apache2/sites-available/
Upvotes: 1
Reputation: 2528
There are many ways to do this. The two I use most are:
patch
An example of patching an Ubuntu flavored Apache conf file
patch -d/ -p0 <<'EOF'
--- /etc/apache2/apache2.conf
+++ /etc/apache2/apache2.conf
@@ -54,6 +54,7 @@
# Global configuration
#
+ServerName your_hostname_goes_here
#
# ServerRoot: The top of the directory tree under which the server's
EOF
perl -pi -e 's%old_string%new_string%' filename
Upvotes: 0