Linux package management-source code compilation

Advanced linux articles

One, the basic description of the source package

  • It is relatively common to install source code packages under the Linux environment. In the early operation and maintenance management work, most of the software was installed through source code. Then installing a source code package requires us to compile the source code into a binary executable file.
  • The compiler of the linux system is used to compile the source code package. Usually the source code package is developed in the C language. This is also because the C language is the most standard programming language on linux. The C language compiler on Linux is called gcc, which can turn C language into an executable binary file. So if gcc is not installed on your machine, there is no way to compile the source code. You can use yum -y install gcc to complete the installation.

Second, the advantages of source code packages

  • 1. Customize and modify the source code
  • 2. Related functions required for customization
  • 3. The updated source code of the new version of the software is preferred

Third, how to obtain the source code package


Fourth, source package classification

  • Source format (need to compile and install)
  • Binary format (you can use it directly after decompression)

Five, the installation of the source code package

  • Compilation requires a compilation environment, development environment, development library, and development tools.
    Commonly used compilation environments include c, c++, perl, java and python
    . Compilers for c environment: gcc (GNU C Complier)
    Compiler for c++ environment: g++

  • make (action for compiling) compilation command: unified project management tool for c and c++, it is possible to call gcc or g++ during compilation. Use the makefile file to define the order in which make compiles the source program in the source program file

  • Source installation trilogy (common):

    • Step 1: ./configure (customized components)

      1. Specify the installation path, such as – prefix=/opt/nginx-1.12
      2. Enable or disable a function, such as --enable-ssl
      3. Associate with other software, such as –with-pcre
      4. Check the installation environment, such as Is there a compiler gcc, and whether it meets the dependency requirements of the software?
      5. After the test is passed, the Makefile file is generated

    • Step 2: make

      1. Execute the make command to compile, you can use -j to specify the number of CPU cores to compile
      2. Compile according to the Makefile file, and compile into an executable binary file
      3. Generate various modules and main programs

    • Step 3: make install

      1. Copy the path defined by the Makefile to the installation directory.
      The source trilogy introduced above cannot be used 100% for all source code packages, which means that there are no standard installation steps for source package installation, but most source code installations are Similar steps
      Suggestion: After
      getting the source code package and decompressing it, then go to the directory to find the relevant help documents, usually with INSTALL or README as the file name

5.1 Functions of configure script

  • Let users select compilation features
  • Check whether the compilation environment meets the basic needs of program compilation

5.2 Notes on Compiling and Installation

  • a. If the default path is not used during installation, you must modify the PATH environment variable to be able to identify the binary file path of this program;
    modify the /etc/profile file or create a .sh in the /etc/profile.d/ directory For files with a suffix, define export PATH=$PATH:/path/to/somewhere in it

    • Adding environment variables makes it more convenient to use the nginx source code package command service.

      • The first step: echo'export PATH=/usr/local/nginx/sbin:$PATH'> /etc/profile.d/nginx.sh Add the nginx command of the installed nginx directory to the environment variable, so that we can extract the command Time is more convenient ,
      • Step 2: source /etc/profile.d/nginx.sh //Add the environment variables written above to the environment variables of the system
      • Step 3: echo $PATH //View the current environment variables
      • Step 4: nginx run command
      • Step 5: Ss -antl Check the port that the system listens to, and whether the port started by the corresponding service is being listened to.
  • b. By default, the path for the system to search for library files is only /lib, /usr/lib

    • Add additional library file search path method:

      Create a file with the extension .conf in /etc/ld.so.conf.d/, and then write the path to be added directly to this file. At this time, the search path added by the library file is valid after restarting. If you want to use the added path to take effect immediately, use the ldconfig command

      • ldconfig: Notify the system to search for library files again

        Insert picture description here

      • /etc/ld.so.conf and /etc/ls.so.conf.d/*.conf //Configuration file

      • /etc/ld.so.cache //Cache file
        -v //Display the process of re-searching the library
        -p //Print out the available library file names and file path mapping relationships that are automatically loaded and cached in memory when the system starts

  • c. Header file: output to the system.
    Default: the system finds the header file in /usr/include. If you want to add the header file search path, use the link to make the
    man file path: install in the man directory under the directory specified by -prefix

    • Generate header file link through link.
      Insert picture description here
    • Default: The system finds the man file in /usr/share/man. At this time, because it is not installed to the default path when compiling and installing, if you want to find the man file, you can use the following two methods:
      man -M /path/to/man_dir command
      Add a MANPATH to the /etc/man_db.conf file
      #

5.3 Source code package compilation example

  • Next, compile and install nginx to understand the source package installation in depth
    • //1. Basic environment preparation [root@localhost ~]# yum -y install gcc gcc-c++ make wget

    • //2. Download the source code package (the source code package must be downloaded from the official site, other sites are not safe) [root@localhost ~]# cd /usr/src
      [root@localhost src]# wget http://nginx.org/ download/nginx-1.12.2.tar.gz

    • //3. Unzip the source package and enter the corresponding directory [root@localhost src]# tar xf nginx-1.12.2.tar.gz
      [root@localhost src]# cd nginx-1.12.2

    • //4. Configure related options and generate Makefile [root@localhost nginx-1.12.2]# ./configure --help|head

      help option:

–help print this message
–prefix=PATH set installation prefix
–sbin-path=PATH set nginx binary pathname
–modules-path=PATH set modules path
–conf-path=PATH set nginx.conf pathname
–error-log-path=PATH set error log pathname
–pid-path=PATH set nginx.pid pathname
–lock-path=PATH set nginx.lock pathname

//The following content is omitted. Use the ./configure --help command to view the available options.//Generally, the
commonly used options are --prefix=PREFIX. This option means to define where the package is installed.//Suggestion
, the source package is Is installed in the /opt/ directory

  • //5. Specify compilation parameters [root@localhost nginx-1.12.2]# ./configure --prefix=/opt/nginx-1.12.2
  • //6. Verify that the command in this step is successful. Anything other than 0 is not considered successful. [root@localhost nginx-1.12.2]# echo $?
    0
  • //7. Compile and install [root@localhost nginx-1.12.2]# make
    [root@localhost nginx-1.12.2]# make install
    [root@localhost nginx-1.12.2]# echo $?
  • //8. Establish a soft link [root@localhost nginx-1.12.2]# ln -s /opt/nginx-1.12.2 /opt/nginx

5.4 Source code compilation error message processing

  • error-1checking for C compiler … not found ./configure: error: C compiler cc is not found

    //Solution
    [root@localhost ~]# yum -y install gcc gcc-c++ make

  • error-2./configure: error: the HTTP rewrite module requires the PCRE library.
    You can either disable the module by using --without-http_rewrite_module
    option, or install the PCRE library into the system, or build the PCRE library
    statically from the source with nginx by using --with-pcre= option.

    //Solution
    [root@localhost ~]# yum install -y pcre-devel

  • error-3./configure: error: the HTTP gzip module requires the zlib library.
    You can either disable the module by using --without-
    http_gzip_module option, or install the zlib library into the
    system, or build the zlib library statically from the source with
    nginx by using --with-zlib= option.

    //Solution:
    [root@localhost ~]# yum -y install zlib-devel

  • error-4./configure: error: SSL modules require the OpenSSL library.
    You can either do not enable the modules, or install the OpenSSL
    library into the system, or build the OpenSSL library statically
    from the source with nginx by using --with-openssl= option.

    //Solution
    [root@localhost ~]# yum -y install openssl-devel

5.5 Source Compilation and Installation-Nginx

1. Prepare the source code package and data files.

  • Obtaining method, obtain Nginx through Nginx official website

    Insert picture description here
  • Download the required image package
  • Download under xshell.

    [root@localhost ~]# wget http://103.95.217.6/nginx.org/download/nginx-1.18.0.tar.gz

  • Unzip the file tar -xf nginx-1.18.0.tar.gz

2. A trilogy of compiling after the documentation is ready

  • We first need to install the gcc compiler, through the command dnf -y install gcc, otherwise the subsequent compilation files cannot be performed.

  • 1. Run the configure configuration file,

    [root@localhost nginx-1.18.0]# ./configure --prefix=/usr/local/nginx

    • It may prompt such an error after running

    ./configure: error: the HTTP gzip module requires the zlib library.
    You can either disable the module by using --without-http_gzip_module
    option, or install the zlib library into the system, or build the zlib library
    statically from the source with nginx by using --with-zlib= option.

    • Solution:

    Install the libraries required by zlib: dnf -y install zlib-devel

  • 2. Run the make command and make install installation command;

    Successful installation:Insert picture description here

  • 3. Run the command to view the web page. Enter the sbin directory of nginx.
    Insert picture description here
    Insert picture description here


5.6, source code compilation and installation-Apache

1. Prepare the source code package and data files.

  • How to obtain Apahce through apache official website
    ![]
  • Download the three image file packages required:
  • Download under xshell.
    Insert picture description here
  • Now copy the files to the directory where the source files are stored in /usr/local/src:

    [root@localhost ~]# mv apr-1.6.5.tar.gz apr-util-1.6.1.tar.gz httpd-2.4.46.tar.gz /usr/local/

  • Then decompress in the terminal: tar -xf httpd-2.4.46.tar.gz (note that all three files must be downloaded and decompressed)

2. A trilogy of compiling after the documentation is ready

  • 1. After decompressing the data, you should see these three folders.

    Insert picture description here

  • We first need to install the gcc compiler, through the command dnf -y install gcc, otherwise the subsequent compilation files cannot be performed.

  • 2. First, we enter the folder apr-1.6.5 to compile and install;

    (Note: If this apr agent is not installed, the following error will appear when compiling the ./configure file in httpd)
    Apache2.4 error checking for APR... no configure: error: APR not found. Please read the documentation.
    So you need Compile apr agent

    • Compile command:

      ttar -zxf apr-1.6.5.tar.gz
      cd apr-1.6.5
      ./configure --prefix=/usr/local/apr
      make && make install

    • There is a problem when compiling Apr. A directory is defined in the configure script file. This directory variable needs to be commented out, otherwise the following error will occur:

      config.status: executing libtool commands
      rm: cannot remove ‘libtoolT’: No such file or directory

  • 3. Secondly, we will explode apr-util error when compiling, so we need to compile and install apr-uitl:

    checking for APR-util… no
    configure: error: APR-util not found. Please read the documentation.

    • Compile command (Note, an error will also be prompted at this time.)

      tar -zxf apr-util-1.3.12.tar.gz
      cd apr-util-1.3.12
      ./configure --prefix=/usr/local/apr-util -with-apr=/usr/local/apr/bin/apr
      make && make install

    • Prompt an error.

      xml/apr_xml.c:35:10: Fatal error: expat.h: No such file or directory
      #include <expat.h>
      ^~~~~~~~~
      Compilation interrupted.
      make[1]: *** [/root/apr-util-1.6.1/build/rules.mk:206:xml/apr_xml.lo] Error 1
      make[1]: Leaving the directory "/root/apr-util -1.6.1"
      make: *** [/root/apr-util-1.6.1/build/rules.mk:118:all-recursive] Error 1

    • Solution:

      dnf -y install expat-devel install this library file.
      It may also report that the pcre package is missing, which is the same package group that installs pcre.

  • 4. After installing the apr and apr-util dependent package groups, we will start our Apache installation

    • Enter the httpd directory and install the configuration file:

      [root@localhost httpd-2.4.46]./configure --prefix=/usr/local/apache --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
      [root@localhost httpd-2.4.46] make
      [root@localhost httpd-2.4.46] make install

    • After the installation is complete, seeing the following content indicates that Apache has been installed successfully.
      Insert picture description here
    • Enter [root@localhost httpd-2.4.46]# /usr/local/apache/bin/apachectl start to start the apache service.
      Then browse the web.
    • Note that you need to turn off the firewall and selinux access control if you cannot access.Insert picture description here

Guess you like

Origin blog.csdn.net/LBJ19224/article/details/109266886