LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-05-2016, 03:06 AM   #1
horendus
LQ Newbie
 
Registered: Feb 2016
Posts: 2

Rep: Reputation: Disabled
Shell script to change Apache port number


Im running a Apache web server and I want to be able to change the http port number Apache uses from a Bash script.

The new port number is coming from a text file containing a bunch of other settings I also use.

Its formatted like this server_port="8080"

I know where it needs to be changed (in 2 places within the Apache install dir) but I am struggling to do it automatically with a bash file as im quite new to Bash scripting.

An example of how its done with a great explanation of how it works would be fantastic.

Thanks in advanced :-)
 
Old 02-05-2016, 04:32 AM   #2
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Well... it's hard to give an example without knowing how the text file looks like.

But, if it were me, I would read the text file (probably using a while loop), look for the string
Code:
server_port
I would then save the port (in this example 8080) and use sed to replace the current port in the Apache config file with the port we get from the text file.

Best regards,
HMW
 
Old 02-05-2016, 04:41 AM   #3
horendus
LQ Newbie
 
Registered: Feb 2016
Posts: 2

Original Poster
Rep: Reputation: Disabled
Smile Solution

I decided to just do some reading and came up with this which works just fine.
Im sure some of the experts will be able to turn my work into just 2 lines but I had to do it step by step to work it out.


Code:
#!/bin/bash

#Get the server port number from my settings file
serverPort=`grep 'server_port' /var/www/pyclock/app/settings.txt`
echo $serverPort

##Get just the port number from the settings variable I just grabbed
serverPort=${serverPort##*=}
echo $serverPort

##Remove the "" marks from the variable as they will not be needed
serverPort=${serverPort//'"'}
echo $serverPort

##Replace 'Virtual Hosts' and 'List' entries with the new port number
sudo  sed -i.bak 's/.*NameVirtualHost.*/NameVirtualHost *:'$serverPort'/' /etc/apache2/ports.conf 
sudo  sed -i.bak 's/.*Listen.*/Listen '$serverPort'/' /etc/apache2/ports.conf

##Restart the apache server to use new port
sudo /etc/init.d/apache2 reload

FEEL FREE TO RE FACTOR THIS
Lets see who can do this in the LEAST amount of code!
 
Old 02-05-2016, 06:13 AM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Looks pretty damn good to me. No reason to shorten it. I prefer readability over "who can write the most incomprehensible oneliner".

Good work!
 
Old 02-05-2016, 06:32 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
You could use the source command which would in a nutshell execute the commands in your settings file. So there is no need to grep. Also depends on what else is in the file.

Code:
#!/bin/bash

. /var/www/pyclock/app/settings.txt (. is a shortcut for source)
echo $serverport

serverPort=${serverPort//'"'} (You could eliminate the the "" in the settings file if you wanted to remove this command)
echo $serverport

##Replace 'Virtual Hosts' and 'List' entries with the new port number
sudo  sed -i.bak 's/.*NameVirtualHost.*/NameVirtualHost *:'$serverPort'/' /etc/apache2/ports.conf 
sudo  sed -i.bak 's/.*Listen.*/Listen '$serverPort'/' /etc/apache2/ports.conf

##Restart the apache server to use new port
sudo /etc/init.d/apache2 reload

http://www.cyberciti.biz/faq/linux-b...ource-command/
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change samba port number nikeshp Linux - Software 1 01-19-2011 12:47 PM
Change MySQL port number tristanlee85 Linux - Networking 2 07-03-2007 12:50 AM
how to change the port number of an application tijo.thomas Linux - Kernel 1 08-23-2006 03:45 AM
shell script to kill processes running on port number varunbihani Linux - Newbie 13 12-06-2005 08:46 AM
How to change Exim port number? ylikone Linux - Software 2 06-26-2005 12:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration