CentOS 7上源码编译安装和配置LNMP Web+phpMyAdmin服务器环境

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介:

什么是LNMP

 

LNMP(别名LEMP)是指由Linux, Nginx, MySQL/MariaDB, PHP/Perl/Python组合成的动态Web应用程序和服务器,它是一组Web应用程序的基础软件包,在这个基础环境上我们可以搭建任何使用PHP/Perl/Python等语言的动态网站,如商务网站、博客、论坛和开源Web应用程序软件等,它是互联网上被广泛使用的Web网站架构之一。

 

部署方式

 

从网站规模大小(访问流量、注册用户等)角度来看,LNMP架构可以使用单机部署方式和集群部署方式。单机部署方式即所有的软件都部署在一台Linux服务器上;集群部署方式可以将Nginx网络服务器,MySQL/MariaDB数据库,PHP软件分开安装到不同的服务器上,它们彼此之间通过TCP网络协议相互通信协助工作,以及对外提供Web应用访问服务。因此,当单台服务器不能满足性能要求时,可以使用集群方式部署。

 

本教程将指导您如何在RHEL 7/CentOS 7以及Fedora 23/24/25等衍生版本中使用Nginx 1.10, MariaDB 10PHP 6安装LNMP应用服务器环境。(在其他系统如SUSEUbuntu中源码编译安装LNMP同样可以参考本教程。)


前提要求

 

准备2RHEL 7CentOS 7服务器,一台用于安装MariaDB,另一台用于安装NginxPHP。当然你也可以仅仅使用1台服务器部署LNMP以通过实验来验证LNMP

HOSTNAME IP OS NODE

hming-server217-mdb

10.0.6.217 CentOS 7.2 x86_64 MariaDB
hming-server218-web 10.0.6.218 CentOS 7.2 x86_64 MariaDB,Nginx,PHP


主软件包(源码包)

 

DB      mariadb-10.1.20.tar.gz

PHP     php-5.6.30.tar.bz2

Nginx   nginx-1.10.2.tar.gz


注意:主软件包在这之前我已经提前下载好了,可能并不是最新和稳定版,你也可以根据你的需求选择其他版本。


依赖软件


源码编译安装LNMP有许多依赖软件(如gccmake)是必要的,一般可以通过YUM软件管理程序来安装,另外小部分需要源码编译安装。


通过yum安装gccgcc-c++make等必需软件包

1
# yum install -y gcc gcc-c++ make automake


安装MariaDB


1. 使用yum安装gccmakebisonncurses-devel等依赖软件包

1
# yum install zlib-devel ncurses ncurses-devel bison


2. 安装cmakecmake是编译MySQL/MariaDB的必要工具(MySQL5.6/MariaDB 10及以后的版本你可以在网站https://cmake.org/中下载cmake软件包

 

将软件包上传到/usr/local/src目录下,然后解压cmake包,cdcmake目录执行./bootstrap脚本进行安装,同时可以使用--prefix=<install dir>选项指定安装的目录。

1
2
3
4
# tar -zxf cmake-3.7.2.tar.gz
# cd cmake-3.7.2/
# ./bootstrap --prefix=/usr/local/cmake
# make && make install


当安装完之后,你可以使用执行/usr/local/cmake/bin/cmake --version查看安装的cmake版本

1
2
3
4
[root@hming-server217-mdb ~ ] # /usr/local/cmake/bin/cmake --version
cmake version 3.7.2
  
CMake suite maintained and supported by Kitware (kitware.com /cmake ).


3. 安装完cmake编译工具之后,接下来我们安装MariaDB(你可以访问https://mariadb.org/下载MariaDB), 首先创建MariaDB安装目录/usr/local/mysql和数据库服务器的数据目录/data/mysql/webdata,并创建MySQL用户,并将/data/mysql/webdata目录的所属主权限修改为mysql用户


创建安装目录和数据目录

1
2
# mkdir /usr/local/mysql
# mkdir /data/mysql/webdata -p


创建mysql用户,并修改数据目录的权限

1
2
3
# groupadd mysql
# useradd -r -g mysql -s /sbin/nologin mysql -M
# chown -R mysql:mysql /data/mysql/


4. 编译安装MariaDB,将MariaDB软件包解压后使用cmake进行编译,根据需求选择相应的编译参数进行编译(查看编译选项/usr/local/cmake/bin/cmake . -LH).  (或者访问MariaDB编译安装帮助文档https://mariadb.com/kb/en/mariadb/compiling-mariadb-from-source/)

1
2
3
4
[root@hming-server217-mdb  /usr/local/src  ] # tar -zxf mariadb-10.1.20.tar.gz
[root@hming-server217-mdb  /usr/local/src  ] # cd mariadb-10.1.20/
  
[root@hming-server217-mdb  /usr/local/src/mariadb-10 .1.20 ] # /usr/local/cmake/bin/cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/webdata -DSYSCONFDIR=/etc -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DEXTRA_CHARSETS=all -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_UNIX_ADDR=/data/mysql/webdata/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_EMBEDDED_SERVER=1 -DWITH_READLINE=1 -DWITH_DEBUG=0


执行gmake  gmake install进行最后的安装

1
2
[root@hming-server217-mdb  /usr/local/src/mariadb-10 .1.20 ] # gmake
[root@hming-server217-mdb  /usr/local/src/mariadb-10 .1.20 ] # gmake install


5. 配置mysql环境变量,如果不配置默认系统找不到mysql的可执行程序和命令,否则你必须使用全路径执行与mysql相关的任何命令,查看系统默认的环境变量


使用export命令添加mysql环境变量

1
2
3
[root@hming-server217-mdb ~ ] # export PATH=$PATH:/usr/local/mysql/bin
[root@hming-server217-mdb ~ ] # echo $PATH
/usr/local/sbin : /usr/local/bin : /usr/sbin : /usr/bin : /root/bin : /usr/local/mysql/bin


最后将mysql环境配置添加到/etc/profile或者/etc/profile.d/的自定义文件中

1
2
[root@hming-server217-mdb ~ ] # echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
[root@hming-server217-mdb ~ ] # source /etc/profile.d/mysql.sh


配置了环境变量,我们就可以直接在shell终端下执行mysql命令,使用mysql -V查看mysql版本

1
2
[root@hming-server217-mdb ~ ] # mysql -V
mysql  Ver 15.1 Distrib 10.1.20-MariaDB,  for  Linux (x86_64) using readline 5.1


6. 添加MariaDB库文件

1
[root@hming-server217-mdb ~ ] # echo "/usr/local/mysql/lib" >> /etc/ld.so.conf.d/mariadb-x86_64.conf


7. 拷贝MariaDB服务启动脚本到/etc/init.d/目录中

1
2
3
4
5
[root@hming-server217-mdb ~ ] # cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@hming-server217-mdb ~ ] # chmod 755 /etc/init.d/mysqld
[root@hming-server217-mdb ~ ] # chkconfig --add mysqld
[root@hming-server217-mdb ~ ] # chkconfig mysqld on
[root@hming-server217-mdb ~ ] # systemctl daemon-reload


8. 执行初始化数据库脚本,安装MariaDB基础数据库(执行./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/webdata --user=mysql --no-defaults)

1
2
3
4
5
[root@hming-server217-mdb ~ ] # cd /usr/local/mysql/scripts/
[root@hming-server217-mdb  /usr/local/mysql/scripts  ] # ./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/webdata --user=mysql --no-defaults
[root@hming-server217-mdb  /usr/local/mysql/scripts  ] # ls /data/mysql/webdata/
aria_log.00000001  ibdata1      ib_logfile1  performance_schema
aria_log_control   ib_logfile0  mysql         test


9. 配置/etc/my.cnf,该文件是mysql服务的主要配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
[root@hming-server217-mdb ~ ] # cp /etc/my.cnf /etc/my.cnf.save
[root@hming-server217-mdb ~ ] # cat /etc/my.cnf
[client]
port=3306
socket= /data/mysql/webdata/mysql .sock
default-character- set =utf8
  
[mysqld]
port=3306
user=mysql
basedir= /usr/local/mysql
datadir= /data/mysql/webdata
socket= /data/mysql/webdata/mysql .sock
character- set -server=utf8
external-locking=FALSE
skip-name-resolv
default-storage-engine=InnoDB
back_log=1024
transaction_isolation=REPEATABLE-READ
max_connections=5000
max_connect_errors=6000
open_files_limit=65535
table_open_cache=512
  
[mysqldump]
quick
max_allowed_packet=32M
  
[mysql]
no-auto-rehash
default-character- set =utf8
  
[mysqld_safe]
open -files-limit=8192
log-error= /var/log/mariadb/mariadb .log
pid- file = /var/run/mariadb/mariadb .pid


创建/var/log/mariadb/var/run/mariadb目录

1
2
3
4
[root@hming-server217-mdb ~ ] # mkdir /var/log/mariadb
[root@hming-server217-mdb ~ ] # mkdir /var/run/mariadb
[root@hming-server217-mdb ~ ] # chown mysql:mysql /var/log/mariadb
[root@hming-server217-mdb ~ ] # chown mysql:mysql /var/run/mariadb


10. 启动MariaDB服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@hming-server217-mdb ~ ] # systemctl start mysqld
[root@hming-server217-mdb ~ ] # systemctl status mysqld
 
[root@hming-server217-mdb ~ ] # systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
    Loaded: loaded ( /etc/rc .d /init .d /mysqld )
    Active: active (running) since Fri 2017-06-02 23:53:09 CST; 3 days ago
      Docs:  man :systemd-sysv-generator(8)
   Process: 27419 ExecStop= /etc/rc .d /init .d /mysqld  stop (code=exited, status=0 /SUCCESS )
   Process: 27449 ExecStart= /etc/rc .d /init .d /mysqld  start (code=exited, status=0 /SUCCESS )
    CGroup:  /system .slice /mysqld .service
            ├─27460  /bin/sh  /usr/local/mysql/bin/mysqld_safe  --datadir= /data/mysql/webdata  --pid- file = /data/mysq ...
            └─27595  /usr/local/mysql/bin/mysqld  --basedir= /usr/local/mysql  --datadir= /data/mysql/webdata  --plugi...
 
Jun 02 23:53:08 hming-server217-mdb systemd[1]: Starting LSB: start and stop MySQL...
Jun 02 23:53:08 hming-server217-mdb mysqld[27449]: Starting MySQL.170602 23:53:08 mysqld_safe Logging to  '/...og' .
Jun 02 23:53:09 hming-server217-mdb mysqld[27449]: SUCCESS!
Jun 02 23:53:09 hming-server217-mdb systemd[1]: Started LSB: start and stop MySQL.
Hint: Some lines were ellipsized, use -l to show  in  full.


查看MariaDB服务进程

1
2
3
4
[root@hming-server217-mdb ~ ] # ps aux | grep mysql
root     27460  0.0  0.0 115380  1744 ?        S    23:53   0:00  /bin/sh  /usr/local/mysql/bin/mysqld_safe  --datadir= /data/mysql/webdata  --pid- file = /data/mysql/webdata/hming-server217-mdb .pid
mysql    27595  1.0  5.2 1209380 99428 ?       Sl   23:53   0:00  /usr/local/mysql/bin/mysqld  --basedir= /usr/local/mysql  --datadir= /data/mysql/webdata  --plugin- dir = /usr/local/mysql/lib/plugin  --user=mysql --log-error= /var/log/mariadb/mariadb .log -- open -files-limit=8192 --pid- file = /data/mysql/webdata/hming-server217-mdb .pid --socket= /data/mysql/webdata/mysql .sock --port=3306
root     27627  0.0  0.0 112644   952 pts /2     S+   23:53   0:00  grep  --color=auto mysql


11. 执行mysql_secure_installation初始化数据库,设mysql用户root密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
[root@hming-server217-mdb ~ ] # mysql_secure_installation
  
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
       SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
  
In order to log into MariaDB to secure it, we'll need the current
password  for  the root user.  If you've just installed MariaDB, and
you haven't  set  the root password yet, the password will be blank,
so you should just press enter here.
  
Enter current password  for  root (enter  for  none):
OK, successfully used password, moving on...
  
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
  
Set root password? [Y /n ] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
  ... Success!
  
  
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created  for
them.  This is intended only  for  testing, and to  make  the installation
go a bit smoother.  You should remove them before moving into a
production environment.
  
Remove anonymous  users ? [Y /n ] y
  ... Success!
  
Normally, root should only be allowed to connect from  'localhost' .  This
ensures that someone cannot guess at the root password from the network.
  
Disallow root login remotely? [Y /n ] y
  ... Success!
  
By default, MariaDB comes with a database named  'test'  that anyone can
access.  This is also intended only  for  testing, and should be removed
before moving into a production environment.
  
Remove  test  database and access to it? [Y /n ] y
  - Dropping  test  database...
  ... Success!
  - Removing privileges on  test  database...
  ... Success!
  
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
  
Reload privilege tables now? [Y /n ] y
  ... Success!
  
Cleaning up...
  
All  done !  If you've completed all of the above steps, your MariaDB
installation should now be secure.
  
Thanks  for  using MariaDB!


12登录MariaDB数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@hming-server217-mdb ~ ] # mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection  id  is 353
Server version: 10.1.20-MariaDB Source distribution
  
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
  
Type  'help;'  or  '\h'  for  help. Type  '\c'  to  clear  the current input statement.
  
MariaDB [(none)]>  select  version();
+-----------------+
| version()       |
+-----------------+
| 10.1.20-MariaDB |
+-----------------+
1 row  in  set  (0.00 sec)
  
MariaDB [(none)]>


安装Nginx


1. 安装依赖软件包

1
[root@hming-server218-web ~ ] # yum install gcc gcc-c++ zlib zlib-devel pcre pcre-devel openssl-devel gd gd-devel perl-devel perl-ExtUtils-Embed


2. 创建nginx用户

1
2
[root@hming-server218-web ~ ] # groupadd nginx
[root@hming-server218-web ~ ] # useradd -g nginx -s /sbin/nologin nginx -M


3. 编译安装Nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[root@hming-server218-web  /usr/local/src  ] # tar -zxf nginx-1.10.2.tar.gz
[root@hming-server218-web  /usr/local/src  ] # cd nginx-1.10.2/
[root@hming-server218-web  /usr/local/src/nginx-1 .10.2 ] # ./configure \
--user=nginx \
--group=nginx \
--prefix= /usr/local/nginx  \
--pid-path= /var/run/nginx/nginx .pid \
--lock-path= /var/lock/subsys/nginx  \
--with-http_stub_status_module \
--with-pcre \
--with-http_ssl_module \
--with-mail_ssl_module \
--with-http_gzip_static_module \
--http-log-path= /var/log/nginx/access .log \
--error-log-path= /var/log/nginx/error .log \
--http-client-body-temp-path= /tmp/nginx/client_body  \
--http-proxy-temp-path= /tmp/nginx/proxy  \
--http-fastcgi-temp-path= /tmp/nginx/fastcgi  \
--http-uwsgi-temp-path= /tmp/nginx/uwsgi  \
--with-http_degradation_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_perl_module \
--with-http_image_filter_module=dynamic \
--with-http_flv_module
 
[root@hming-server218-web  /usr/local/src/nginx-1 .10.2 ] # make && make install


4. 启动Nginx服务

1
2
3
4
5
6
7
[root@hming-server218-web ~ ] # ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@hming-server218-web ~ ] # mkdir /tmp/nginx
[root@hming-server218-web ~ ] # nginx
[root@hming-server218-web ~ ] # ps -ef | grep nginx
root     27913     1  0 00:49 ?        00:00:00 nginx: master process nginx
nginx    27914 27913  0 00:49 ?        00:00:00 nginx: worker process
root     27923 22381  0 00:49 pts /1     00:00:00  grep  --color=auto nginx

5. 添加防火墙

1
2
3
4
[root@hming-server218-web ~ ] # firewall-cmd --permanent --add-port=80/tcp
success
[root@hming-server218-web ~ ] # firewall-cmd --reload
success


6. 通过客户端访问http://10.0.6.218

wKiom1k1KC6TnB5QAAAswLN_yC0724.png

本文转自 HMLinux 51CTO博客,原文链接:

http://blog.51cto.com/7424593/1932481


相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
HAProxy的高级配置选项-配置haproxy支持https协议及服务器动态上下线
文章介绍了如何配置HAProxy以支持HTTPS协议和实现服务器的动态上下线。
80 8
HAProxy的高级配置选项-配置haproxy支持https协议及服务器动态上下线
|
1月前
|
监控 Apache
HAProxy的高级配置选项-Web服务器状态监测
这篇文章介绍了HAProxy的高级配置选项,特别是如何进行Web服务器状态监测,包括基于四层传输端口监测、基于指定URI监测和基于指定URI的request请求头部内容监测三种方式,并通过实战案例展示了配置过程和效果。
49 8
HAProxy的高级配置选项-Web服务器状态监测
|
1月前
|
Linux
CentOS 7.x时间同步服务chrony配置详解
文章详细介绍了在CentOS 7.x系统中如何安装和配置chrony服务,以及它与ntpd服务的对比,强调了chrony在时间同步方面的高效性和准确性。
52 1
CentOS 7.x时间同步服务chrony配置详解
|
16天前
|
Ubuntu Linux
Linux服务器的自动启动可以在哪里进行配置?
Linux服务器的自动启动可以在哪里进行配置?
72 3
|
27天前
|
监控 应用服务中间件
Nagios 服务器 Nrpe 配置
Nagios服务器需安装NRPE并定义监控命令于`command.cfg`中。示例配置如下:`check_nrpe -H $HOSTADDRESS$ -c $ARG1$`。客户端配置文件如`192.168.149.128.cfg`可引用NRPE配置的命令,如`check_nrpe!check_load`以监控负载。监控HTTP关键词使用`check_http`命令加参数,如`-I`指定IP,`-u`指定URL,`-s`指定关键词,可在`command.cfg`中定义如`check_http_word`命令,并在主机配置文件中引用。
44 13
|
1月前
|
编解码 小程序
无影云电脑产品使用黑神话悟空之:游戏服务器更新/配置问题
该文档主要介绍了使用无影云电脑玩《黑神话:悟空》时可能遇到的问题及解决方法,包括游戏服务器更新、配置问题、画质建议及如何开启帧数显示等内容,并提供了详细的步骤指导与参考链接。
|
1月前
|
监控 安全 网络协议
快速配置Linux云服务器
快速配置Linux云服务器
|
关系型数据库 MySQL 应用服务中间件
手动部署LNMP环境(Alibaba Cloud Linux 2)
本场景带您体验如何在Alibaba Cloud Linux 2.1903 LTS 64位操作系统的云服务器上搭建LNMP环境。
271 0
|
5月前
|
关系型数据库 应用服务中间件 nginx
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建
|
关系型数据库 MySQL 应用服务中间件
快速搭建LNMP环境
Nginx是一款小巧而高效的Web服务器软件,可帮您在Linux系统下快速方便地搭建出LNMP Web服务环境。本教程介绍如何搭建LNMP环境,其中LNMP分别代表Linux、Nginx、MySQL和PHP。
385 2
下一篇
无影云桌面