搭建LNMP环境

1.解决LNMP环境依赖

yum -y install make gcc gcc-c++ flex bison file libtool libtool-libs autoconf automake kernel-devel libjpeg libjpeg-devel libpng libpng-devel gd freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel libevent ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel gettext gettext-devel ncurses-devel gmp-devel unzip libcap lsof pcre*

2.安装Nginx
Nginx官网:www.nginx.org
下载Nginx程序包

wget http://nginx.org/download/nginx-1.14.2.tar.gz

安装Nginx需要pcre程序,需要下载pcre程序包:
官网:https://sourceforge.net/projects/pcre/files/pcre/

wget https://sourceforge.net/projects/pcre/files/pcre/8.41/pcre-8.41.tar.gz/download

3.安装epel扩展yum源

rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum clean all && yum makecache

4.创建Nginx运行用户

useradd -M -s /sbin/nologin nginx

5.上传pcre、nginx源码包到linux,并解压

tar xf pcre-8.41.tar.gz -C /usr/local/src/

注:解压即可,不用安装,Nginx安装时指定pcre的解压路径即可

[root@localhost ~]# tar zxf nginx-1.14.2.tar.gz -C /usr/local/src/
[root@localhost ~]# cd /usr/local/src/nginx-1.14.2/
[root@localhost nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.41 --user=nginx --group=nginx

注:

--with-http_dav_module
#启用支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)默认关闭,需要编译开启
--with-http_stub_status_module
#启用支持(获取Nginx上次启动以来的工作状态)
--with-http_addition_module
#启用支持(作为一个输出过滤器,支持不完全缓冲,分部分相应请求)
--with-http_sub_module
#启用支持(允许一些其他文本替换Nginx相应中的一些文本)
--with-http_flv_module
#启用支持(提供支持flv视频文件支持)
--with-http_mp4_module
#启用支持(提供支持mp4视频文件支持,提供伪流媒体服务端支持)
--with-pcre=/usr/local/src/pcre-8.41
#需要注意,这里指的是源码,用#./configure --help |grep pcre查看帮助
[root@localhost nginx-1.14.2]# echo $?
0
[root@localhost nginx-1.14.2]# make -j 4 && make install
[root@localhost nginx-1.14.2]# ll /usr/local/nginx/
total 4
drwxr-xr-x 2 root root 4096 Oct 12 10:17 conf	#Nginx配置文件
drwxr-xr-x 2 root root   38 Oct 12 10:17 html	#网站根目录
drwxr-xr-x 2 root root    6 Oct 12 10:17 logs	#日志文件
drwxr-xr-x 2 root root   18 Oct 12 10:17 sbin	#Nginx启动脚本

6.配置Nginx支持php文件
备份:

[root@localhost ~]# cp /usr/local/nginx/conf/nginx.conf{,.bak}

修改:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
在#user nobody;下一行添加user nginx;

启用PHP支持,修改66-72行

 66         location ~ \.php$ {
 67             root           html;
 68             fastcgi_pass   127.0.0.1:9000;
 69             fastcgi_index  index.php; 70             fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
 71             include        fastcgi_params;
 72         }

7.启动Nginx服务

[root@localhost ~]# /usr/local/nginx/sbin/nginx

8.编辑配置环境变量,让系统读到Nginx路径

[root@localhost ~]# vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH

9.读取变量

[root@localhost ~]# chmod +x /etc/profile.d/nginx.sh 
[root@localhost ~]#source /etc/profile.d/nginx.sh

或者使用软连接也可以:

[root@localhost ~]# ln -s /usr/local/nginx/sbin/ /usr/local/sbin/

10.生成服务启动脚本

[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 2
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
        start)
        $PROG
        ;;
        stop)
        kill -3 $(cat $PIDF)
        ;;
        restart)
        $0 stop &> /dev/null
        if [ $? -ne 0 ] ; then continue ; fi
        $0 start
        ;;
        reload)
        kill -1 $(cat $PIDF)
        ;;
        *)
        echo "Userage: $0 { start | stop | restart | reload }"
        exit 1
