lnmp框架搭建---源码编译

简介

LNMP指的是一个基于CentOS/Debian编写的Nginx、PHP、MySQL、phpMyAdmin、eAccelerator一键安装包。可以在VPS、独立主机上轻松的安装LNMP生产环境。
Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Mysql是一个小型关系型数据库管理系统。 PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。
这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统

说明:

和LAMP不同的是,提供web服务的是Nginx
并且php是作为一个独立服务存在的,这个服务叫做php-fpm
Nginx直接处理静态请求,动态请求会转发给php-fpm

优点:

作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。 作为负载均衡服务器:Nginx
既可以在内部直接支持Rails和PHP,也可以支持作为 HTTP代理服务器 对外进行服务。Nginx
用C编写,不论是系统资源开销还是CPU使用效率都比Perlbal要好的多。
作为邮件代理服务器:Nginx同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一 也是作为邮件代理服务器),Last/fm
描述了成功并且美妙的使用经验。 Nginx 安装非常的简单,配置文件非常简洁(还能够支持perl语法)。Nginx支持平滑加载新的配 置,
还能够在不间断服务的情况下进行软件版本的升级

选择源码编译而不是在网上直接下载模版的主要目的:
可以设置自己需要的内容


MYSQL源码编译

(一)编译源码
1.下载mysql数据库的源码包并进行解压

[root@server3 ~]# ls
mysql-boost-5.7.17.tar.gz
[root@server3 ~]# tar zxf mysql-boost-5.7.17.tar.gz 

2.下载安装源码编译工具cmake(支持编译C++语言)
cmake跨平台工具是用来预编译mysql源码的,用于设置mysql的编译参数。如:安装目录、数据存放目录、字符编码、排序规则等

[root@server3 ~]# ls
cmake-2.8.12.2-4.el6.x86_64.rpm  mysql-5.7.17  mysql-boost-5.7.17.tar.gz
[root@server3 ~]# yum install cmake-2.8.12.2-4.el6.x86_64.rpm -y         #安装MySQL 5.6以后My SQL要用cmake编译

3.创建预编译环境,可以定制即加上自己所需的安装条件(比如安装位置等等)

[root@server3 ~]# cd mysql-5.7.17/    #进入程序安装目录
[root@server3 mysql-5.7.17]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql \      #安装目录
>  -DMYSQL_DATADIR=/usr/local//lnmp/mysql/data \                     #数据库存放目录
>  -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock \         #Unix socket 文件路径
> -DWITH_MYISAM_STORAGE_ENGINE=1 \                                   #安装 myisam 存储引擎
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \                                 #安装 innodb 存储引擎
> -DDEFAULT_CHARSET=utf8 \                                           #使用 utf8 字符
> -DDEFAULT_COLLATION=utf8_general_ci \                              #校验字符
> -DEXTRA_CHARSETS=all                                               #安装所有扩展字符集
> -DWITH_BOOST=boost/boost_1_59_0/

    # 生成makefile文件
    # 每次编译完之后根据报错信息安装需要的软件,安装完后删除CMakeCache.txt 缓存文件,重新编译,直到没有报错
[root@server3 mysql-5.7.17]# make     #根据makefile文件进行安装
[root@server3 mysql-5.7.17]# make install

3.在初次编译后,我们会发现有许多依赖性未安装,一个一个解决依赖项,预编译环境配置完成

[root@server3 mysql-5.7.17]# yum install gcc gcc-c++ -y
[root@server3 mysql-5.7.17]# yum install ncurses-devel -y
[root@server3 mysql-5.7.17]# yum install bison -y  
[root@server3 mysql-5.7.17]# rm -fr CMakeCache.txt # 一次编译完成后,会提示我们删除所在目录下的CMakeCache.txt文件,才能清除上次编译的缓存,从而开始新的编译
[root@server3 mysql-5.7.17]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql -DMYSQL_DATADIR=/usr/local/lnmp/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=boost/boost_1_59_0/

4.编译汇编(大概需要一个半小时,请耐心等待)

[root@server3 mysql-5.7.17]# make
[root@server3 mysql-5.7.17]# make install

(二)配置MYSQL
1.进入源码编译目录,再进入mysql安装的位置,将编译完成的默认配置文件放在/etc下

[root@server3 mysql-5.7.17]# cd /usr/local/lnmp/mysql
[root@server3 mysql]# cd support-files/
[root@server3 support-files]# cp my-default.cnf /etc/my.cnf 

2.配置mysql服务

[root@server3 support-files]# vim /etc/my.cnf

basedir = /usr/local/lnmp/mysql                 #mysql的安装目录
datadir = /usr/local/lnmp/mysql/data            #mysql数据存放目录
port = 3306                                     #mysql服务对外端口
#server_id = .....
socket = /usr/local/lnmp/mysql/data/mysql.sock  #mysql与外界联系的套接字文件位置

这里写图片描述
3.将编译完成的mysql启动脚本复制到系统启动服务的默认目录中

[root@server3 support-files]# file mysql.server
mysql.server: POSIX shell script text executable
[root@server3 support-files]# cp mysql.server /etc/init.d/mysqld

4.对mysql服务进行限制(利用mysql用户),将mysql服务的工作空间局限在mysql用户的家目录中,创建mysql组和mysql用户

[root@server3 mysql]# groupadd -g 27 mysql
[root@server3 mysql]# useradd -u 27 -g 27 -M -d /usr/local/lnmp/mysql/data -s /sbin/nologin mysql
  # 指定mysql用户的家目录为/usr/local/lnmp/mysql/data
  #   (-M表示不创建其家目录)。为了系统安全,MYSQL默认使用mysql用户来运行相关程序。

这里写图片描述
5.将mysql目录下的data目录的所属组修改为mysql,因为mysql服务将要往这个目录中写数据,保证其有执行权限

[root@server3 mysql]# chown mysql data/ -R

6.将编译生成的mysql二进制命令放进系统的环境变量文件中,保证可以直接调用该二进制命令

[root@server3 mysql]# cd bin/           #mysql二进制命令存在目录/usr/local/lnmp/mysql/bin
[root@server3 bin]# ls
innochecksum                mysql_config               mysqlshow
lz4_decompress              mysql_config_editor        mysqlslap
myisamchk                   mysqld                     mysql_ssl_rsa_setup
myisam_ftdump               mysqld_multi               mysqltest
myisamlog                   mysqld_safe                mysqltest_embedded
myisampack                  mysqldump                  mysql_tzinfo_to_sql
my_print_defaults           mysqldumpslow              mysql_upgrade
mysql                       mysql_embedded             mysqlxtest
mysqladmin                  mysqlimport                perror
mysqlbinlog                 mysql_install_db           replace
mysqlcheck                  mysql_plugin               resolveip
mysql_client_test           mysqlpump                  resolve_stack_dump
mysql_client_test_embedded  mysql_secure_installation  zlib_decompress
[root@server3 bin]# cd
[root@server3 ~]# vim .bash_profile                 #系统环境变量文件

PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin

[root@server3 ~]# source .bash_profile 

这里写图片描述
7.执行mysql的配置初始化文件

[root@server3 mysql]# mysql --initialize --user=mysql       #初始化

这里写图片描述
(三).mysql的安全初始化
1.打开mysql服务

[root@server3 mysql]# /etc/init.d/mysqld start 
Starting MySQL.Logging to '/usr/local/lnmp/mysql/data/server3.err'.
. SUCCESS!

2.执行安全初始化脚本mysql_secure_installation

[root@server3 mysql]# mysql_secure_installation    #安全初始化脚本1)在第一次交互请求后面输入刚刚初始化时复制的密码
(2)然后输入两次新密码
(3)接下来的交互式请求可以直接回车,这条交互式请求是询问你是否要检查密码强度,如果输入y,
就要重新修改密码,密码长度必须超过8位,必须有大小写字母,所以我们直接回车
(4)接下来会询问是否要修改root密码,直接回车,因为上面已经设置过了
(5)接下来全部输入y回车就行

这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

这里写图片描述
3.登陆mysql数据库测试

[root@server3 mysql]# mysql -p
Enter password:                #输入密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, 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> show databases;        #显示数据库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

LNMP架构中的MYSQL数据库源码编译完成!!!

二.PHP源码编译

1.下载PHP的源码包并解压

[root@server3 ~]# tar jxf php-5.6.35.tar.bz2

2.进入解压目录创造预编译环境

[root@server3 ~]# cd php-5.6.35
[root@server3 php-5.6.35]# ./configure --prefix=/usr/local/lnmp/php    # php安装的位置
--with-config-file-path=/usr/local/php/etc              # php配置文件所在的位置
--with-mysql=mysqlnd 
--enable-mysqlnd 
--with-mysqli=mysqlnd 
--with-pdo-mysql=mysqlnd    # 支持与mysql数据库之间建立联系
--with-openssl              # 支持openssl加密
--with-snmp 
--with-gd 
--with-zlib 
--with-curl 
--with-libxml-dir           #支持 libxml 函数
--with-png-dir              # php处理图片的格式
--with-jpeg-dir 
--with-freetype-dir 
--with-pear 
--with-gettext              # 文本
--with-gmp 
--enable-inline-optimization 
--enable-soap 
--enable-ftp 
--enable-sockets 
--enable-mbstring 
--enable-fpm                # 开启fpm模式(nginx等服务用的)
--with-fpm-user=nginx       # 对PHP服务的限制通过nginx用户来实现
--with-fpm-group=nginx      # PHP以nginx用户组身份运行
--with-mcrypt               # 提供了对多种块算法的支持 主要用来实现加密等算法
--with-mhash                # 加密算法

3.解决依赖性

[root@server3 php-5.6.35]# yum install libxml2-devel -y
[root@server3 php-5.6.35]# yum install openssl-devel -y
[root@server3 php-5.6.35]# yum install libcurl-devel -y
[root@server3 ~]# ls
gd-devel-2.0.35-11.el6.x86_64.rpm           #需要在网上下载
[root@server3 ~]# yum install gd-devel-2.0.35-11.el6.x86_64.rpm -y
[root@server3 php-5.6.35]# yum install gmp-devel -y
[root@server3 ~]# ls                       #这两个需要在网上下载
libmcrypt-2.5.8-9.el6.x86_64.rpm
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
[root@server3 ~]# yum install libmcrypt-devel-2.5.8-9.el6.x86_64.rpm libmcrypt-2.5.8-9.el6.x86_64.rpm -y
[root@server3 php-5.6.35]# yum install net-snmp-devel -y
[root@server3 ~]# ls
re2c-0.13.5-1.el6.x86_64.rpm
[root@server3 ~]# rpm -ivh re2c-0.13.5-1.el6.x86_64.rpm 
warning: re2c-0.13.5-1.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing...                                                       ########################################### [100%]
   1:re2c                                                          ########################################### [100%]

再执行一次,不报错方可编译

[root@server3 php-5.6.35]# ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/php/etc --with-mysql=mysqlnd --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-pear --with-gettext --with-gmp --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

4.编译和安装(需要一个小时,请耐心等待)

[root@server3 php-5.6.35]# make
[root@server3 php-5.6.35]# make install

5.修改时区

[root@server3 php-5.6.35]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini
[root@server3 php-5.6.35]# cd /usr/local/lnmp/php/etc/
[root@server3 etc]# vim php.ini

date.timezone = Asia/Shanghai               #修改时区为上海

6.打开pid

[root@server3 php-5.6.35]# cd /usr/local/lnmp/php/etc/
[root@server3 etc]# ls
pear.conf  php-fpm.conf.default
[root@server3 etc]# cp php-fpm.conf.default php-fpm.conf     # 修改文件名
[root@server3 etc]# ls
pear.conf  php-fpm.conf  php-fpm.conf.default  php.ini
[root@server3 etc]# vim php-fpm.conf

pid = run/php-fpm.pid                #打开pid

7.创建nginx用户

[root@server3 etc]# useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin nginx

8.将php启动的脚本复制到系统启动脚本的默认目录下,并测试php是否可以启动

[root@server3 ~]# cd php-5.6.35/sapi/fpm/
[root@server3 fpm]# file init.d.php-fpm                    #查看文件类型
init.d.php-fpm: POSIX shell script text executable         
[root@server3 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm  #init.d.php-fpm是编译完成的启动脚本
[root@server3 fpm]# chmod +x /etc/init.d/php-fpm           #可执行权限
[root@server3 fpm]# /etc/init.d/php-fpm start              #开启php服务
Starting php-fpm  done
[root@server3 fpm]# netstat -antlp                         #查看php服务的监听端口
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      921/sshd            
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      998/master          
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      22535/php-fpm 
# php服务的监听端口未9000      
tcp        0      0 172.25.8.3:22               172.25.8.250:54144          ESTABLISHED 22326/sshd          
tcp        0      0 :::22                       :::*                        LISTEN      921/sshd            
tcp        0      0 ::1:25                      :::*                        LISTEN      998/master          
tcp        0      0 :::3306                     :::*                        LISTEN      22616/mysqld        

三.nginx的源码编译

1.下载nginx的源码包并解压

[root@server3 ~]# ls
nginx-1.10.1.tar.gz
nginx-sticky-module-ng.tar.gz
[root@server3 ~]# tar zxf nginx-1.10.1.tar.gz 
[root@server3 ~]# tar zxf nginx-sticky-module-ng.tar.gz

2.去掉nginx的版本号

[root@server3 ~]# cd nginx-1.10.1
[root@server3 nginx-1.10.1]# vim src/core/nginx.h 


#define NGINX_VER          "nginx"

3.注释debug

[root@server3 nginx-1.10.1]# vim auto/cc/gcc 

# debug
#CFLAGS="$CFLAGS -g"

4.创造预编译环境

[root@server3 nginx-1.10.1]# ./configure --prefix=/usr/local/lnmp/nginx # nginx的安装路径
--with-http_ssl_module                    # 支持https服务
--with-http_stub_status_module 
--user=nginx 
--group=nginx                             # 运行在nginx用户和nginx组
--user=nginx --group=nginx 
--with-threads                            # 支持线程并发
--with-file-aio

5.解决源码编译中的依赖性问题

 [root@server3 nginx-1.10.1]# yum install pcre-devel -y

再执行一次,预编译无错后开始编译

[root@server3 nginx-1.10.1]# ./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --user=nginx --group=nginx --with-threads  --with-file-aio

6.编译安装

[root@server3 nginx-1.10.1]# make && make install

7.配置nginx服务

[root@server3 nginx-1.10.1]# vim /usr/local/lnmp/nginx/conf/nginx.conf


    worker_connections  65535;                     # 最大文件数



        location ~ \.php$ {                        # 将nginx与php结合起来
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #注释,因为该行本就包含在fastcgi.conf文件中
            include        fastcgi.conf;
        }

8.对nginx用户做限制

[root@server3 nginx-1.10.1]# vim /etc/security/limits.conf 
nginx            -       nofile           65536

9.将编译生成的mysql二进制命令放进系统的环境变量文件中,保证可以直接调用该二进制命令

[root@server3 ~]# vim .bash_profile 

PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin:/usr/local/lnmp/nginx/sbin
[root@server3 ~]# source .bash_profile 

10.编写nginx启动文件,检测nginx服务是否可用并打开nginx服务

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() {
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload(){
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart(){
stop
start
}
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|
restart|configtest}"
RETVAL=1
esac
exit $RETVAL
[root@server3 ~]# nginx -t                 # 检测配置文件是否有错
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
[root@server3 ~]# nginx                    # 开启nginx服务
[root@server3 ~]# netstat -antlp           # 查看端口
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      26262/nginx  
# nginx服务端口:80       
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      921/sshd            
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      998/master          
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      22535/php-fpm       
tcp        0      0 172.25.8.3:22               172.25.8.250:54144          ESTABLISHED 22326/sshd          
tcp        0      0 :::22                       :::*                        LISTEN      921/sshd            
tcp        0      0 ::1:25                      :::*                        LISTEN      998/master          
tcp        0      0 :::3306                     :::*                        LISTEN      22616/mysqld        

11.访问测试

[root@server3 ~]# vim /usr/local/lnmp/nginx/html/index.php

<?php
phpinfo()
?>

在浏览器处输入nginx和php所在主机ip/地址:172.25.8.5/index.php

12.修改php的默认访问(将php后缀文件作为默认发布文件)

[root@server3 ~]# vim /usr/local/lnmp/nginx/conf/nginx.conf

            index index.php index.html index.htm;

[root@server3 ~]# nginx -s reload

猜你喜欢

转载自blog.csdn.net/wwy0324/article/details/81417352
今日推荐