Squid proxy servers are an essential tool for caching and obfuscating web traffic. They allow web requests to be served from different inbound or outbound addresses, providing users with the ability to make requests from a different IP address than their own. This can be useful for various purposes, such as researching how the web is served differently across jurisdictions or avoiding surveillance and web traffic throttling.
While there are many third-party proxy service providers available, they may not always be reliable, especially if your primary goal is to route traffic away from your internet service provider. In such cases, setting up your own proxy server can be a more secure and legally bound option. Squid, an open-source HTTP proxy, is a stable and popular choice for this purpose.
In this tutorial, we will guide you through the process of installing and configuring Squid to provide an HTTP proxy on a Rocky Linux 9 server. By following these steps, you will be able to set up a private proxy server that meets your specific requirements.
Prerequisites
Before getting started, make sure you have the following:
- A Rocky Linux 9 server
- A non-root user with sudo privileges
If you haven’t set up a user with these privileges yet, you can refer to our “Initial Server Setup with Rocky Linux 9” guide for detailed instructions.
Step 1: Installing Squid Proxy
Squid offers a range of use cases beyond routing individual user’s outbound traffic. It can be used as a distributed caching mechanism, a load balancer, or as part of a routing stack in large-scale server deployments. However, for the purpose of this tutorial, we will focus on setting up Squid as an HTTP proxy for individual users.
To install Squid on Rocky Linux 9, we need to use the EPEL repository since the squid package is not available in the default package sources. The EPEL repository is a trusted source for additional packages on RHEL-based distributions.
To add the Rocky Linux EPEL repository, open a terminal and run the following command:
$ sudo dnf install epel-release
Once the EPEL repository is installed, we can proceed with the installation of Squid by running the following command:
$ sudo dnf install squid
After the installation is complete, Squid will not start automatically. We can check the status of the Squid service using the following command:
$ systemctl status squid.service
If the output shows that the service is inactive, we need to make some changes to the Squid configuration file before enabling and starting the service.
Step 2: Configuring Squid Proxy
The Squid configuration file is located at /etc/squid/squid.conf
. We can use the nano
text editor to make the necessary changes to the file. If nano
is not installed on your system, you can install it by running the following command:
$ sudo dnf install nano
Open the Squid configuration file with nano
:
$ sudo nano /etc/squid/squid.conf
In the configuration file, navigate to the line containing the phrase http_access deny all
. This line represents Squid’s default access rules. By default, Squid denies all connections except for connections originating from localhost.
To allow access from your IP address, add the following line above the http_access deny all
line:
acl localnet src your_ip_address
Replace your_ip_address
with your actual IP address. You can find your IP address by visiting a site like “What’s my IP.”
After making the necessary changes, save and close the file. If you are using nano
, press Ctrl+X
, then Y
to save the changes, and finally Enter
to exit.
To enable and start the Squid service, use the following commands:
$ sudo systemctl enable squid.service $ sudo systemctl start squid.service
Verify that Squid is running by checking its status:
$ systemctl status squid.service
If the output shows that the service is active and running, Squid has been successfully installed and configured on your Rocky Linux 9 server.
Step 3: Securing Squid Proxy
Most proxies and client-side applications that connect to proxies support various methods of authentication. Squid allows you to create username-password pairs using built-in Linux functionality, providing an additional layer of security to your proxy server.
To generate a password for a new Squid user, we need to install the httpd-tools
package, which provides the htpasswd
command:
$ sudo dnf install httpd-tools
Once the package is installed, we can create a username-password pair by running the following command:
$ sudo htpasswd -c /etc/squid/passwords your_squid_username
Replace your_squid_username
with the desired username for your Squid user. You will be prompted to enter and confirm a password for the user.
The htpasswd
command stores the username and a hash of the password in the /etc/squid/passwords
file. You can view the contents of the file using the following command:
$ sudo cat /etc/squid/passwords
After verifying that the username and password have been stored, we can update the Squid configuration to use the newly created passwords file.
Open the Squid configuration file with nano
:
$ sudo nano /etc/squid/squid.conf
Add the following lines to the configuration file:
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/passwords auth_param basic realm proxy acl authenticated proxy_auth REQUIRED http_access allow authenticated
These lines configure Squid to use the /etc/squid/passwords
file for authentication and require authentication for access to the proxy. Save and close the file.
Restart the Squid service for the changes to take effect:
$ sudo systemctl restart squid.service
If you are using firewalld, allow Squid through the firewall by adding a rule for port 3128:
$ sudo firewall-cmd --permanent --add-port=3128/tcp $ sudo firewall-cmd --reload
Step 4: Connecting through Squid Proxy
To test the Squid proxy server, we can use the curl
command-line program, which is commonly used for making web requests. curl
is typically installed by default on most modern operating systems.
To test the proxy server, open a terminal and run the following command:
$ curl -v -x http://your_squid_username:your_squid_password@your_server_ip:3128 http://www.google.com/
Replace your_squid_username
and your_squid_password
with the username and password you created earlier. Also, replace your_server_ip
with the IP address of your Rocky Linux 9 server.
If the command is successful, you should see the output containing the HTTP request and response headers.
You can also access HTTPS websites through your Squid proxy without any additional configuration. Use the following command to test an HTTPS connection:
$ curl -v -x http://your_squid_username:your_squid_password@your_server_ip:3128 https://www.google.com/
Again, replace the placeholders with your own information.
Conclusion
In this tutorial, we have shown you how to set up a Squid proxy server on Rocky Linux 9. By following these steps, you can create a private proxy server that allows you to route web traffic through a different IP address. This can be useful for various purposes, such as maintaining privacy and bypassing certain restrictions.
Setting up your own proxy server gives you full control over the security and reliability of your connections. Squid, being an open-source HTTP proxy, offers stability and flexibility, making it an excellent choice for your proxy server needs.
By configuring Squid to require authentication, you can further enhance the security of your proxy server. This ensures that only authorized users can access the proxy and protects against unauthorized usage.
If you are looking for reliable and scalable cloud hosting solutions, Shape.host offers Linux SSD VPS services that can meet your needs. With their advanced infrastructure and top-notch support, Shape.host can provide you with a secure and efficient hosting environment for your applications.