CentOS 7 编译部署LAMP环境

1、需求以及环境准备

1.1、版本需求

SERVICE VERSION
Linux CentOS 7.6
Apache 2.4.37
Mysql 5.7.25
Php 7.1.33
Openssl 1.1.1i
Curl 7.74.0
  • LAMP=Linux+Apache+Mysql+Php
  • LNMP=Linux+Nginx+Mysql+Php
  • LNMT=Linux+Nginx+Mysql+Tomcat
    • 当然,Mariadb也可以替换Mysql

1.2、环境准备

"内网服务器,没有要求的话,直接把firewalld关了"
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@localhost ~]# sestatus
SELinux status:                 disabled
[root@localhost ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
"主机名看需求修改,不是强制项"
[root@localhost ~]# hostnamectl set-hostname --static LAMP-Server

1.3、安装包准备

[root@LAMP-Server ~]# cd /usr/local/src/
[root@LAMP-Server src]# wget http://archive.apache.org/dist/httpd/httpd-2.4.37.tar.gz
[root@LAMP-Server src]# wget https://archive.apache.org/dist/apr/apr-1.4.5.tar.gz
[root@LAMP-Server src]# wget https://archive.apache.org/dist/apr/apr-util-1.4.1.tar.gz
[root@LAMP-Server src]# wget https://ftp.pcre.org/pub/pcre/pcre-8.10.tar.gz
[root@LAMP-Server src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.25.tar.gz
[root@LAMP-Server src]# wget https://www.php.net/distributions/php-7.1.33.tar.gz
[root@LAMP-Server src]# wget https://www.openssl.org/source/openssl-1.1.1i.tar.gz
[root@LAMP-Server src]# wget https://github.com/curl/curl/releases/download/curl-7_74_0/curl-7.74.0.tar.gz
[root@LAMP-Server src]# tar xf httpd-2.4.37.tar.gz
[root@LAMP-Server src]# tar xf apr-1.4.5.tar.gz
[root@LAMP-Server src]# tar xf apr-util-1.4.1.tar.gz
[root@LAMP-Server src]# tar xf pcre-8.10.tar.gz
[root@LAMP-Server src]# tar xf mysql-boost-5.7.25.tar.gz
[root@LAMP-Server src]# tar xf php-7.1.33.tar.gz
[root@LAMP-Server src]# tar xf openssl-1.1.1i.tar.gz
[root@LAMP-Server src]# tar xf curl-7.74.0.tar.gz
"都是国外服务器,下载都比较慢,已经打包上传到百度云,担心有问题的,可以自己wget下载,或者使用迅雷下载"
链接:https://pan.baidu.com/s/1i_LZzXnRQsFEbgx8jrA9KA 
提取码:d4pj 

2、编译升级Openssl

"下载编译所需环境"
[root@LAMP-Server src]# yum -y install gcc gcc-c++ glibc make autoconf openssl-devel

2.1、查看当前Openssl版本

[root@LAMP-Server src]# openssl version
OpenSSL 1.0.2k-fips  26 Jan 2017

2.2、备份当前版本Openssl文件

[root@LAMP-Server src]# mv /usr/bin/openssl{,-1.0.2k}

2.3、编译新版本Openssl

[root@LAMP-Server src]# cd openssl-1.1.1i/
[root@LAMP-Server openssl-1.1.1i]# ./config shared --prefix=/usr/local/openssl-1.1.1i && make && make install

2.3、生成新版本Openssl

[root@LAMP-Server openssl-1.1.1i]# ln -s /usr/local/openssl-1.1.1i/bin/openssl /usr/bin/openssl
[root@LAMP-Server openssl-1.1.1i]# echo "/usr/local/openssl-1.1.1i/lib" >> /etc/ld.so.conf
[root@LAMP-Server openssl-1.1.1i]# /sbin/ldconfig
[root@LAMP-Server openssl-1.1.1i]# openssl version
OpenSSL 1.1.1i  8 Dec 2020

3、编译部署Apache

"Apache编译安装依赖apr、apr-util、pcre"
"使用 ./configure --help 命令可以查看相关的模块,有需要的模块,可以在编译的时候加入"
[root@LAMP-Server ~]# mkdir /usr/local/httpd-2.4.37
[root@LAMP-Server ~]# cd /usr/local/src/apr-1.4.5/
[root@LAMP-Server apr-1.4.5]# ./configure --prefix=/usr/local/httpd-2.4.37/apr-1.4.5 && 、
make && \
make install
--------------------------------------------------------------------------------------------------------------
[root@LAMP-Server ~]# cd /usr/local/src/apr-util-1.4.1/
[root@LAMP-Server apr-util-1.4.1]# ./configure --prefix=/usr/local/httpd-2.4.37/apr-util-1.4.1 \
--with-apr=/usr/local/httpd-2.4.37/apr-1.4.5 && \
make && \
make install
--------------------------------------------------------------------------------------------------------------
[root@LAMP-Server ~]# cd /usr/local/src/pcre-8.10/
[root@LAMP-Server pcre-8.10]# ./configure --prefix=/usr/local/httpd-2.4.37/pcre-8.10 && \
make && \
make install
--------------------------------------------------------------------------------------------------------------
[root@LAMP-Server ~]# cd /usr/local/src/httpd-2.4.37/
[root@LAMP-Server httpd-2.4.37]# ./configure --prefix=/usr/local/httpd-2.4.37 \
--with-apr=/usr/local/httpd-2.4.37/apr-1.4.5 \
--with-apr-util=/usr/local/httpd-2.4.37/apr-util-1.4.1 \
--with-pcre=/usr/local/httpd-2.4.37/pcre-8.10 && \
make && \
make install

3.1、启动Apache

[root@LAMP-Server ~]# echo 'export PATH=/usr/local/httpd-2.4.37/bin:$PATH' >> /etc/profile
[root@LAMP-Server ~]# source /etc/profile 
[root@LAMP-Server ~]# apachectl -k start
[root@LAMP-Server httpd-2.4.37]# curl http://192.168.72.128/
<html><body><h1>It works!</h1></body></html>
"可以访问到apache服务默认的内容了"

4、编译部署Mysql

"安装编译所需的环境依赖"
[root@LAMP-Server ~]# yum -y install gcc gcc-c++ cmake ncurses ncurses-devel bison
"创建mysql普通用户,禁止登陆bash"
[root@LAMP-Server ~]# useradd mysql -s /sbin/nologin
[root@LAMP-Server ~]# cd /usr/local/src/mysql-5.7.25/
[root@LAMP-Server mysql-5.7.25]# 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 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=/usr/local/src/mysql-5.7.25/boost/boost_1_59_0 \
-DWITH_SYSTEMD=1 && \
make && \
make install
  • 如果在cmake的过程中有报错,当报错解决后,需要把源码目录中的CMakeCache.txt文件删除,然后再重新cmake,否则依旧报错

4.1、修改mysql配置文件

"因为使用的是mysql普通用户,所以需要修改mysql目录的用户权限"
[root@LAMP-Server mysql-5.7.25]# chown -R mysql.mysql /usr/local/mysql/
"修改mysql配置文件"
[root@LAMP-Server ~]# cp /etc/my.cnf{,.bak}
[root@LAMP-Server ~]# vim /etc/my.cnf
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
port=3306
character_set_server=utf8
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mysql according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mysql/mysql.log
pid-file=/var/run/mysql/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

4.2、mysql数据库初始化

"添加mysql命令到PATH环境变量"
[root@LAMP-Server ~]# echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@LAMP-Server ~]# source /etc/profile
"数据库初始化"
[root@LAMP-Server ~]# mysqld --initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
"数据库初始化后,密码为空"
2021-01-22T17:15:19.994597Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

4.3、启动mysql数据库

[root@LAMP-Server ~]# cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system
[root@LAMP-Server ~]# systemctl daemon-reload
[root@LAMP-Server ~]# systemctl enable mysqld --now

4.4、修改数据库密码

[root@LAMP-Server ~]# mysqladmin -u root -p password 'Test123.com'    # Test123.com是新密码
Enter password:                                                       # 这里直接回车即可,初始密码是空的
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@LAMP-Server ~]# mysql -u root -p
Enter password:                                                      # 这时候需要上面设置的密码Test123.com

5、编译部署php

"安装编译所需的依赖"
[root@LAMP-Server ~]# yum -y install epel-release.noarch
[root@LAMP-Server ~]# yum -y install libxml2 libxml2-devel gmp-devel expat-devel xmlrpc-c xmlrpc-c-devel libicu-devel libmcrypt-devel libmemcached-devel openssl-devel libjpeg-devel libpng libpng-devel freetype-devel 
[root@LAMP-Server ~]# useradd http -s /sbin/nologin
[root@LAMP-Server ~]# cd /usr/local/src/php-7.1.33/
[root@LAMP-Server php-7.1.33]# echo 'export PHP_OPENSSL_DIR=yes' >> /etc/profile
[root@LAMP-Server php-7.1.33]# source /etc/profile
[root@LAMP-Server php-7.1.33]# ./configure \
--prefix=/usr/local/php \
--exec-prefix=/usr/local/php \
--bindir=/usr/local/php/bin \
--sbindir=/usr/local/php/sbin \
--includedir=/usr/local/php/include \
--libdir=/usr/local/php/lib/php \
--mandir=/usr/local/php/php/man \
--with-config-file-path=/usr/local/php/etc \
--with-mysql-sock=/tmp/mysql.sock \
--with-mcrypt \
--with-mhash \
--with-openssl \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-gd \
--with-iconv \
--with-zlib \
--enable-zip \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache \
--enable-fpm \
--with-fpm-user=http \
--with-fpm-group=http \
--without-gdbm \
--enable-fast-install \
--disable-fileinfo \
--with-php-config=/usr/local/bin/php-config \
--with-pdo-mysql=mysqlnd && \
make && \
make install
[root@LAMP-Server php-7.1.33]# echo 'export PATH=//usr/local/php/bin:$PATH' >> /etc/profile
[root@LAMP-Server php-7.1.33]# source /etc/profile
[root@LAMP-Server php-7.1.33]# php -v
PHP 7.1.33 (cli) (built: Jan 23 2021 01:59:48) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

5.1、配置php.ini以及php-fpm

[root@LAMP-Server ~]# cp /usr/local/src/php-7.1.33/php.ini-production /etc/php.ini
[root@LAMP-Server ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@LAMP-Server ~]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@LAMP-Server ~]# cp /usr/local/src/php-7.1.33/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
[root@LAMP-Server ~]# systemctl enable php-fpm.service --now

6、编译部署Curl

"Curl当前版本"
[root@LAMP-Server ~]# curl -V
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.53.1 zlib/1.2.7 libidn/1.28 libssh2/1.8.0
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz unix-sockets
"备份当前版本的Curl"
[root@LAMP-Server ~]# mv /usr/bin/curl{,-7.29.0}
[root@LAMP-Server ~]# mv /usr/bin/curl-config{,-7.29.0}
[root@LAMP-Server ~]# cd /usr/local/src/curl-7.74.0/
./configure \
--prefix=/usr/local/curl-7.74.0  \
--without-nss \
--with-ssl=/usr/local/openssl-1.1.1i && \
make && \
make install
  • –without-nss是因为centos自带的curl支持的https是nss版本的,不是openssl的,我们这里要用openssl版本的
  • –with-ssl后面跟的openssl安装目录
  • 直接./configure,默认是不支持https协议的
"更新curl"
[root@LAMP-Server ~]# ln -s /usr/local/curl-7.74.0/bin/curl /usr/bin/curl
[root@LAMP-Server ~]# ln -s /usr/local/curl-7.74.0/bin/curl-config /usr/bin/curl-config
[root@LAMP-Server ~]# curl -V
curl 7.74.0 (x86_64-pc-linux-gnu) libcurl/7.74.0 OpenSSL/1.1.1i zlib/1.2.7
Release-Date: 2020-12-09
Protocols: dict file ftp ftps gopher http https imap imaps mqtt pop3 pop3s rtsp smb smbs smtp smtps telnet tftp
Features: alt-svc AsynchDNS HTTPS-proxy IPv6 Largefile libz NTLM NTLM_WB SSL TLS-SRP UnixSockets
  • 到此,一套完整的LAMP环境已经编译完成

猜你喜欢

转载自blog.csdn.net/u010383467/article/details/113014463