esac
exit 0

11.配置服务开机自动启动

[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig nginx on

12.关闭iptables在浏览器访问可以看到Wecome to nginx页面。
13.Nginx维护命令

[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successfu
#检查配置文件是否有语法错误
[root@localhost ~]# nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
configure arguments: --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.41 --user=nginx --group=nginx
#查看Nginx版本和配置参数

注:重新编译时,一定要看以前的编译配置,只需在原有配置参数后添加新的参数即可

[root@localhost ~]# nginx -s reload	#重载Nginx配置文件

三、安装配置MySQL
1.MySQL从5.7版本之后,boost是必须的,建议把系统自带的boost库卸载,源码编译安装高版本。

yum -y remove boost-*
卸载系统自带的mysql
yum -y remove mysql mariadb-*

2.安装依赖包

[root@localhost ~]# yum install -y cmake make gcc gcc-c++ bison ncurses ncurses-devel

3.添加用户和组

[root@localhost ~]# groupadd mysql
[root@localhost ~]# useradd -M -s /sbin/nologin -r -g mysql mysql
注意:在生成环境中,安装数据库之前,需要规划好数据存储的目录
这个目录最好是一块单独的分区或者磁盘,做成raid或者LVM,方便日后磁盘的维护和扩容
对于读写比较频繁的业务,可以采用SSD等转速高的磁盘

mysql-5.7.19程序包下载地址

http://www.mysql.com/Downloads/MySQL-5.7/mysql-5.7.19.tar.gz

boost_1_59_0程序包下载地址

http://liquidtelecom.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

4.解压源码包

[root@localhost ~]# tar xf boost_1_59_0.tar.gz -C /usr/local/src/
[root@localhost ~]# tar xf mysql-5.7.19.tar.gz -C /usr/local/src/
[root@localhost ~]# cd /usr/local/src/mysql-5.7.19/

5.规划安装目录
安装目录:/var/lib/mysql
数据目录:/var/lib/mysql/data

[root@localhost mysql-5.7.19]# mkdir -p /var/lib/mysql/data
[root@localhost mysql-5.7.19]# chown -R mysql:mysql /var/lib/mysql

6.编译安装

cmake -DCMAKE_INSTALL_PREFIX=/var/lib/mysql \
 -DMYSQL_DATADIR=/var/lib/mysql/data \
 -DSYSCONFDIR=/etc \
 -DWITH_MYISAM_STORAGE_ENGINE=1 \
 -DWITH_INNOBASE_STORAGE_ENGINE=1 \
 -DWITH_MEMORY_STORAGE_ENGINE=1 \
 -DWITH_READLINE=1 \
 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
 -DMYSQL_TCP_PORT=3306 \
 -DENABLED_LOCAL_INFILE=1 \
 -DWITH_PARTITION_STORAGE_ENGINE=1 \
 -DEXTRA_CHARSETS=all \
 -DDEFAULT_CHARSET=utf8 \
 -DDEFAULT_COLLATION=utf8_general_ci \
 -DDOWNLOAD_BOOST=1 \
 -DWITH_BOOST=/usr/local/src/boost_1_59_0
参数注释:
DCMAKE_INSTALL_PREFIX:指定MySQL程序的安装目录,默认/usr/local/mysql
DEFAULT_CHARSET:指定服务器默认字符集,默认latin1
DEFAULT_COLLATION:指定服务器默认的校对规则,默认latin1_general_ci
ENABLED_LOCAL_INFILE:指定是否允许本地执行LOAD DATA INFILE,默认OFF
WITH_COMMENT:指定编译备注信息
WITH_xxx_STORAGE_ENGINE:指定静态编译到mysql的存储引擎,MyISAM,MERGE,MEMBER以及CSV四种引擎默认即被编译至服务器,不需要特别指定。
WITHOUT_xxx_STORAGE_ENGINE:指定不编译的存储引擎
SYSCONFDIR:初始化参数文件目录
MYSQL_DATADIR:数据文件目录
MYSQL_TCP_PORT:服务端口号,默认3306
MYSQL_UNIX_ADDR:socket文件路径,默认/tmp/mysql.sock

编译参数的帮助寻找方法(官方):

http://www.mysql.com → Documentation → MySQL Server MySQL栏的 Reference Manual (选择对应的版本5.7(MySQL 5.7 Reference Manual))→ Installation and Upgrades MySQL → Installing MySQL from Source → MySQL Source-Configuration Options。

最终的URL是

https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html 

7.编译

mysql-5.7.19编译时会占用大量的系统资源,建议使用多个核心同时进行编译,否则可能会编译失败
[root@localhost mysql-5.7.19]# make -j 4
如果编译出错时,在mysql的解压目录下执行命令:
[root@localhost mysql-5.7.19]# make clean

[root@localhost mysql-5.7.19]# rm CMakeCache.txt
再从cmake开始

8.安装

[root@localhost mysql-5.7.19]# make install

9.编辑配置文件

[root@localhost ~]# vim /etc/my.cnf 
[mysqld]
basedir=/var/lib/mysql
datadir=/var/lib/mysql/data
port=3306
socket=/var/lib/mysql/mysql.sock
character-set-server=utf8
log-error=/var/log/mysqld.log
pid-file=/tmp/mysqld.pid
[mysql]
socket=/var/lib/mysql/mysql.sock 
[client]
socket=/var/lib/mysql/mysql.sock

10.添加path路径,让系统能读到mysql的命令:

[root@localhost ~]# vim /etc/profile.d/mysql.sh 
export PATH=/var/lib/mysql/bin:$PATH

11.使修改生效

[root@localhost ~]# source /etc/profile.d/mysql.sh

12.生成服务启动脚本

[root@localhost ~]# cp /var/lib/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# chmod +x /etc/init.d/mysqld

13.初始化数据库

[root@localhost ~]# /var/lib/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/var/lib/mysql --datadir=/var/lib/mysql/data

14.启动服务

[root@localhost ~]# /etc/init.d/mysqld start
Starting MySQL... ERROR! The server quit without updating PID file (/tmp/mysqld.pid).

服务启动失败。
解决方法:
把数据库目录删除,重新初始化:

[root@localhost ~]# cd /var/lib/mysql/data/
[root@localhost data]# ls
auto.cnf  ib_buffer_pool  ibdata1  ib_logfile0  ib_logfile1
[root@localhost data]# rm -rf ./*
[root@localhost data]# /var/lib/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/var/lib/mysql --datadir=/var/lib/mysql/data
[root@localhost data]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 
[root@localhost data]# mysql
mysql> set password for root@localhost=password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
安全初始化:
[root@localhost data]# mysql_secure_installation
MySQL5.7 提供了三种密码策略,根据需要设置复杂度不同的密码
输入两次密码,然后一直Y下去

四、安装PHP
在Nginx中,我们使用的是php-fpm来对php页面解析,PHP-FPM其实是PHP源代码的一个补丁,指在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,再编译安装PHP后才可以使用。
从PHP5.3.3开始,PHP中直接整合了PHP-FPM,所以从PHP5.3.3版本以后,不需要下载PHP-FPM补丁包了。
1.安装依赖关系
依赖包下载地址

http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
http://iweb.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
http://iweb.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
  1. 解决依赖:yum安装解决
[root@localhost ~]# yum -y install php-mcrypt  libmcrypt  libmcrypt-devel php-pear libxml2 libxml2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel freetype-devel

官网地址:http://php.net/
php7.1.24程序包下载地址

http://jp2.php.net/distributions/php-7.1.24.tar.gz

3.解压PHP包并配置

[root@localhost ~]# tar zxf php-7.1.24.tar.gz -C /usr/local/src/; cd /usr/local/src/cd
[root@localhost php-7.1.24]#  ./configure 
--prefix=/usr/local/php	\ 
--with-config-file-path=/usr/local/php/ \
--enable-fpm  \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--disable-fileinfo \
--enable-maintainer-zts

参数选项可参考http://php.net/manual/zh/configure.about.php官方中文手册

–with-config-file-path #设置 php.ini 的搜索路径。默认为 PREFIX/lib
–with-mysql #mysql安装目录,对mysql的支持 7.0版本没有此参数
–with-mysqli #mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定。是一个数据库驱动
–with-iconv-dir #种字符集间的转换
–with-freetype-dir #打开对freetype字体库的支持
–with-jpeg-dir #打开对jpeg图片的支持
–with-png-dir #打开对png图片的支持
–with-zlib #打开zlib库的支持,实现GZIP压缩输出
–with-libxml-dir=/usr #打开libxml2库的支持,libxml是一个用来解析XML文档的函数库
–enable-xml #支持xml文档
–disable-rpath #关闭额外的运行库文件
–enable-bcmath #打开图片大小调整,用到zabbix监控的时候用到了这个模块
–enable-shmop #shmop共享内存操作函数,可以与c/c++通讯
–enable-sysvsem #加上上面shmop,这样就使得你的PHP系统可以处理相关的IPC函数(活动在内核级别)。
–enable-inline-optimization #优化线程
–with-curl #打开curl浏览工具的支持
–with-curlwrappers #运用curl工具打开url流 ,新版PHP5.6已弃用
–enable-mbregex #支持多字节正则表达式
–enable-fpm #CGI方式安装的启动程序,PHP-FPM服务
–enable-mbstring #多字节,字符串的支持
–with-gd #打开gd库的支持,是php处理图形的扩展库,GD库提供了一系列用来处理图片的API,使用GD库可以处理图片,或者生成图片。
–enable-gd-native-ttf #支持TrueType字符串函数库
–with-openssl #打开ssl支持
–with-mhash #支持mhash算法扩展
–enable-pcntl #freeTDS需要用到的,pcntl扩展可以支持php的多线程操作
–enable-sockets #打开 sockets 支持
–with-xmlrpc #打开xml-rpc的c语言
–enable-zip #打开对zip的支持
–enable-soap #扩展库通过soap协议实现了客户端与服务器端的数据交互操作
–with-mcrypt #mcrypt算法扩展
4.编译并安装

[root@localhost ~]# make -j 4 && make install

5.配置php和php-fpm
生成php自身配置 文件

[root@localhost ~]# cp /usr/local/src/php-7.1.24/php.ini-production /usr/local/php/php.ini

生成php-fpm配置文件

[root@localhost ~]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf

PHP-FPM启动脚本
修改 /usr/local/php/etc/php-fpm.conf 运行用户和组改为nginx

[root@localhost ~]# vim /usr/local/php/etc/php-fpm.conf

改:23 user = nobody
为:23 user = nginx

改:24 group = nobody
为:24 group = nginx
生成php-fpm启动脚本:

[root@localhost ~]# cp /usr/local/src/php-7.1.24/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost ~]# chmod +x /etc/init.d/php-fpm 
[root@localhost ~]# chkconfig php-fpm on
[root@localhost ~]# /etc/init.d/php-fpm start
Starting php-fpm  done

检测php-fpm有没有启动:

[root@localhost ~]# netstat -antup |grep php-fpm
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      97209/php-fpm: mast 

测试LNMP的PHP支持

[root@localhost ~]# echo "<?php phpinfo(); ?>" >/usr/local/nginx/html/index.php

浏览器访问:http://192.168.1.27/index.php
在这里插入图片描述
LNMP环境搭建完成!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值