Build your own CDN of with DNSPod and Squid

  • 2020-05-06 12:11:32
  • OfStack

Chapter 5 preliminary preparation for installing Squid

Starting with this chapter, you will learn how to install and compile programs under Linux, learn how to optimize program compilation, and finally install Squid by means of source code compilation.

1. Basics of Linux installation
A. Classification of program installation packages
Usually, we install the program under Windows. Generally, we run the installation program directly, and then the installation program will copy the compiled binary files into the system, and finally complete the installation process.
Under Linux, the installer has more freedom than Windows. Because most of what follows Linux is open source, programs are typically provided in binary or source packages.
Binary packages are also packaged and managed differently (similar to zip and rar, but more advanced). The two most common packages are rpm (packages used by redhat, suse, etc.) and deb (debian, ubuntu). The software package installed on the machine is much, the nature needs a software to carry on the management, the update. So there are yum (rpm), apt (deb) such software package management, upgrade software.
Source code package, this package is relatively simple, generally is directly packaged into a compressed file, the suffix is generally tar.gz or tar.bz2
Installing software through package management software is relatively simple. After setting up the installation source of the software, you can install the software using the following command:
yum install php (yum)
apt-get install php5(apt)
If you don't know the exact name of the package you want to install, you can search for it using the following method:
yum search php(yum)
apt-cache search php(apt)
This article focuses on the installation of source code packages.

B. Install
through the source code package In general, the program from the source code to normal use, need to go through three steps: 1. According to the system environment to configure the source code (configure); 2. 2. Compiler (make); 3. Setup (make install).
a. The source code configures
By running the configure script, you can automatically find some of the basic environment of the program you need to compile, the commands you use, libraries, and other files, and generate the Makefile files you need to compile the files. Because there are too many distributions of Linux, each with a different environment, this script is needed. The configure script also allows you to customize your program's modules, disabling or enabling a feature.
b. Compiler
According to configure script generated Makefile file, the source code is compiled, link, generate binary files. But binaries at this time are generally not usable.
c. Installation program
With the make install command, the compiled binary files are installed to the corresponding path, and then the program can be used.

C. Where is the program installed?
Generally, compiled and installed programs are installed under /usr/local. Such as php will be installed to/usr local/bin/php, php. ini will be installed to/usr local/lib/php ini, and so on. If you need to specify an installation directory, specify the --prefix parameter at configure, and all files will be installed in the prefix directory. For example./configure --prefix=/usr/local/php. Then will to install all files in this directory, the final look will be/usr local/php bin/php and/usr local/php/lib/php ini

D. How to get the parameters of configure?
The commonly used configure parameters are --prefix, which can be obtained through./configure --help. Generally, parameters starting with --enable-, --with- enable a function, and --disable-, --without- disable a function.

E. How do I remove installed programs?
The Linux delete program is not quite the same as Windows. Windows recommends uninstalling programs using the uninstall function. Programs installed by package management software under Linux can also be removed by direct command, e.g.
yum remove php(yum)
apt-get remove php5(apt)

If the program is installed through the source code package, it will generally come with make uninstall, which allows you to delete the installed files. If you do not have make uninstall, you can simply remove the program installation directory.
Note: if the dependent libraries (such as php if you want to use to mysql function, must want to install mysql first, then the specified in the configuration when php mysql library paths, then php is dependent on the mysql), delete the dependent program, then use dependent libraries application will not be able to use (such as delete after mysql php part will not be able to normal use mysql function).

What is make clean for?
make clean is used to clean up the battlefield. Clean up all temporary files left at compile time, compiled binaries, etc. make clean is generally recommended after make install to facilitate the next recompilation and save space.

2. Source code compile-time optimization
As anyone who USES Windows knows, the biggest headache is that the program takes up too much CPU and consumes too much memory. This is because Windows's programs are "generic" and not optimized for specific platforms and specific CPU. Under Linux, compiling binaries from source code will effectively improve this problem. By adding optimization parameters, we can make the program optimize for a certain CPU model and a certain system to reduce the file size, CPU utilization, and memory utilization.
However, programs that are compiled by specifying optimization parameters will no longer have the ability to cross systems and platforms. Even if the two machines have the same system version, the program will not work as long as CPU is different. Programs can only be run on the compiled machine.

