linux centos7源码编译安装LNMP企业级架构

Posted 黄昏单车

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux centos7源码编译安装LNMP企业级架构相关的知识,希望对你有一定的参考价值。

准备工作

# 统一下载存放目录
cd /usr/local/src
# mysql源码包下载网址
https://downloads.mysql.com/archives/community/
# 下载包括 Boost 标头的mysql-5.7.33.tar.gz
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.33.tar.gz
# nginx源码包下载地址
https://nginx.org/en/download.html
# 下载1.18 stable稳定版
wget https://nginx.org/download/nginx-1.18.0.tar.gz
# php 源码包下载地址
https://www.php.net/downloads.php
# 下载php
wget https://www.php.net/distributions/php-7.3.28.tar.gz

# 全部的源码使用wget命令下载到/usr/local/src

安装mwget(如果觉得wget下载太慢,可以安装mwget)

#!/bin/bash
# 下载mwget源码包
wget http://jaist.dl.sourceforge.net/project/kmphpfm/mwget/0.1/mwget_0.1.0.orig.tar.bz2
# 安装扩展
yum -y install bzip2 gcc-c++ openssl-devel intltool
# 解压源码包
bzip2 -d mwget_0.1.0.orig.tar.bz2
tar -xvf mwget_0.1.0.orig.tar 
# 切换进入目录
cd mwget_0.1.0.orig
# 配置
./configure --prefix=/usr/local/wget
# 编译安装
make && make install
# 添加环境变量
echo "export PATH=$PATH:/usr/local/wget/src" >> /etc/profile
# 刷新环境变量配置
source /etc/profile

安装Mysql

# 安装依赖包
yum install -y 
gcc gcc-c++ cmake ncurses ncurses-devel bison openssl-devel
# 解压源码包
tar -zxf mysql-boost-5.7.33.tar.gz
# 移动解压包
mv mysql-5.7.33 /usr/local/mysql
# 创建用户mysql,指定用户使用的shell为/sbin/nologin,表示不登录
useradd -r mysql -s /sbin/nologin
# 改变目录权限,包括子目录,指定用户,用户组
chown -R mysql.mysql /usr/local/mysql
# 切换目录
cd /usr/local/mysql
# cmake源配置选项
cmake \\
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \\
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \\
-DSYSCONFDIR=/etc \\
-DSYSTEMD_PID_DIR=/usr/local/mysql \\
-DDEFAULT_CHARSET=utf8 \\
-DDEFAULT_COLLATION=utf8_general_ci \\
-DMYSQL_DATADIR=/usr/local/mysql/data \\
-DDOWNLOAD_BOOST=1 \\
-DWITH_BOOST=/usr/local/boost \\
-DWITH_SYSTEMD=1

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql 		// 指定mysql数据库安装目录
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock 	// 连接文件位置
-DSYSCONFDIR=/etc 								// 指定配置文件目录
-DSYSTEMD_PID_DIR=/usr/local/mysql 				// 进程文件目录
-DDEFAULT_CHARSET=utf8  						// 指定默认使用的字符集编码
-DDEFAULT_COLLATION=utf8_general_ci 			// 指定默认使用的字符集
-DMYSQL_DATADIR=/usr/local/mysql/data 			// 数据库文件
-DDOWNLOAD_BOOST=1 								// 下载boost
-DWITH_BOOST=/usr/local/boost 					// 指定Boost库的位置
-DWITH_SYSTEMD=1								// 使系统支持MySQL数据库

# 编译并安装
make && make install
# 编辑配置文件my.cnf
# mysql 5.7 版本和以前的有所不同,如果配置文件不做修改,则服务启动失败
vim /etc/my.cnf

