LNMP environment to build (PHP7.2.25)

As a PHP developer, we must know how to build a PHP development environment, the current mainstream PHP and LAMP development environment is a combination of LNMP, this article describes how to build LNMP development environment on CentOS7. *.

The release notes:

CentOS7: 7.7

Nginx: 1.16.1

MySQL:5.7.28

PHP:7.2.25

I needed to install all of the resources are placed under / usr / local / src directory

Ready to work

Install wget

wget Is automatically downloaded from the Internet a free tool files, support through HTTP, HTTPS, FTP the three most common TCP / IP protocol to download, and can be used HTTP proxy.

sudo yum -y install wget

Install net-tools

Minimizing installation CentOS7 If you can not use the ifconfig command, you need to install net-tools, if it is installed you need to install version CentOS6

sudo yum -y install net-tools

Install vim

sudo yum -y install vim

Configure the display line number

vim ~/.vimrc # 编辑.vimrc配置文件
set nu # 输入set nu 后退出保存

Turn off the firewall

systemctl stop firewalld.service  #令关闭防火墙
systemctl disable firewalld.service  #关闭防火墙开机自启动
通过浏览器输入IP测试是否成功

Install Nginx

Installation depends

(1) installation nginxneed to first download the official source code to compile, gcc compiler-dependent environment, if there is no gcc environment, you need to install gcc-c ++.

yum -y install gcc gcc-c++

(2) PCREis a Perl library, Chinese "Perl-compatible regular expression library." In order to install Nginx Nginx is supported with URI rewriting the rewrite module, if you do not install pcre library, you can not use Nginx rewrite module function, Nginx Rewrite module function of enterprise applications almost a must.

yum -y install pcre pcre-devel

(3) zliblibrary provides a variety of compression and decompression of the way, nginx using zlib the contents of the package are http gzip, so you need to install on Centos zlib library.

yum -y install zlib zlib-devel

(4) OpenSSLis a powerful Secure Sockets Layer cryptographic libraries include major cryptographic algorithms commonly used key and certificate management and SSL protocol encapsulation, and provides a wealth of applications for testing or other purposes. nginx not only supports the http protocol, also supports https (http over ssl ie transmission protocol), so you need to install the OpenSSL library.

yum -y install openssl openssl-devel

Description: yum install installation of pcre version is relatively low, but basically does not affect the use, but it is best to manually compile and install the latest stable version of the official website of openssl.

Examine the underlying dependencies

After the above installation dependencies can check each dependency installation was successful by command

rpm -qa pcre pcre-devel
rpm -qa zlib zlib-devel
rpm -qa pcre pcre-devel

Compile and install Nginx

# 这里我们把安装包都放到了/usr/local/src目录下,便于统一管理
cd /usr/local/src  #切换到软件包目录
wget http://nginx.org/download/nginx-1.16.1.tar.gz   #下载nginx源码包
useradd nginx -s /sbin/nologin -M   #创建nginx用户用于管理nginx程序
tar -zxvf nginx-1.16.1.tar.gz  #解压nginx源码包

cd nginx-1.16.1

#预编译
./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx-1.16.1 \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_stub_status_module

make && make install #编译 和 安装

cd /usr/local
ln -s nginx-1.16.1 nginx  #创建nginx的软链接

Installation Notes

--prefix=PATH    #设置安装路劲
--user=USER      #进程用户权限
--group=GROUP    #进程用户组权限
--with-http_v2_module  # HTTP2
--with-http_stub_status_module   #激活状态信息
--with-http_ssl_module  #激活ssl功能

Configuration environment variable

vim /etc/profile
export PATH=/usr/local/nginx/sbin:$PATH
source /etc/profile

Systemd management

Create and edit /usr/lib/systemd/system/nginx.servicedocuments

vim /usr/lib/systemd/system/nginx.service

And add the following (herein is disposed in the path to their mounting Nginx configuration, Nginx installed in the /usr/localdirectory)

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

By nginx yum installed, the default configuration is as follows nginx.service can be used as a reference

# /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Overloaded daemon

Execute the following command to reload systemd, scan new or changes to the unit

systemctl daemon-reload

Set boot from Kai

systemctl enable nginx.service # 设置开机自启
systemctl disable nginx.service # 取消开机自启服务

Nginx service management commonly used commands