In general, the optimization parameters are set by export command CFLAGS and CXXFLAGS, and then configure will be read automatically, make will automatically use the selected optimization parameters.

For example, CPU (Intel(R) Pentium(R) 4 CPU XXXXMHz, cpu family: 15, model: 0/1/2) for normal Pentium 4 CPU (Intel(R) Pentium(R) 4 CPU XXXXMHz, cpu family: 15, model: 0/1/2) can be entered as
export CHOST="i686-pc-linux-gnu"
export CFLAGS="-march=pentium4 -O2 -pipe -fomit-frame-pointer"
export CXXFLAGS="${CFLAGS}"


CPU model and other information can be obtained by entering the following command to obtain
cat /proc/cpuinfo
This will output information like

processor             : 0
vendor_id             : AuthenticAMD
cpu family           : 15
model                     : 47
model name           : AMD Athlon(tm) 64 Processor 3200+
stepping               : 2
cpu MHz                 : 2000.336
cache size           : 512 KB
fdiv_bug               : no
hlt_bug                 : no
f00f_bug               : no
coma_bug               : no
fpu                         : yes
fpu_exception     : yes
cpuid level         : 1
wp                           : yes
flags                     : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 syscall nx mmxext fxsr_opt lm 3dnowext 3dnow up pni lahf_lm ts fid vid ttp tm stc
bogomips               : 4002.57

The above information shows that this is an AMD Athlon 64, 3200+ CPU.

CPU optimization parameters can be obtained from the following address (some cpu family and model do not have cpu family and model can be replaced by the same model)

http://gentoo-wiki.com/Safe_Cflags


3. Preliminary preparation for compiling Squid We need to first understand something called a file descriptor. We know there is a limit to what one can do at once. A file descriptor is something that limits the maximum limit. The file descriptor is used to limit the number of files a program can open at the same time. The default is 1024. That is, a program can only open 1024 files at a time without modifying the file descriptor. The number 1024 is enough for the general program, but not for Squid. Squid opens thousands of files at once for maximum efficiency and responsiveness.
Imagine two scenarios: a. Each time the user accesses squid, squid opens the file as needed, then reads the contents of the file and returns it to the user. Es306en.squid opens the frequently accessed file in advance, the user accesses squid, and squid directly returns the content to the user. The latter approach is relatively quick to respond to user requests.

To change the size of the file descriptor, you must modify two files.
/usr/include/bits/typesizes.h
/usr/include/linux/posix_types.h
Open the above file with vi (refer to the previous section if you forget how to use it) and find
#define __FD_SETSIZE 1024
Change 1024 to 65536 and save.
Why 65536, not even bigger? Because this is the maximum value that Linux can accept.
The two files just edited are the header files in the C/C++ program, which will be automatically referenced when compiling squid. In addition to these two files, we also need to set up the current environment.
The environment, which is what you set up when you log into the system with ssh. Each login process can be set individually, and environment Settings are lost when the login process is turned off without writing the Settings to the environment profile (.profile,.bash_rc).
For example, if you open two Windows with pietty, use the same account and password, log in to the same server at the same time, and then use the export command in one login process, it will only take effect in this login process, not in the other login process.

With that in mind, let's talk about the ulimit command. ulimit is used to set some resource limits for the current environment. As mentioned earlier, this command is setting the environment, so it will become invalid after exiting the current login process.

We enter the following command
ulimit -Hs 65536
ulimit -n 65536

The H parameter is a hard limit, s is the stack limit, and n is the file descriptor limit.

Finally, we download the source code for squid back using wget.
wget http://www.squid-cache.org/Versi ... 2.6.STABLE13.tar.gz

wget is the next download tool for unix to support breakpoint continuation. There will be some practical functions, such as downloading someone's entire website back (like the usual thief?). .


Related articles: