Build CentOS7 to install the new LNMP environment

  • 2020-05-12 06:38:14
  • OfStack

Since the company requires the latest version of ZABBIX2.4.4, the latest version of CENTOS7 and the new version of LNMP environment are needed, so I tried to build the LNMP system with the new version of ZABBIX2.4.4. The environment version is as follows:

System: CentOS 7 x86_64 NGINX: nginx - 1.7.12 Database: mariadb-10.0.13 PHP: php - 5.5.23

To do some preliminary work, first change the firewall of centos7 to iptables. You can see the link below

centos7 firewall set iptables

After changing to iptables, you can clear out the filter rules in iptables, and then turn off selinux service. Don't forget to install gcc gcc-c ++ wget net-tools.

Start by installing mariadb

Because it takes a long time to compile the database, I have downloaded the compiled version of the binary package, mariadb-10.0.13-linux-glibc_214-x86_64.tar.gz

1. Download the binary package to the directory /usr/local/src:

[root@centos74 ~]# cd /usr/local/src/
[root@centos74 src]# wget http://ftp.osuosl.org/pub/mariadb/mariadb-10.0.13/bintar-quantal-amd64/mariadb-10.0.13-linux-glibc_214-x86_64.tar.gz[/code]
2 , unpack the compressed package to /usr/local Directory:
[code][root@centos74 src]# tar zvxf mariadb-10.0.13-linux-glibc_214-x86_64.tar.gz -C /usr/local/

3. Create mariadb data initialization directory /data/mysql:

[root@centos74 src]# mkdir -p /data/mysql

4. Add the system user mysql and disable the login of the system. Meanwhile, modify the main and group of the data initialization directory of mariadb to mysql:

[root@centos74 src]# useradd -r -s /sbin/nologin mysql ;chown -R mysql.mysql /data/mysql/

5. Rename the extracted mariadb directory:

[root@centos74 src]# mv /usr/local/mariadb-10.0.13-linux-x86_64/ /usr/local/mysql

6. Enter the renamed directory and initialize mariadb:


[root@centos74 src]# cd /usr/local/mysql/
[root@centos74 mysql]# ./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
Installing MariaDB/MySQL system tables in '/data/mysql' ...
140906 2:03:19 [Note] InnoDB: Using mutexes to ref count buffer pool pages
140906 2:03:19 [Note] InnoDB: The InnoDB memory heap is disabled
140906 2:03:19 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
140906 2:03:19 [Note] InnoDB: Compressed tables use zlib 1.2.3
140906 2:03:19 [Note] InnoDB: Using Linux native AIO
140906 2:03:19 [Note] InnoDB: Using CPU crc32 instructions
140906 2:03:19 [Note] InnoDB: Initializing buffer pool, size = 128.0M
........................................................................
The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Support MariaDB development by buying support/new features from
SkySQL Ab. You can contact us about this at sales@skysql.com.
Alternatively consider joining our community based development effort:
http://mariadb.com/kb/en/contributing-to-the-mariadb-project/

Error: WARNING: The host 'test4' could not be looked up with resolveip
Solution: vim /etc/hosts add 192.168.1.242 test4 on the last line

Error reporting:./bin/mysqld: error loading shared libraries: libaio so cannot open object file No file or directory
Solutions: yum -y install libaio-devel libaio

7. Copy the configuration file to /etc directory before overwriting my.cnf:

[root@centos74 mysql]# cp support-files/my-large.cnf /etc/my.cnf

cp: does it cover "/etc/ my.cnf "? y

8. Copy the mysql startup script file to /etc/ init.d and rename it mysqld:

[root@centos74 mysql]# cp support-files/mysql.server /etc/init.d/mysqld

At the same time, the permission to modify the startup script is 755:

[root@centos74 mysql]# chmod 755 !$
chmod 755 /etc/init.d/mysqld

9. Edit the startup script and define the datadir path:

[root@centos74 mysql]# vim /etc/init.d/mysqld

Define the data storage path:
datadir=/data/mysql

10. Put the mariadb native command into $PATH


[root@localhost ~]# PATH=$PATH:/etc/init.d/# Currently valid, restart shell Is the failure 
[root@localhost ~]# echo "export PATH=$PATH:/etc/init.d/" >>/etc/profile
[root@localhost ~]# echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile
[root@localhost ~]# source !$

11. Launch mariadb:


[root@centos74 mysql]# /etc/init.d/mysqld start

Starting MySQL. SUCCESS!

Step 2, start installing php

In this case, there is a difference between php installation for Nginx and php installation for apache, because php in Nginx combines nginx in fastcgi, which can be understood as nginx proxy fastcgi of php, while apache calls php as its own module. Similarly, php official download address: http: / / www php. net/downloads php

Download php


[rot@localhost src]# cd /usr/local/src
[root@localhost src]# wget http://am1.php.net/distributions/php-5.5.23.tar.gz

Unpack the php


[root@localhost src]# tar zxf php-5.5.23.tar.gz

Create related accounts


[root@localhost src]# useradd -s /sbin/nologin php-fpm

Configure compilation parameters

[root@localhost src]# cd php-5.5.23
[root@localhost src]# yum -y install gcc make gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel
[root@localhost php-5.5.23]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=mysqlnd  --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --enable-bcmath --enable-mbstring --enable-sockets --with-gd --with-libxml-dir=/usr/local --with-gettext

Error: configure: error: xml2-config not found. Please check your libxml2 installation.
Solutions: yum -y install libxml2-devel

Error: configure: error: Please reinstall the libcurl distribution easy h should be in < curl-dir > /include/curl/
Solutions: yum -y install libcurl-devel

Error: configure: error: jpeglib.h not found.
Solutions: yum -y install libjpeg-turbo-devel

Error: configure: error: png.h not found.
Solutions: yum -y install libpng-devel

Error: configure: error: freetype-config not found
Solution: configure: error: mcrypt.h not found. Please reinstall libmcrypt.

Error: configure: error: mcrypt.h not found. Please reinstall libmcrypt.
Solutions: yum -y install libmcrypt-devel

Install php


[root@localhost php-5.3.27]# make && make install

For each of the above steps, the next step cannot be performed without complete correctness. echo $? See if the result is "0" and if it is not, it is not executed correctly.

Modify the configuration file


cp php.ini-production /usr/local/php/etc/php.ini
vim /usr/local/php/etc/php-fpm.conf

Write the following to the file:


[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
user = php-fpm
group = php-fpm
listen.owner = nobody
listen.group = nobody
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024

After saving the configuration file, the method to verify that the configuration is correct is:

/usr/local/php/sbin/php-fpm -t

If words such as "test is successful" appear, there is no problem with the configuration.

Start the php - fpm


cp /usr/local/src/php-5.5.23/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
service php-fpm start


If you want it to boot up, execute:

chkconfig php-fpm on

Check whether to start:

ps aux |grep php-fpm

See if there are many processes (about 20).

Install nginx

Download nginx


[root@localhost ~]# PATH=$PATH:/etc/init.d/# Currently valid, restart shell Is the failure 
[root@localhost ~]# echo "export PATH=$PATH:/etc/init.d/" >>/etc/profile
[root@localhost ~]# echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile
[root@localhost ~]# source !$
0

Unpack the nginx

tar zxvf nginx-1.7.12.tar.gz

Configure compilation parameters


cd nginx-1.7.12
./configure \
--prefix=/usr/local/nginx \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre

Error:./configure: error: the HTTP rewrite requires the PCRE library.
Solutions: yum -y install libxml2-devel0

Error:./configure: error: the HTTP module requires the zlib library
Solutions: yum install -y zlib-devel

Compile nginx

make

Install nginx

make install

Write nginx startup scripts and add system services

vim /etc/init.d/nginx

Write the following:


#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() {
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}

reload(){
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

restart(){
    stop
    start
}

configtest(){
  $NGINX_SBIN -c $NGINX_CONF -t
  return 0
}

case "$1" in
 start)
    start
    ;;
 stop)
    stop
    ;;
 reload)
    reload
    ;;
 restart)
    restart
    ;;
 configtest)
    configtest
    ;;
 *)
    echo $"Usage: $0 {start|stop|reload|restart|configtest}"
    RETVAL=1