systemctl status nginx.service # 查看Nginx状态
systemctl start nginx.service # 开启Nginx
systemctl stop nginx.service # 关闭Nginx
systemctl reload nginx.service # 重载配置
systemctl restart nginx.service  # 重启Nginx(相当于stop&start)

Check the service starts

80 ports can be queried by whom occupied by the command

lsof -i :80

If you can not recognize the command, you need to installlsof

sudo yum -y install lsof

Installing MySQL

Installation depends

(1) cmake is a new version of the MySQL build tools, you must install

sudo yum -y install gcc gcc-c++ cmake ncurses-devel perl perl-devel autoconf bison bison-devel libtirpc libtirpc-devel

Download boost

If MySQL5.7 and above versions installed before you install the compiler need to install boost, because of the high boots need to install mysql version of the library before they can run properly. Otherwise it will report CMake Error at cmake/boost.cmake:81an error

Switch to the /usr/local/srcdirectory, and then downloaded in this directory boost
MySQL5.7.27 required boost version 1.59, later NA MySQL5.7.28

wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz

Baidu network disk link: boost_1_59_0.tar.gz

Compile and install MySQL

# 添加MySQL用户
useradd -s /sbin/nologin -M mysql

# 切换到/usr/src目录
cd /usr/local/src

# 下载MySQL
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28.tar.gz

# 解压MySQL
tar -zxvf mysql-5.7.28.tar.gz

#解压boost,并移至mysql/boost
tar -zxvf boost_1_59_0.tar.gz
mv boost_1_59_0 mysql-5.7.28/boost

# 进到MySQL目录
cd mysql-5.7.28

# 预编译
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.7.28 \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 \
-DWITH_SSL=system \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_DATADIR=/var/lib/mysql/data \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_INNODB_MEMCACHED=1 \
-DWITH_DEBUG=OFF \
-DWITH_ZLIB=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DENABLED_PROFILING=ON \
-DMYSQL_MAINTAINER_MODE=OFF \
-DMYSQL_TCP_PORT=3306

# 编译&安装
make && make install

# 创建软链接
cd /usr/local
ln -s mysql-5.7.28 mysql

Configuration environment variable

# 添加到环境变量
vim /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
source /etc/profile

Modify the configuration file

  1. In /var/libCreate a directory mysqlfolder

    mkdir -p /var/lib/{mysql,mysql/data}
    touch /var/lib/mysql/mysqld.pid
    chown mysql.mysql -R /var/lib/mysql/
  2. Modify /etc/my.cnffile

    # 修改/etc/my.cnf文件,编辑配置文件如下
    [mysqld]
    character-set-server=utf8mb4
    collation-server=utf8mb4_general_ci
    datadir=/var/lib/mysql/data
    socket=/var/lib/mysql/mysql.sock
    
    [mysqld_safe]
    log-error=/var/log/mysql/mysqld.log
    pid-file=/var/lib/mysql/mysqld.pid
    
    [client]
    default-character-set=utf8mb4
  3. Creating mysqld.logand mysqld.pidfiles, and change file permissions

    # 创建mysqld.log 和 mysqld.pid文件
    mkdir /var/log/mysql
    touch /var/log/mysql/mysqld.log
    chown mysql.mysql -R /var/log/mysql/
  4. Initialize the database

    # 初始化数据库, –initialize 表示默认生成一个安全的密码,–initialize-insecure 表示不生成密码
    mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/var/lib/mysql/data

Systemd management

Create a /usr/lib/systemd/system/mysqld.servicefile, and then edit the content as follows

vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/var/lib/mysql/mysqld.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Needed to create system tables
ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd

# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/var/lib/mysql/mysqld.pid $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=/etc/my.cnf

# Sets open_files_limit
LimitNOFILE = 5000

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=true

Overloaded daemon

Execute the following command to reload systemd, scan new or changes to the unit

systemctl daemon-reload

Start MySQL

systemctl start mysqld.service # 启动MySQL
systemctl stop mysqld.service # 关闭MySQL
systemctl status mysqld.service # 查看MySQL状态

Boot from Kai

systemctl enable mysqld.service # 设置开机自启
systemctl disable mysqld.service # 取消开机自启

Log in MySQL

mysql -u root -p #第一次登陆不需要密码,回车即可
set password for root@localhost = password('root');  #修改密码

