Httpd port change by chef cookbook

hii Sir

I have written a simple a cookbook to install and configure a chef cookbook.
I want to change httpd default port from 80 and 443 to 81 and 8443
can you provide assitance to achive this task??

vim default.rb

package ‘httpd’ do
action :install
end

service ‘httpd’ do
action [:enable, :start]
end

:wq

Hello @sudipta1436,

I would definitely help you out with your task.

In order to change the listening ports in apache, first you need to know that the configuration changes are to be made in httpd.conf where we need to change the listening ports in httpd.conf.

Your above recipe just installs the httpd package with default configurations. To make changes in the httpd configuration we make use of attributes in chef. You can find more information on attributes here About Attributes.

First we need to make the following changes in your recipe.

# The ports that you need to change in your httpd.conf

node.default[“port”] = “81”
node.default[“secureport”] = “8443”

package ‘httpd’ do
action :install
end

# The path to your httpd.conf file

template ‘/etc/httpd/conf/httpd.conf’ do
source ‘httpd.conf.erb’
end

service ‘httpd’ do
action :restart
end

:wq

Change your recipe as above.

Create a template file

Goto your httpd cookbook and create the following file.

vim cookbooks/httpd/template/default/httpd.conf.erb

ServerRoot “/etc/httpd”

Listen <%=node[“port”]%>
Listen <%=node[“secureport”]%>
Include conf.modules.d/*.conf

User apache
Group apache

ServerAdmin root@localhost

AllowOverride none Require all denied

DocumentRoot “/var/www/html”
<Directory “/var/www”>
AllowOverride None
# Allow open access:
Require all granted

<Directory “/var/www/html”>
Options Indexes FollowSymLinks
Require all granted

DirectoryIndex index.html

<Files “.ht*”>
Require all denied

ErrorLog “logs/error_log”

LogLevel warn

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined LogFormat "%h %l %u %t \"%r\" %>s %b" common
<IfModule logio_module>
  LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>
CustomLog "logs/access_log" combined
AllowOverride None Options None Require all granted TypesConfig /etc/mime.types AddType application/x-compress .Z AddType application/x-gzip .gz .tgz AddType text/html .shtml AddOutputFilter INCLUDES .shtml

AddDefaultCharset UTF-8

IncludeOptional conf.d/*.conf

:wq

The above is basically the default httpd.conf with the attribute changes to the ports. Basically you can take the default configuration file from /etc/httpd/httpd.conf and make the changes as in line 2 and line 3 as in the above code snippet.

Finally bootstrap and apply your recipe and apache should be listening to port 81 and port 8443 respectively.