Linux 下编译搭建 LNMP 环境

文章
林里克斯

很多项目都需要运行在 LNMP 环境,比如监控 Zabbix,国外博客平台 Wordpress 等。LNMP 还有一个一样的环境叫 LAMPLNMP = Linux + Nginx + MySQL + PHP,`LAMP = Linux + Apache + MySQL + PHP

Linux 下编译搭建 LNMP 环境


试验平台:CentOS Linux 7.6.1810

Nginx Version:Nginx-1.18.0

MySQL Version:MySQL-5.7.32

PHP Version:php-7.4.12



一、Linux


1.Linux 这里选择了 CentOS 7.6.1810 大多数云厂商都提供了次镜像供选择使用。

2.LNMPLAMP 有许多的一键安装平台

  • LNMP.org
https://lnmp.org/
  • 宝塔面板
https://www.bt.cn/

二、Nginx


1.安装所需依赖

$ sudo yum -y install gcc gcc-c++ pcre-devel openssl-devel zlib-devel make

2.官网下载最新 tar

$ wget http://nginx.org/download/nginx-1.18.0.tar.gz
$ tar xf nginx-1.18.0.tar.gz

3.创建用户环境

$ sudo groupadd --system nginx
$ sudo useradd --system -g nginx -G nginx -d /data/nginx/ -s /sbin/nologin -c "Nginx User" nginx

4.编译安装

$ ./configure --prefix=/data/nginx --sbin-path=/data/nginx/sbin/nginx --conf-path=/data/nginx/conf/nginx.conf --error-log-path=/data/nginx/logs/error.log --http-log-path=/data/nginx/logs/access.log --pid-path=/data/nginx/logs/nginx.pid --lock-path=/data/logs/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre

$ make && make install

5.编辑配置文件

$ vim /data/nginx/conf/nginx.conf
server {
        listen       8080;   #如果你跟我一样用的普通用户将端口调整为 8080 或 1024+,因为只有 root 用户才能用 1024 以下的端口。如果用的 root 用户可以忽略。
}
include /usr/local/nginx/conf/conf.d/*.conf;

6.查看版本

$ cd /data/nginx/sbin/
$ ./nginx -v
nginx version: nginx/1.18.0

三、MySQL


1.下载官方 tar

$ wget -c https://downloads.mysql.com/archives/get/file/mysql-5.7.32.tar.gz
#如果链接失效了,可以使用 清华大学 的镜像站
wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.32.tar.gz

2.安装依赖

$ yum -y install gcc gcc-c++ ncurses ncurses-devel bison libgcrypt perl make cmake

3.创建用户

$ sudo groupadd --system mysql
$ sudoa useradd --system -g mysql -G mysql -d /data/mysql/ -s /sbin/nologin -c "MySQL User" mysql

4.下载 boost

$ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.32.tar.gz
$ tar xf mysql-boost-5.7.32.tar.gz
$ cp -rp mysql-5.7.32/boost /data/mysql/
  1. cmake 编译
$ cmake . -DCMAKE_INSTALL_PREFIX=/data/mysql -DMYSQL_DATADIR=/data/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DENABLE_DOWNLOADS=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DMYSQL_UNIX_ADDR=/data/mysql/data/mysqld.sock -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/data/mysql/boost
#返回以下为 cmake 编译完成,会生成一个 Makefile 文件
-- Build files have been written to: /usr/local/mysql-5.7.23

6.开始make编译

$ cd ~/mysql-5.7.32/
$ make
#等待时间较长
[100%] Built target my_safe_process
#安装完成
$ make install

7.添加环境变量

$ sudo vim /etc/profile.d/mysql.sh
#全局变量
$ vim /home/users/.bash_profile
#为某一指定用户配置环境变量

export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH

$ sudo sourece /etc/profile
#使环境变量生效

8.初始化数据库

$ mysqld --initialize --user=mysql --basedir=/data/mysql --datadir=/data/mysql/data &>/tmp/passwd.txt
# –initialize 表示默认生成一个安全的密码,–initialize-insecure 表示不生成密码

9.使用 mysql 自带的脚本

$ cp -rp /data/mysql/support-files/mysql.server /data/mysql/mysqld.sh

10.编辑配置文件

$vim /etc/my.cnf
[mysqld]
datadir=/data/mysql/data/
socket=/data/mysql/data/mysqld.sock
port=3306
user=mysql
character_set_server = utf8
collation_server = utf8_general_ci
symbolic-links=0

[mysqld_safe]
log-error=/data/mysql/logs/mysql.log
pid-file=/data/mysql/logs/mysql.pid

!includedir /etc/my.cnf.d

11.创建 logs 目录

$ cd /data/mysql/
$ mkdir logs

12.启动mysql

$ cd /data/mysql/
$ sh mysqld.sh start
Starting MySQL. SUCCESS!

13.登陆测试

$ mysql -u root -p -S /data/mysql/data/mysqld.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23 Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

14.修改数据库密码

mysql>  ALTER USER 'root'@'localhost' IDENTIFIED BY 'MySQL123..';
Query OK, 0 rows affected (0.00 sec)

四、PHP


1.官网 downloads

https://www.php.net/downloads.php

2.下载

$ wget https://www.php.net/distributions/php-7.4.12.tar.gz

3.安装所需依赖

$ sudo yum -y install gcc gcc-c++ libxml2-devel sqlite-devel libcurl-devel make
$ sudo yum -y install http://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-6.9.6-1.el7.remi.x86_64.rpm
$ sudo yum -y install http://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-devel-6.9.6-1.el7.remi.x86_64.rpm

4.解压

$ tar xf php-7.4.12.tar.gz 
$ cd php-7.4.12/

5.编译安装

$ ./configure --prefix=/data/php --with-config-file-path=/data/php --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib --with-openssl --with-iconv --with-curl --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --with-xmlrpc --with-libxml-dir=/usr --enable-pcntl --enable-mbstring --enable-ftp --enable-zip --enable-fpm --enable-xml --enable-sockets --enable-soap --enable-libxml --enable-openssl --enable-gd --enable-curl --enable-pcntl --enable-iconv
#这里把基本上需要用到的模块都加上了,可以根据自己所需增减 PHP 模块

$ make && make install

6.验证 php 版本

$ cd /data/php/bin/
$ ./php -v
PHP 7.4.12 (cli) (built: Nov 20 2020 17:00:35) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

7.增加用户环境变量

$ vim /home/users/.bash_profile
#编辑所需增加用户的环境变量文件,注意这只是个某一个用户增加,不是全局变量添加。若需全局,在 /etc/profile.d/ 下添加

$ sudo vim /etc/profile.d/php.sh

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH=/data/php/bin:/data/php/sbin:$PATH
export PATH

$ source /home/users/.bash_profile

五、配置 PHP


1.编辑 php.conf

$ vim /data/php/etc/php-fpm.conf

[global]
pid = /data/php/logs/php-fpm.pid
error_log = /data/php/logs/php-fpm.log
log_level = notice
include=/data/php/etc/php-fpm.d/*.conf

[www]
listen = /data/php/logs/php-cgi-73.sock
listen.backlog = 8192
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.status_path = /phpfpm_73_status
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
request_terminate_timeout = 100
request_slowlog_timeout = 30
slowlog = /data/php/logs/php-slow.log
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

2.编辑 nginx.conf

$ vim /data/nginx/conf/nginx.conf

server {
    listen       8080;
    server_name  localhost;

    location / {
        root   html;
        index  index.php index.html index.htm;
    }

    location ~ .php$ {
            root /data/nginx/html;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
}

3.启动 php-fpm

$ cd /data/php/sbin/
$ ./php-fpm

4.验证测试

Linux 下编译搭建 LNMP 环境

5.测试通过后,建议将 phpinfo 的信息删除,包括 nginxlocationphp-fpm.conf 里监听的 9000 端口都可以去除,业务层用不到。


Over~

版权协议须知!

本篇文章来源于 Uambiguous ,如本文章侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意

785 0 2020-11-21


分享:
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif
博主卡片
林里克斯 博主大人
一个致力于Linux的运维平台
运维时间
搭建这个平台,只为分享及记载自己所遇之事和难题。

现在时间 2024-04-28

今日天气
站点统计
  • 文章总数:240篇
  • 分类总数:29个
  • 评论总数:10条
  • 本站总访问量 216588 次

@奥奥

@Wong arrhenius 牛比

@MakerFace 厉害了!