Installing PHP

Installation depends

sudo yum -y install gcc gcc-c++ zip unzip libxml2 libxml2-devel curl-devel autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel gd-devel bzip2 bzip2-devel libzip libzip-devel libwebp libwebp-devel

Compile and install PHP

cd /usr/local/src
tar -zxvf php-7.2.25.tar.gz
cd  php-7.2.25
./configure \
--prefix=/usr/local/php-7.2.25 \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql-sock=/var/lib/mysql/mysql.sock \
--with-gd \
--with-webp-dir \
--with-png-dir \
--with-gettext \
--with-jpeg-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-bz2 \
--with-openssl \
--with-curl \
--enable-mbstring \
--enable-static \
--enable-zip \
--enable-bcmath \
--enable-ftp \
--enable-pcntl \
--enable-soap \
--enable-calendar \
--enable-sockets \
--enable-exif \
--enable-xml

make && make install

Compile arguments detailed

./configure \
--prefix=/usr/local/php-7.2.25 \ # 指定安装路径
--enable-fpm \             # 表示激活PHP-FPM方式服务,即FactCGI方式运行PHP服务。
--with-fpm-user=nginx \    # 指定PHP-FPM进程管理的用户为nginx,此处最好和Nginx服务用户统一。
--with-fpm-group=nginx \   # 指定PHP-FPM进程管理用户组为nginx,此处最好和Nginx服务用户组统一。
--enable-mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-mysql-sock=/var/lib/mysql/mysql.sock \
--with-gd \          # 打开gd库的支持
--with-webp-dir \
--with-png-dir \
--with-gettext \     # 实现了NLS (Native Language Support) API,他可以用来国际化您的PHP程序
--with-jpeg-dir \
--with-freetype-dir \
--with-iconv-dir \   # 包含了 iconv 字符集转换功能的接口。
--with-zlib-dir \    # 打开zlib库的支持,用于http压缩传输
--with-bz2 \         # 用于透明地读写 bzip2(.bz2)压缩文件。
--with-openssl \     # 打开openssl,加密传输时用到
--with-curl \        # 打开curl浏览工具的支持 
--enable-mbstring \  # 多字节,字符串的支持
--enable-static \    # 生成静态链接库
--enable-zip \       # 打开对zip的支持
--enable-bcmath \
--enable-ftp \       # 通过文件传输协议 (FTP) 提供对文件服务器的客户端访问
--enable-pcntl \     # 多进程
--enable-soap \
--enable-calendar \
--enable-sockets \   # 打开 sockets 支持
--enable-exif \      # 可交换图像信息
--enable-xml

Configuration

cd /usr/local

ln -s php-7.2.25 php
cp /usr/local/src/php-7.2.25/php.ini-development /usr/local/php-7.2.25/lib/php.ini
 
vim /usr/local/php/lib/php.ini
date.timezone = PRC  (大约在934行)
expose_php = Off  #避免PHP信息暴露在http头中(大约369行)
 
display_errors = Off(生产环境设置为off,开发环境就设置为On,便于调试)
 说明:设置了dispaly_errors为off后,需要在php-fpm.conf中开启错误日志记录路径error_log = log/php-fpm.log
 
cd /usr/local/php 
cp etc/php-fpm.conf.default etc/php-fpm.conf

cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
cd /usr/local/php
sbin/php-fpm
ps -e | grep php-fpm

如果在编译PHP时指定了--with-mysql=mysqlnd和--with-pdo-mysql=mysqlnd的参数,那么在生产中可能会遇到socket连接问题,解决办法是在php.ini里加入命令: pdo_mysql.default_socket=/var/lib/mysql/mysql.sock

最好是在编译PHP的时候,指定mysql.socket的位置:
--with-mysql-sock=/var/lib/mysql/mysql.sock

Management PHP-FPM

vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid
error_log = log/php-fpm.log #24行这个在php.ini设置display_errors = Off时启用
设置完之后重启服务器
向进程发送信号,就可以完成进程管理
停止: kill -INT `cat /usr/local/php/var/run/php-fpm.pid`
平滑停止: kill -QUIT `cat /usr/local/php/var/run/php-fpm.pid`
重启:kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
重新打开日志:kill -USR1 `cat /usr/local/php/var/run/php-fpm.pid`

