How to Configure Apache Reverse Proxy on CentOS

Description: Here I have explained what is Apache Reverse Proxy and How to configure it.

What is Reverse Proxy: 
A reverse proxy accepts connections and then routes them to an appropriate path.

For example, if we have any application running on port 5000, we can configure a reverse proxy to accept connections on HTTP or HTTPS, which can then transparently proxy requests to the application backend.

Configure Reverse Proxy.

  1. Verify Proxy module using below command
# httpd -M
Output


proxy_module (shared)


lbmethod_byrequests_module (shared)


proxy_balancer_module (shared)


proxy_http_module (shared

      2. Configure Virtual Host as follow: [We’ll use example application running on 127.0.0.1:5000 as the backend service that we want to reverse proxy requests to]

 




        # The ServerName directive sets the request scheme, hostname and port that


        # the server uses to identify itself. This is used when creating


        # redirection URLs. In the context of virtual hosts, the ServerName


        # specifies what hostname must appear in the request's Host: header to


        # match this virtual host. For the default virtual host (this file) this


        # value is not decisive as it is used as a last resort host regardless.


        # However, you must set it for any further virtual host explicitly.


        #ServerName www.example.com


        ProxyPreserveHost On


        ProxyPass /api http://127.0.0.1:5000


        ProxyPassReverse /api http://127.0.0.1:5000


         ServerAdmin webmaster@localhost


        ServerName app1.demo.com


        ServerAlias app1.demo.com


        DocumentRoot /var/www/html/demoapp


SSLEngine on


SSLCertificateFile /var/www/html/certs/demo.crt


SSLCertificateKeyFile /var/www/html/certs/demo.key


SSLCertificateChainFile /var/www/html/certs/demo_CA.crt




        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,


        # error, crit, alert, emerg.


        # It is also possible to configure the loglevel for particular


        # modules, e.g.


        #LogLevel info ssl:warn




        ErrorLog ${APACHE_LOG_DIR}/error.log


        CustomLog ${APACHE_LOG_DIR}/access.log combined




        # For most configuration files from conf-available/, which are


        # enabled or disabled at a global level, it is possible to


        # include a line for only one particular virtual host. For example the


        # following line enables the CGI configuration for this host only


        # after it has been globally disabled with "a2disconf".


        #Include conf-available/serve-cgi-bin.conf




  • Restart httpd service and verify. 
# systemctl start httpd

Run Script in Screen Mode on Startup

Description: Here I have explained, How to run script on Startup

Procedure: 

  • Create one script as below which will run in screen mode

 #!/bin/sh
screen -d -m -S SessionName /home/script/run.sh

  • To run script on startup open crontab and define schedule as follow.
 @reboot /bin/sh /home/script/run.sh 

  • After define in schedule restart crontab service and test by restart server.

Install and Configure FirewallD on Centos 7

Description: FirewallD is a firewall management tool available on CentOS 7 servers. Basically, it is a wrapper around iptables and it comes with graphical configuration tool firewall-config and command line tool firewall-cmd. With the iptables service, every change requires flushing of the old rules and reading the new rules from the ‘/etc/sysconfig/iptables’ file, while with firewalld only differences are applied.

Procedure: 

  • Install FirewallD using yum 
# yum install firewalld 

FirewallD Zones: FirewallD uses services and zones instead of iptables rules and chains. By default the following zones are available

  1. Drop: Drop all incoming network packets with no reply, only outgoing network connections are available.
  2. Block: Reject all incoming network packets with an icmp-host-prohibited message, only outgoing network connections are available.
  3. Public: Only selected incoming connections are accepted, for use in public areas
  4. External: For external networks with masquerading enabled, only selected incoming connections are accepted.
  5. DMZ: DMZ demilitarized zone, publicly-accessible with limited access to the internal network, only selected incoming connections are accepted.
  6. work: For computers in your home area, only selected incoming connections are accepted.
  7. home: For computers in your home area, only selected incoming connections are accepted.
  8. internal: For computers in your internal network, only selected incoming connections are accepted.
  9. trusted: All network connections are accepted.
  • To list all available zones 
# firewall-cmd --get-zones

work drop internal external trusted home dmz public block

  • To list default zone
#firewall-cmd --get-default-zone
public
  • To change the default zone:
# firewall-cmd --set-default-zone
# firewall-cmd --get-default-zone
dmz
  • Add and allow service in DMZ zone 
# firewall-cmd --zone=dmz --add-service=http --permanent
# firewall-cmd --zone=dmz --add-service=https --permanent
# firewall-cmd --zone=dmz --add-service=imap --permanent
# firewall-cmd --zone=dmz --add-service=imaps --permanent
# firewall-cmd --zone=dmz --add-service=pop3 --permanent
# firewall-cmd --zone=dmz --add-service=pop3s --permanent
  • Remove service and add custom port 
# firewall-cmd --remove-service=ssh --permanent
# firewall-cmd --add-port=7022/tcp --permanent
  • Reload Firewall configuration 
# firewall-cmd --reload
  • List Firewall Rules 
# firewall-cmd --list-all
dmz
target: default
icmp-block-inversion:
interfaces
sources
services: http https imap imaps pop3 pop3s smtp smtps
ports: 7022/tcp
protocols
masquerade: no
forward-ports
sourceports
icmp-blocks
rich rules

Install and Configure FirewallD on Centos 7

Description: FirewallD is a firewall management tool available on CentOS 7 servers. Basically, it is a wrapper around iptables and it comes with graphical configuration tool firewall-config and command line tool firewall-cmd. With the iptables service, every change requires flushing of the old rules and reading the new rules from the ‘/etc/sysconfig/iptables’ file, while with firewalld only differences are applied.

Procedure: 

  • Install FirewallD using yum 
# yum install firewalld 

FirewallD Zones: FirewallD uses services and zones instead of iptables rules and chains. By default the following zones are available

  1. Drop: Drop all incoming network packets with no reply, only outgoing network connections are available.
  2. Block: Reject all incoming network packets with an icmp-host-prohibited message, only outgoing network connections are available.
  3. Public: Only selected incoming connections are accepted, for use in public areas
  4. External: For external networks with masquerading enabled, only selected incoming connections are accepted.
  5. DMZ: DMZ demilitarized zone, publicly-accessible with limited access to the internal network, only selected incoming connections are accepted.
  6. work: For computers in your home area, only selected incoming connections are accepted.
  7. home: For computers in your home area, only selected incoming connections are accepted.
  8. internal: For computers in your internal network, only selected incoming connections are accepted.
  9. trusted: All network connections are accepted.
  • To list all available zones 
# firewall-cmd --get-zones

work drop internal external trusted home dmz public block

  • To list default zone
#firewall-cmd --get-default-zone
public
  • To change the default zone:
# firewall-cmd --set-default-zone
# firewall-cmd --get-default-zone
dmz
  • Add and allow service in DMZ zone 
# firewall-cmd --zone=dmz --add-service=http --permanent
# firewall-cmd --zone=dmz --add-service=https --permanent
# firewall-cmd --zone=dmz --add-service=imap --permanent
# firewall-cmd --zone=dmz --add-service=imaps --permanent
# firewall-cmd --zone=dmz --add-service=pop3 --permanent
# firewall-cmd --zone=dmz --add-service=pop3s --permanent
  • Remove service and add custom port 
# firewall-cmd --remove-service=ssh --permanent
# firewall-cmd --add-port=7022/tcp --permanent
  • Reload Firewall configuration 
# firewall-cmd --reload
  • List Firewall Rules 
# firewall-cmd --list-all
dmz
target: default
icmp-block-inversion:
interfaces
sources
services: http https imap imaps pop3 pop3s smtp smtps
ports: 7022/tcp
protocols
masquerade: no
forward-ports
sourceports
icmp-blocks
rich rules

Install Samba on Centos 7 for file sharing on Windows

Description: In this article I have explain how we can sharing files between windows and Centos

Procedure: 

  • Install Samba in Centos 7 using below command
# yum install samba samba-client samba-common
  • After installation need to add in firewall 
# firewall-cmd --permanent --zone=public --add-service=samba
# firewall-cmd --reload
  • Check windows system work group settings. Before configure samba need to check work group in windows PC.  We can check from My Computer — Properties — Advance System settings — Computer Name
  • After checking work group configure Samba on centos 7. The configuration file of samba is /etc/samba/smb.conf  with pre-configuration settings. But make sure to take backup before made any changes. 
  • First create any directory that you want to share like “Testdirectory”  and set appropriate permission on it.
# mkdir -p /srv/samba/Testdirectory
# chmod -R 0775 /srv/samba/Testdirectory
# chown -R nobody:nobody /srv/samba/Testdirectory
  • You need to set SELinux security context for shared directory
# chcon -t samba_share_t /srv/samba/Testdirectory
  • After creating directory open smb configuration directory and add as following 
# useradd authuser
# vi /etc/samba/smb.conf
[global]
workgroup = WORKGROUP
netbios name = centos
security = user

[Testdirectory]
comment = Test Directory
path = /srv/samba/Testdirectory
browsable =yes
writable = yes
guest ok = yes
read only = no
force user = nobody
  • Now test configuration using below command 
# useradd authuser
# testparm

Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Processing section "[Anonymous]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
# Global parameters

[global]
netbios name = centos
printcap name = cups
security = USER
idmap config * : backend = tdb
cups options = raw

[homes]
comment = Home Directories
browseable = No
inherit acls = Yes
read only = No
valid users = %S %D%w%S

[printers]
comment = All
Printers
path = /var/tmp
browseable = No
printable = Yes
create mask = 0600
[print$]
comment = Printer
Drivers
path = /var/lib/samba/drivers
create
mask = 0664
directory mask = 0775
write list = root
[Testdirectory]
comment = Anonymous File Server Share
path = /srv/samba/Testdirectory
force user = nobody
guest ok = Yes
read only = No
  • Now start samba service and enable it during boot process
# systemctl enable smb.service
# systemctl enable nmb.service
# systemctl start smb.service
# systemctl start nmb.service
  • Now test from your windows client by using \\ipaddress_of_server 

Setup Secure Samba in Centos 7 

  • First create samba group, then add user and set password for them.
# groupadd smbgrp
# usermod systalk -aG smbgrp
# smbpasswd -a systalk
  • Then create a secure directory where the shared files will be kept and set the appropriate permissions on the directory with SELinux security context for the samba.
# mkdir -p /srv/samba/secure
# chmod -R 0770 /srv/samba/secure
# chown -R root:smbgrp /srv/samba/secure
# chcon -t samba_share_t /srv/samba/secure
  • Open samba configuration file and add samba group for appropriate sharing 
# vi /etc/samba/smb.conf
[Secure]
comment = Secure File Server Share
path =  /srv/samba/secure
valid users = @smbgrp
guest ok = no
writable = yes
browsable = yes
  • After configuration run testparm to test configuration and restart service 
# systemctl restart smb.service
# systemctl restart nmb.service
  • Now test secure samba file sharing it will prompt for password. 

Install Samba on Centos 7 for file sharing on Windows

Description: In this article I have explain how we can sharing files between windows and Centos

Procedure: 

  • Install Samba in Centos 7 using below command
# yum install samba samba-client samba-common
  • After installation need to add in firewall 
# firewall-cmd --permanent --zone=public --add-service=samba
# firewall-cmd --reload
  • Check windows system work group settings. Before configure samba need to check work group in windows PC.  We can check from My Computer — Properties — Advance System settings — Computer Name
  • After checking work group configure Samba on centos 7. The configuration file of samba is /etc/samba/smb.conf  with pre-configuration settings. But make sure to take backup before made any changes. 
  • First create any directory that you want to share like “Testdirectory”  and set appropriate permission on it.
# mkdir -p /srv/samba/Testdirectory
# chmod -R 0775 /srv/samba/Testdirectory
# chown -R nobody:nobody /srv/samba/Testdirectory
  • You need to set SELinux security context for shared directory
# chcon -t samba_share_t /srv/samba/Testdirectory
  • After creating directory open smb configuration directory and add as following 
# useradd authuser
# vi /etc/samba/smb.conf
[global]
workgroup = WORKGROUP
netbios name = centos
security = user

[Testdirectory]
comment = Test Directory
path = /srv/samba/Testdirectory
browsable =yes
writable = yes
guest ok = yes
read only = no
force user = nobody
  • Now test configuration using below command 
# useradd authuser
# testparm

Load smb config files from /etc/samba/smb.conf
rlimit_max: increasing rlimit_max (1024) to minimum Windows limit (16384)
Processing section "[homes]"
Processing section "[printers]"
Processing section "[print$]"
Processing section "[Anonymous]"
Loaded services file OK.
Server role: ROLE_STANDALONE
Press enter to see a dump of your service definitions
# Global parameters

[global]
netbios name = centos
printcap name = cups
security = USER
idmap config * : backend = tdb
cups options = raw

[homes]
comment = Home Directories
browseable = No
inherit acls = Yes
read only = No
valid users = %S %D%w%S

[printers]
comment = All
Printers
path = /var/tmp
browseable = No
printable = Yes
create mask = 0600
[print$]
comment = Printer
Drivers
path = /var/lib/samba/drivers
create
mask = 0664
directory mask = 0775
write list = root
[Testdirectory]
comment = Anonymous File Server Share
path = /srv/samba/Testdirectory
force user = nobody
guest ok = Yes
read only = No
  • Now start samba service and enable it during boot process
# systemctl enable smb.service
# systemctl enable nmb.service
# systemctl start smb.service
# systemctl start nmb.service
  • Now test from your windows client by using \\ipaddress_of_server 

Setup Secure Samba in Centos 7 

  • First create samba group, then add user and set password for them.
# groupadd smbgrp
# usermod systalk -aG smbgrp
# smbpasswd -a systalk
  • Then create a secure directory where the shared files will be kept and set the appropriate permissions on the directory with SELinux security context for the samba.
# mkdir -p /srv/samba/secure
# chmod -R 0770 /srv/samba/secure
# chown -R root:smbgrp /srv/samba/secure
# chcon -t samba_share_t /srv/samba/secure
  • Open samba configuration file and add samba group for appropriate sharing 
# vi /etc/samba/smb.conf
[Secure]
comment = Secure File Server Share
path =  /srv/samba/secure
valid users = @smbgrp
guest ok = no
writable = yes
browsable = yes
  • After configuration run testparm to test configuration and restart service 
# systemctl restart smb.service
# systemctl restart nmb.service
  • Now test secure samba file sharing it will prompt for password. 

Configure NFS [Network File System] in Centos 7

Description: 

Network File System (NFS) is a popular distributed file system protocol that enables users to mount remote directories on their server. NFS lets you leverage storage space in a different location and allows you to write onto the same space from multiple servers or clients in an effortless manner.
Procedure:

NFS Server side:
  • First step to install NFS using yum using below command 

# yum install nfs-utils

  • Now create directory that need to share using NFS 

# mkdir /data

  • Change permission on directory as follow

# chmod -R 755 /data

# chown nfsnobody:nfsnobody /data
  • Need to start service and enable them as boot time

# systemctl enable rpcbind
# systemctl enable nfs-server
# systemctl enable nfs-lock
# systemctl enable nfs-idmap
# systemctl start rpcbind
# systemctl start nfs-server
# systemctl start nfs-lock
# systemctl start nfs-idmap
  • Share directory over network using following

# vi /etc/exports

/var/nfsshare    * (rw,sync,no_root_squash,no_all_squash)
/home            10.10.10.10 (rw,sync,no_root_squash,no_all_squash)
Note : 10.10.10.10 is the IP of client machine, if you wish that any other client should access it you need to add the it IP wise other wise you can add “*” instead of IP for all IP access.
  • Restart NFS service 

# systemctl restart nfs-server

  • After restart service need to allow NFS port in firewall 

# firewall-cmd –permanent –zone=public –add-service=nfs
# firewall-cmd –permanent –zone=public –add-service=mountd
# firewall-cmd –permanent –zone=public –add-service=rpc-bind
# firewall-cmd –reload
NFS Client Side:
  • Install nfs-util using below command 

# yum install nfs-utils

  • Create directory to  mount from NFS

# mkdir /mnt/nfsshare

# mkdir /mnt/home
  • After creating directory now mount from NFS using below command:

# mount -t nfs 10.10.10.1:/var/nfsshare /mnt/nfsshare

# mount -t nfs 10.10.10.1:/home /mnt/home
  • After mount check storage using below command 

# df -kh

Filesystem                             Size     Used   Avail   Use%   Mounted on
/dev/mapper/centos-root       39G      1.1G    38G    3%       /
devtmpfs                                488M    0        488M   0%       /dev
tmpfs                                     494M     0        494M   0%      /dev/shm
tmpfs                                     494M    6.7M   487M   2%     /run
tmpfs                                     494M     0        494M    0%      /sys/fs/cgroup
/dev/mapper/centos-home    19G      33M    19G      1%      /home
/dev/sda1                               497M  126M   372M    26%    /boot
10.10.10.1:/var/nfsshare       49G    980M   48G     3%      /mnt/nfsshare 
10.10.10.1:/home                   19G   33M     19G      1%      /mnt/home
  • Now we are connecting NFS shared drive, please check by create test file 

# touch  /mnt/nfsshare/testfile

Permanent  NFS Mounting
  • We need to remount after every reboot, so to mount them as permanent by adding NFS share in fstab as follow:

# vi /etc/fstab

Add entries as follow 
10.10.10.1:/var/nfsshare   /mnt/nfsshare  nfs defaults 0 0
Save file using :wq 
  • Now check by reboot client machine and directory mounted or not.

Configure NFS [Network File System] in Centos 7

Description: 

Network File System (NFS) is a popular distributed file system protocol that enables users to mount remote directories on their server. NFS lets you leverage storage space in a different location and allows you to write onto the same space from multiple servers or clients in an effortless manner.
Procedure:

NFS Server side:
  • First step to install NFS using yum using below command 

# yum install nfs-utils

  • Now create directory that need to share using NFS 

# mkdir /data

  • Change permission on directory as follow

# chmod -R 755 /data

# chown nfsnobody:nfsnobody /data
  • Need to start service and enable them as boot time

# systemctl enable rpcbind
# systemctl enable nfs-server
# systemctl enable nfs-lock
# systemctl enable nfs-idmap
# systemctl start rpcbind
# systemctl start nfs-server
# systemctl start nfs-lock
# systemctl start nfs-idmap
  • Share directory over network using following

# vi /etc/exports

/var/nfsshare    * (rw,sync,no_root_squash,no_all_squash)
/home            10.10.10.10 (rw,sync,no_root_squash,no_all_squash)
Note : 10.10.10.10 is the IP of client machine, if you wish that any other client should access it you need to add the it IP wise other wise you can add “*” instead of IP for all IP access.
  • Restart NFS service 

# systemctl restart nfs-server

  • After restart service need to allow NFS port in firewall 

# firewall-cmd –permanent –zone=public –add-service=nfs
# firewall-cmd –permanent –zone=public –add-service=mountd
# firewall-cmd –permanent –zone=public –add-service=rpc-bind
# firewall-cmd –reload
NFS Client Side:
  • Install nfs-util using below command 

# yum install nfs-utils

  • Create directory to  mount from NFS

# mkdir /mnt/nfsshare

# mkdir /mnt/home
  • After creating directory now mount from NFS using below command:

# mount -t nfs 10.10.10.1:/var/nfsshare /mnt/nfsshare

# mount -t nfs 10.10.10.1:/home /mnt/home
  • After mount check storage using below command 

# df -kh

Filesystem                             Size     Used   Avail   Use%   Mounted on
/dev/mapper/centos-root       39G      1.1G    38G    3%       /
devtmpfs                                488M    0        488M   0%       /dev
tmpfs                                     494M     0        494M   0%      /dev/shm
tmpfs                                     494M    6.7M   487M   2%     /run
tmpfs                                     494M     0        494M    0%      /sys/fs/cgroup
/dev/mapper/centos-home    19G      33M    19G      1%      /home
/dev/sda1                               497M  126M   372M    26%    /boot
10.10.10.1:/var/nfsshare       49G    980M   48G     3%      /mnt/nfsshare 
10.10.10.1:/home                   19G   33M     19G      1%      /mnt/home
  • Now we are connecting NFS shared drive, please check by create test file 

# touch  /mnt/nfsshare/testfile

Permanent  NFS Mounting
  • We need to remount after every reboot, so to mount them as permanent by adding NFS share in fstab as follow:

# vi /etc/fstab

Add entries as follow 
10.10.10.1:/var/nfsshare   /mnt/nfsshare  nfs defaults 0 0
Save file using :wq 
  • Now check by reboot client machine and directory mounted or not.

How to add user and grant Root Privileges in centos 7

Description: To assign root privileges to another user on centos 7

Procedure: 

  • First add user using below command 

# adduser testuser1

  • Set password to user 

# passwd testuser1

  • Grant privileges to user using below command 

# visudo

## find the following content
root ALL=(ALL) ALL
## Add following content
testuser1 ALL=(ALL) ALL

  • Then save and exit file using :wq command
  • To test privileges login with testuser1 and use below command to take previleges

$ sudo su
password prompt for testuser1 now testuser1 can run all commands as a root.