[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
# skip-grant-tables 为了方便第一次登录不用密码
skip-grant-tables 
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306   
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

# 修改配置文件的权限
chown mysql:mysql /etc/my.cnf
# 添加环境变量
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
# 重新环境变量配置
source /etc/profile
# 初始化数据库
/usr/local/mysql/bin/mysqld \\
--initialize-insecure \\
--user=mysql \\
--basedir=/usr/local/mysql \\
--datadir=/usr/local/mysql/data

/usr/local/mysql/bin/mysqld 
--initialize-insecure 				//生成初始化密码,实际会生成空密码
--user=mysql 						//指定管理用户
--basedir=/usr/local/mysql 			//指定工作目录
--datadir=/usr/local/mysql/data		//指定数据文件目录

# 添加系统服务
cp ./usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
# 刷新识别mysqld.service服务
systemctl daemon-reload    
# 加入系统自启动
systemctl enable mysqld    
# 启动服务
systemctl start mysqld
# 进入mysql,由于上面my.cnf加了skip-grant-tables,不用密码,回车,回车
mysql -uroot -p
# 重新修改root账号的密码
update mysql.user set authentication_string=password('root') where user='root';
# 刷新权限
flush privileges;
# 退出mysql
exit
# 重新编辑my.cnf,删除skip-grant-tables
vim /etc/my.cnf
# 重新尝试新设置的账号密码登录mysql
mysql -uroot -proot
# 授权远程登陆数据库账号
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
# 刷新权限
flush privileges;
# 退出mysql
exit
# 开启防火墙
systemctl start firewalld.service
# 开启3306端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent
# 重启防火墙
systemctl restart firewalld.service

// 配置参考地址:
//https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html


安装nginx

# 安装依赖包
yum -y install pcre-devel zlib-devel openssl-devel
# 解压
tar -zxf nginx-1.18.0.tar.gz
# 切换目录
cd nginx-1.18.0
# 配置nginx
./configure --prefix=/usr/local/nginx \\
useradd -r www -s /sbin/nologin --user=www --group=www \\
--with-http_ssl_module \\
--with-http_stub_status_module \\
--with-http_realip_module

# --prefix=/usr/local/nginx			安装目录
# useradd -r www -s /sbin/nologin	创建不登录用户www
# --user=www --group=www			以www用户和www用户组安装
# --with*							安装一些模块支持

# 编译安装
make && make install
# 切换进入nginx目录
cd /usr/local/nginx/
# 说下几个重要信息
./sbin/nginx -h

#  -V            : 显示版本号和配置选项信息,加载哪些模块
#  -t            : 检查nginx.conf配置文件语法
#  -s signal     : 发送信号,stop(停止),quit(优雅的退出),
#				   reopen(重新打开配置文件),reload(热重载配置文件)
#  -c filename   : 加载配置文件

# 启动nginx,正常启动的话,106.14.36.65(ip)即可访问页面成功
./sbin/nginx -c ./conf/nginx.conf

# 扩展nginx服务脚本,先停止nginx
./sbin/nginx -s stop
# 编写nginx服务配置
vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=Nginx Web Server
After=network.target

[Service]
Type=forking										#代表后台运行
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf	# 启动
ExecReload=/usr/local/nginx/sbin/nginx -s reload	# 热重载
ExecStop=/usr/local/nginx/sbin/nginx -s quit		# 退出
PrivateTmp=true										# 开启独立空间

[Install]
WantedBy=multi-user.target

# 然后就可以使用以下命令操作nginx
systemctl start nginx		# 启动
systemctl stop nginx		# 停止
systemctl reload nginx		# 重启
systemctl enable nginx		# 开机启动
systemctl disable nginx		# 禁止开机启动

# 出现错误:Warning: nginx.service changed on disk. 
# Run 'systemctl daemon-reload' to reload units,执行下面命令即可:
systemctl daemon-reload

# 添加环境变量
echo "export PATH=$PATH:/usr/local/nginx/sbin" >> /etc/profile
# 重新加载环境
source /etc/profile


安装PHP

# 安装依赖库
yum -y install libxml2-devel libjpeg-devel \\
libpng-devel freetype-devel curl-devel openssl-devel
# 解压
tar -zxf php-7.3.28.tar.gz
# 切换目录
cd 
# 配置
./configure \\
--prefix=/usr/local/php \\
--with-config-file-path=/usr/local/php/etc \\
--enable-fpm \\
--with-fpm-user=www \\
--with-fpm-group=www \\
--with-mysqli=mysqlnd \\
--with-pdo-mysql=mysqlnd

# --prefix=/usr/local/php 		安装目录
# --with-config-file-path=/usr/local/php/etc 	配置文件目录
# --enable-fpm 					开启php-fpm功能(fastcgi)
# --with-fpm-user=www 			以www用户运行php-fpm
# --with-fpm-group=www 			以www用户组运行php-fpm
# --with-mysqli=mysqlnd 			安装php-mysqli扩展
# --with-pdo-mysql=mysqlnd		安装php-pdo-mysql扩展

# 编译安装
make && make install
# 切换目录进入php
cd /usr/local/php
# 拷贝默认配置文件
cp /usr/local/src/php-7.3.28/php.ini-development /usr/local/php/etc/php.ini
# 拷贝主配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# 拷贝子配置文件
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
# php-fpm启动加载顺序
php.ini -> php-fpm.conf -> www.conf

# 注意:下面的添加启动服务两种方式,使用一种即可


# 第一种
# 添加启动服务
cp /usr/local/src/php-7.3.28/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# 加执行权限
chmod +x /etc/init.d/php-fpm
# 启动php服务
service php-fpm start

# 第二种
# 添加启动服务脚本,内容如下:
vim /usr/lib/systemd/system/php-fpm.service

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

[Service]
Type=forking
ExecStart=/usr/local/php/sbin/php-fpm --daemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

# 重新加载启动服务
systemctl daemon-reload
# 启动php服务
systemctl start php-fpm
# 开机启动
systemctl enable php-fpm


# 添加环境变量
echo "export PATH=$PATH:/usr/local/php/bin" >> /etc/profile
# 重新加载环境
source /etc/profile

// 卸载php
./configure --prefix=/tmp/to_remove
rm -rf /usr/local/php


nginx关联php-fpm

# 切换nginx目录
cd /usr/local/nginx
# 移动备份配置文件
mv conf/nginx.conf conf/nginx.conf.bak
# 重新生成一份忽略 #|^$(#号和空格的配置文件),得到内容如下:
grep -Ev '#|^$' conf/nginx.conf > conf/nginx.conf
# -E:使用或者 | 条件
# -v:取反,除了 | 或者条件外的都要

worker_processes  1;
events 
    worker_connections  1024;

http 
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server 
        listen       80;
        server_name  localhost;
        location / 
            root   html;
            index  index.html index.htm;
        
        error_page   500 502 503 504  /50x.html;
        location = /50x.html 
            root   html;
        
    


# 重新整理配置如下:
worker_processes  1;
events 
    worker_connections  1024;

http 
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server 
    	# 监听端口
        listen       80;
        # 真实域名
        server_name  localhost;
        # 项目目录
        root   html;
        location / 
            # 默认首页文件
            index  index.html index.htm;
        
        # 添加php文件支持
        location ~ \\.php$ 
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        
        # 遇到访问错误返回50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html 
            root   html;
        
    

# 重启nginx 
systemctl reload nginx
# 切换进入nginx访问目录
cd html
# 新建index.php,内容如下:
<?php
echo phpinfo();
# 访问php文件
ip/index.php

LNMP的完整安装脚本,执行脚本source mysql.sh

mysql.sh

#!bin/bash

# 下载源码包,安装扩展,解压移动mysql
cd /usr/local/src
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.33.tar.gz
yum install -y gcc gcc-c++ cmake ncurses ncurses-devel bison openssl-devel
tar -zxf mysql-boost-5.7.33.tar.gz
mv mysql-5.7.33 /usr/local/mysql

# 创建组,不登录用户,加权限
useradd -r mysql -s /sbin/nologin
chown -R mysql.mysql /usr/local/mysql

# 配置,编译安装
cd /usr/local/mysql
cmake \\
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \\
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \\
-DSYSCONFDIR=/etc \\
-DSYSTEMD_PID_DIR=/usr/local/mysql \\
-DDEFAULT_CHARSET=utf8 \\
-DDEFAULT_COLLATION=utf8_general_ci \\
-DMYSQL_DATADIR=/usr/local/mysql/data \\
-DDOWNLOAD_BOOST=1 \\
-DWITH_BOOST=/usr/local/boost \\
-DWITH_SYSTEMD=1

make && make install

# 编写配置文件,加入忽略密码登录mysql相关配置,给文件加权限
touch /etc/my.cnf

echo "[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
# skip-grant-tables 为了方便第一次登录不用密码
skip-grant-tables 
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306   
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES" > /etc/my.cnf

chown mysql:mysql /etc/my.cnf

# 初始化数据库
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

# 添加启动服务,环境变量,启动mysql
cp ./usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl enable mysqld
systemctl start mysqld
echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile

nginx.sh

#!bin/bash
# 切换目录
cd /usr/local/src
# 下载源码包
wget https://nginx.org/download/nginx-1.18.0.tar.gz
# 安装依赖包
yum -y install pcre-devel zlib-devel openssl-devel
# 解压
tar -zxf nginx-1.18.0.tar.gz
# 切换目录
cd nginx-1.18.0
# 创建不登录用户www
useradd -r www -s /sbin/nologin
# 配置nginx
./configure --prefix=/usr/local/nginx \\
--user=www --group=www \\
--with-http_ssl_module \\
linux centos7源码编译安装LNMP企业级架构

Linux精华篇—CentOS 7.4下源码编译构建LNMP架构

CentOS7.4 源码编译安装LNMP

centos7源码编译安装LNMP+ZABBIX4.0LTS——php

centos7源码编译安装LNMP+ZABBIX4.0LTS——zabbix server+zabbix agent

企业网站应用模式之—LNMP架构的源码编译超详细步骤,有手就行!