Logo

دليل تثبيت وإعداد Squid Proxy على الخادم باستخدام سكريبت Bash

دليل تثبيت وإعداد Squid Proxy على الخادم باستخدام سكريبت Bash

 

This guide explains how to use a Bash script to install and configure a Squid Proxy server on a machine running Ubuntu, CentOS, AlmaLinux, or Rocky Linux with authentication (username and password) support.

 

https://github.com/Ematrex-Scripts/E-MATREX/blob/main/setup_proxy.sh


1. Requirements

The script must be run as root, or it will not work properly.

To switch to the root user, run:

sudo su

Then execute the script as described below.


2. How to Use the Script

The script requires three arguments when executed:

  • Port (the proxy server will run on this port).
  • Username (for authentication).
  • Password (for authentication).

Run the script as follows:

bash setup_proxy.sh <PORT> <USERNAME> <PASSWORD>

Example:

bash setup_proxy.sh 3128 myuser mypassword
  • 3128 → The proxy server will listen on this port.
  • myuser → Username for authentication.
  • mypassword → Password for authentication.

3. What the Script Does

  1. Ensures it is run as root:

    • If the user is not root, the script exits with an error message.
  2. Detects the Operating System and Version:

    • The script reads /etc/os-release to determine if the system is Ubuntu, CentOS, AlmaLinux, or Rocky Linux.
  3. Installs Squid and Required Packages:

    • On Ubuntu, it uses apt-get to install Squid and apache2-utils.
    • On CentOS, AlmaLinux, Rocky, it uses yum or dnf to install Squid and httpd-tools.
  4. Sets Up Authentication (Username & Password):

    • The script locates the basic_ncsa_auth file required for authentication.
    • It creates a password file (squid_passwd) and stores the username and password.
  5. Configures Squid Proxy:

    • It removes old configurations and creates a new configuration that allows only authenticated users.
    • Sets Squid to run on the specified port (http_port 0.0.0.0:$PORT_PROXY).
  6. Validates Squid Configuration:

    • Runs squid -k parse to check for errors.
  7. Restarts Squid and Enables It on Boot:

    • Uses systemctl restart squid and systemctl enable squid to start Squid and ensure it runs automatically on boot.

4. Checking If Squid Is Running

After running the script, verify if Squid is active with:

systemctl status squid

If it's running, you should see:

Active: active (running)

5. Using the Proxy

Once Squid is installed and running, you can configure your browser, system, or tools to use it.

Proxy Configuration Details:

  • Server Address (IP): Your server's IP.
  • Port: The one you specified during script execution.
  • Username & Password: As defined during script execution.

Example: Using the Proxy with curl

curl -x http://myuser:mypassword@your-server-ip:3128 http://google.com

(Replace your-server-ip with your actual server's IP.)


6. Troubleshooting Common Issues

🔴 Squid is not running after executing the script?

  • Check the logs for errors:
    journalctl -xe | grep squid
    
  • Ensure the chosen port is open in the firewall:
    firewall-cmd --add-port=3128/tcp --permanent
    firewall-cmd --reload
    
    (Replace 3128 with the port you selected.)

🔴 Authentication Issues?

  • Verify the squid_passwd file contains your credentials:
    cat /etc/squid/squid_passwd
    

7. Conclusion

 

This script provides a quick and easy way to set up a Squid Proxy with username and password authentication on Ubuntu, CentOS, AlmaLinux, or Rocky Linux. If you need to customize the configuration, you can modify Squid’s config file at /etc/squid/squid.conf. 🚀

Need help? Call us:

+212634179489