Configuration environment variable

vim /etc/profile
export PATH=/usr/local/php/bin:$PATH
source /etc/profile

Configuration Systemd Service

In fact, php file php-fpm.service has helped us configured, we just need to be copied to the specified location, and enabled on the line.

cp /usr/local/src/php-7.2.25/sapi/fpm/php-fpm.service /usr/lib/systemd/system/

Edit /usr/lib/systemd/system/php-fpm.servicethe file and modify the following:

# It's not recommended to modify this file in-place, because it
# will be overwritten during upgrades.  If you want to customize,
# the best way is to use the "systemctl edit" command.

[Unit]
Description=The PHP FastCGI Process Manager
After=network.target

[Service]
Type=simple
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

Overloaded daemon

Execute the following command to reload systemd, scan new or changes to the unit

systemctl daemon-reload

Boot from Kai

systemctl enable php-fpm.service

Start php-fpm

systemctl start php-fpm.service

Associated Nginx and PHP

nginx.conf Configuration

#user  nobody;
# 有一个工作的子进程,可以自行修改,但太大无益,因为要争夺CPU
# 一般设置CPU数 * 核数
worker_processes  1; 

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
#一般是配置Nginx进程与连接的特性
#若几个同时工作
    multi_accept on; #打开同时接受多个新网络连接请求的功能。
    use epoll;  #使用epoll事件驱动,因为epoll的性能相比其他事件驱动要好很多
    worker_connections  10240; #这是指一个子进程最大允许连接10240个连接
}


http { # 这是配置http服务器的主要段
    include       mime.types;
    default_type  application/octet-stream;
    
    #隐藏Nginx软件版本号
    server_tokens off;
    
    #激活tcp_nodelay功能,提高I/O性能
    tcp_nodelay on;

    # 设置读取客户端请求头数据的超时时间。此处的数值为15,其单位是秒,为经验参考值
    client_header_timeout 15;

    # 设置读取客户端请求体的超时时间
    client_body_timeout 15;

    # 指定响应客户端的超时时间
    send_timeout 25;

    # 上传文件大小限制
    client_max_body_size 8m;
    
    #压缩配置
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types text/plain text/css text/xml application/javascript;
    gzip_vary on;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #include extra/*.conf;
    server {
        listen       80;
        server_name  www.blog.com;
        root         html;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            index  index.php index.html index.htm;
            if (!-e $request_filename) {
                rewrite ^/(.*)$ /index.php/$1 last;
            }
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
}

Installation Redis

Compile and install

# 解压源码文件
tar -zxvf redis-5.0.7.tar.gz

# 切换到解压目录
cd redis-5.0.7

# 编译安装
make PREFIX=/usr/local/redis-5.0.7 install

mkdir /usr/local/redis-5.0.7/etc
cp redis.conf /usr/local/redis-5.0.7/etc/

# 创建软链接
cd /usr/local
ln -s redis-5.0.7 redis

Configuration environment variable

vim /etc/profile
export PATH=/usr/local/redis/bin:$PATH
source /etc/profile # 使修改立即生效

Configuration running in the background

Let redisa background process running after

vim /usr/local/redis/etc/redis.conf

# daeonize no(大约136行)
# 改为 ->
daemonize yes

Configuration Systemd Service

In /usr/lib/systemd/system/adding a redis.servicefile, and add the following

[Unit]
Description=Redis
After=network.target
 
[Service]
Type=forking
PIDFile=/var/run/redis_6379.pid
ExecStart=/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
ExecStop=/usr/local/redis/bin/redis-cli shutdown
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

Overloaded daemon

Execute the following command to reload systemd, scan new or changes to the unit

systemctl daemon-reload

Boot from Kai

systemctl enable redis.service

Start redis service

systemctl start redis.service

Reference material

centos7 source compiler installation mysql5.7

mysql in linux7 under the relevant configuration systemd

Managing MySQL Server with systemd

centos7 7.3php compile and install

centos7 compiled installation php7.3

cmake Installation and Configuration Getting Started Guide

CMake 3.15 and gcc 5.3.0 compiler

CentOS7 upgrade to OpenSSL 1.1.1

Guess you like

Origin www.cnblogs.com/itbsl/p/12059892.html
Recommended