esac

exit $RETVAL

After saving, change permissions:


[root@localhost ~]# PATH=$PATH:/etc/init.d/# Currently valid, restart shell Is the failure 
[root@localhost ~]# echo "export PATH=$PATH:/etc/init.d/" >>/etc/profile
[root@localhost ~]# echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile
[root@localhost ~]# source !$
3

If you want to boot, please execute:


[root@localhost ~]# PATH=$PATH:/etc/init.d/# Currently valid, restart shell Is the failure 
[root@localhost ~]# echo "export PATH=$PATH:/etc/init.d/" >>/etc/profile
[root@localhost ~]# echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile
[root@localhost ~]# source !$
4

Change the nginx configuration

First, clean out the original configuration file:


[root@localhost ~]# PATH=$PATH:/etc/init.d/# Currently valid, restart shell Is the failure 
[root@localhost ~]# echo "export PATH=$PATH:/etc/init.d/" >>/etc/profile
[root@localhost ~]# echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile
[root@localhost ~]# source !$
5

" > "This symbol means to redirect, and you can use it alone to quickly empty a text document.


[root@localhost ~]# PATH=$PATH:/etc/init.d/# Currently valid, restart shell Is the failure 
[root@localhost ~]# echo "export PATH=$PATH:/etc/init.d/" >>/etc/profile
[root@localhost ~]# echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile
[root@localhost ~]# source !$
6

Write the following:


user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
  use epoll;
  worker_connections 6000;
}

http
{
  include mime.types;
  default_type application/octet-stream;
  server_names_hash_bucket_size 3526;
  server_names_hash_max_size 4096;
  log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
  '$host "$request_uri" $status'
  '"$http_referer" "$http_user_agent"';
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 30;
  client_header_timeout 3m;
  client_body_timeout 3m;
  send_timeout 3m;
  connection_pool_size 256;
  client_header_buffer_size 1k;
  large_client_header_buffers 8 4k;
  request_pool_size 4k;
  output_buffers 4 32k;
  postpone_output 1460;
  client_max_body_size 10m;
  client_body_buffer_size 256k;
  client_body_temp_path /usr/local/nginx/client_body_temp;
  proxy_temp_path /usr/local/nginx/proxy_temp;
  fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
  fastcgi_intercept_errors on;
  tcp_nodelay on;
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 8k;
  gzip_comp_level 5;
  gzip_http_version 1.1;
  gzip_types text/plain application/x-javascript text/css text/htm application/xml;

server
{
  listen 80;
  server_name localhost;
  index index.html index.htm index.php;
  root /usr/local/nginx/html;

  location ~ \.php$ {
    include fastcgi_params;
    fastcgi_pass unix:/tmp/php-fcgi.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
  }

}

}

After saving the configuration, check whether there are any errors in the following configuration files:


/usr/local/nginx/sbin/nginx -t

If the display is as follows, the configuration is correct; otherwise, the configuration file needs to be modified according to the error:

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

Start the nginx:


[root@localhost ~]# PATH=$PATH:/etc/init.d/# Currently valid, restart shell Is the failure 
[root@localhost ~]# echo "export PATH=$PATH:/etc/init.d/" >>/etc/profile
[root@localhost ~]# echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile
[root@localhost ~]# source !$
9

If you can't start, please see the "/ usr local nginx/logs/error log" file, check whether nginx activation:


ps aux |grep nginx

See if there is a process.

Test whether the php file is parsed

Create a test file:


vim /usr/local/nginx/html/2.php

It reads as follows:


<?php echo phpinfo();?>

Testing:


[root@localhost nginx]# curl localhost/2.php

Or use the browser open http: / / YourServerIPAddress / 2. php

Important: if you can't parse it, check the log and find that you can't connect php. My version of php is 5.5.23. The newer version needs to be added in the php/etc/ php-fpm.conf file


listen.owner = nobody
listen.group = nobody

With these two lines, restart the service for 1 more time to use php

The reason is that the file /tmp/ php-fcgi.sock has no read permissions

At this point, the latest version of the LNMP environment source code compilation and installation is complete


Related articles: