前言

对于新手的一点建议:

  • 最好熟悉一下linux 的基本命令,vim的常用命令
  • 千万不要无脑复制,先看一下命令,特别是路径要注意
  • 学会排查错误

本篇安装的软件版本为:

  • Linux:Centos6.8
  • Nginx:1.10.3
  • MySQL:5.7.17
  • PHP:7.0.16

最近研究了Linux系统下的PHP环境搭建,个人感觉最好最好不要用yum默认的程序包安装,因为版本都比较低,下载最新的稳定版自行安装比较好。现在网上教程很多,之所以还记这篇,原因有一点,当你重复网上的教程自行安装时,90%还是会出现各种各样的问题,因为你可能linux的系统版本不同,你想装的软件版本不同,安装的方法不同,你下错了安装包的版本,还有其它乱七八糟的。举个例,比如你看着5.6的mysql安装教程,装5.7的,你感觉没问题,但是事实就是,5.7的不一样了!而且网上还没有新的这方面内容,不好找,这就需要你去摸索了,亲身经历啊。这里面,Niginx感觉最好配,MySQL最坑。

Centos 6.8编译安装LNMP环境(Nginx+MySQL+PHP)教程

一 准备工作

1. 关闭SELINUX

修改配置文件,重启服务后永久生效。

# sed -i ‘s/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config

命令行设置立即生效。

# setenforce 0

2. 如果是阿里云ECS用户,安全组设置中开启80端口方便调试。 

二 安装Nginx

1. 下载源码包

上Nginx官网,复制最新稳定版的下载地址过来,然后用wget下载(接下来需要下载安装包的都可以用wget):

# cd /usr/local/src
# wget http://nginx.org/download/nginx-1.10.3.tar.gz

Centos 6.8编译安装LNMP环境(Nginx+MySQL+PHP)教程

下载完成的状态基本都是以下这样的:

Centos 6.8编译安装LNMP环境(Nginx+MySQL+PHP)教程

2. 进行解压编译

# tar xvf nginx-1.10.3.tar.gz
# yum groupinstall “Development tools”
# yum -y install gcc wget gcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel

Centos 6.8编译安装LNMP环境(Nginx+MySQL+PHP)教程

执行完成。

进入解压后的nginx-1.10.3文件夹:

cd /usr/local/src/nginx-1.10.3

执行以下语句:

./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --user=nginx --group=nginx --with-pcre --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-threads --with-stream --with-stream_ssl_module

完成后执行编译:

# make && make install
# mkdir -pv /var/tmp/nginx/client

3. 添加SysV启动脚本。

用vim编辑脚本:

# vim /etc/init.d/nginx

写入以下内容:

#!/bin/sh 
# 
# nginx - this script starts and stops the nginx daemon 
# 
# chkconfig: - 85 15 
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \ 
#  proxy and IMAP/POP3 proxy server 
# processname: nginx 
# config: /etc/nginx/nginx.conf 
# config: /etc/sysconfig/nginx 
# pidfile: /var/run/nginx.pid 
# Source function library. 
. /etc/rc.d/init.d/functions
# Source networking configuration. 
. /etc/sysconfig/network
# Check that networking is up. 
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
 [ -x $nginx ] || exit 5
 [ -f $NGINX_CONF_FILE ] || exit 6
 echo -n $"Starting $prog: " 
 daemon $nginx -c $NGINX_CONF_FILE
 retval=$"Stopping $prog: " 
 killproc $prog -QUIT
 retval=$"Reloading $prog: " 
 killproc $nginx -HUP
RETVAL=$"$1" in
 start)
 rh_status_q && exit 0
 $1
 ;;
 stop)
 rh_status_q || exit 0
 $1
 ;;
 restart|configtest)
 $1
 ;;
 reload)
 rh_status_q || exit 7
 $1
 ;;
 force-reload)
 force_reload
 ;;
 status)
 rh_status
 ;;
 condrestart|try-restart)
 rh_status_q || exit 0
  ;;
 *)
 echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
 exit 2
esac

保存退出(按:wq!);可能你得稍微查一下vim的一些命令,不然操作时可能会出现一点小问题。

赋予脚本执行权限:

# chmod +x /etc/init.d/nginx

添加至服务管理列表,设置开机自启:

# chkconfig –add nginx
# chkconfig nginx on

4. 启动服务。

# service nginx start

Centos 6.8编译安装LNMP环境(Nginx+MySQL+PHP)教程

出现这玩意说明成功了!

注:如果报错 [emerg]: getpwnam(“nginx”) failed ;

解决方法:

# useradd -s /sbin/nologin -M nginx
# id nginx

三 安装mysql

1. 版本选择

在安装之前必须明白一件事情,mysql有很多种安装方式,每种不一样,不要弄混了。

比如源码编译安装(mysql-5.7.17.tar.gz),二进制安装(mysql-5.7.17-linux-glibc2.5-i686.tar),nmp安装(最简单的)。这里我们用源码自己编译安装。

2. 准备编译环境

# yum groupinstall “Server Platform Development” “Development tools” -y
# yum install cmake -y

cmake在现在的版本是必须要安装的,你可以下载camke之后编译,也可以直接yum安装。接下来的编译过程如果报错缺少什么就补什么。

3. 准备mysql数据库存放目录

# mkdir /mnt/data
# groupadd -r mysql
# useradd -r -g mysql -s /sbin/nologin mysql
# id mysql

4. 更改数据目录权限。

# chown -R mysql:mysql /mnt/data

5. 下载并解压编译官网下载的稳定版的源码包。

在下载的时候注意一下版本,下载对应的版本。我们源码编译,要下载长这样的安装包:mysql-5.7.17.tar.gz,同时在安装的时候我们需要boost库,5.7需要1.59版本的库;你可以下载boost库然后编译boost库,或者像我一样,下载带有boost库的mysql版本。

开始解压编译:

# tar xvf mysql-boost-5.7.17.tar.gz -C /usr/local/src
# cd /usr/local/src/mysql-5.7.17
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mnt/data -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/mysql/boost/boost_1_59_0 # make && make install

6. 修改安装目录的权限属组

# chown -R mysql:mysql /usr/local/mysql/

7. 初始化数据库。

# /usr/local/mysql/bin/mysqld –initialize –user=mysql –basedir=/usr/local/mysql –datadir=/mnt/data/

需要注意这里是mysql5.7的初始化命令,而5.7以下的都是用:

# /usr/local/mysql/scripts/mysql_install_db –user=mysql –datadir=/mnt/data/

在初始化成功之后,5.7的initial命令会产生一个随机的root登录密码,你要用这个密码登录,然后修改(必须修改生成的随机密码不然无法后续操作)。在最后有一个类似这样的密码:

Centos 6.8编译安装LNMP环境(Nginx+MySQL+PHP)教程

8. 复制配置文件

# cp support-files/my-default.cnf /etc/my.cnf

这里又有一点要注意:mysql5.7配置文件需要修改my.cnf关键配置, mysql5.7之前默认配置文件中是有配置项的,不用手动修改。以下为配置,根据实际情况修改:

</div>
<div>[mysqld]</div>
<div>basedir = /usr/local/mysql</div>
<div>datadir = /mnt/data</div>
<div>port = 3306</div>
<div>socket = /Ultrapower/test/mysql/tmp/mysql.sock</div>
<div></div>
<div>sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES</div>
<div>[client]</div>
<div>socket = /Ultrapower/test/mysql/tmp/mysql.sock</div>
<div>

如果添加[client]下 的内容,注意sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES要放在[mysqld]下。
如果报错tmp目录不错在,到对应的地方去创建目录,然后创建后要赋予mysql权限,chown -R mysql:mysql tmp。

9. 设置开机启动

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# chmod +x /etc/init.d/mysql

注册为开机启动服务:

# chkconfig mysqld on
# chkconfig –add mysqld

查看是否设置成功:

# chkconfig –list mysql

10. 设置PATH环境变量。

# echo “export PATH=$PATH:/usr/local/mysql/bin” > /etc/profile.d/mysql.sh
# source /etc/profile.d/mysql.sh

11. 启动服务

# service mysqld start

Centos 6.8编译安装LNMP环境(Nginx+MySQL+PHP)教程

这样基本上,这个mysql就装好了。

12. 登录mysql并修改密码

mysql -uroot -p生成的密码

执行修改密码:

alter user ‘root'@'localhost' identified by ‘newpassword';

四 安装php-fpm

1. 安装依赖包:

yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel

这里还漏了几个,如果报错了提示缺少了什么就yum补上。

2. 到官网下载源码包后,开始编译安装:

# tar xvf php-7.0.16.tar.bz2 -C /usr/local/src
# cd /usr/local/src/php-7.0.16
执行下面的配置文件:
# ./configure --prefix=/usr/local/php --with-config-file-scan-dir=/etc/php.d --with-config-file-path=/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-fpm --enable-opcache --disable-fileinfo --with-jpeg-dir --with-iconv-dir=/usr/local --with-freetype-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-bcmath --enable-shmop --enable-exif --with-curl --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-inline-optimization --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-ftp --with-gettext --enable-zip --enable-soap --with-bz2

执行以上的配置,如果出现下面这样的license,才是正确的,才可以开始编译,如果出问题,就解决,一般是少了什么库。

Centos 6.8编译安装LNMP环境(Nginx+MySQL+PHP)教程

执行编译:

# make && make install

3. 添加php和php-fpm配置文件。

# cp /usr/local/src/php-7.0.16/php.ini-production /etc/php.ini
# cd /usr/local/php/etc/
# cp php-fpm.conf.default php-fpm.conf
# sed -i ‘s@;pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@' php-fpm.conf

4. 添加php-fpm启动脚本。

# cp /usr/local/src/php-7.0.16/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod +x /etc/init.d/php-fpm

5. 添加php-fpm至服务列表并设置开机自启。

# chkconfig –add php-fpm
# chkconfig –list php-fpm
# chkconfig php-fpm on

6. 启动服务。

# service php-fpm start

注:启动时如出现错误:

WARNING: Nothing matches the include pattern ‘/usr/local/etc/php-fpm.d/*.conf' from /usr/local/etc/php-fpm.conf at line 125.
ERROR:. No pool defined at least one pool section must be specified in config file
ERROR: failed to post process the configuration
ERROR: FPM initialization failed

解决:到指定目录执行cp www.conf.default www.conf

7. 添加nginx对fastcgi的支持,

首先备份默认的配置文件。

# cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak

# cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf

编辑/etc/nginx/nginx.conf,在所支持的主页面格式中添加php格式的主页,类似如下:

</div>
<div>location / {</div>
<div>root /usr/local/nginx/html;</div>
<div>index index.php index.html index.htm;</div>
<div>}</div>
<div>

取消以下内容前面的注释:

</div>
<div>location ~ \.php$ {</div>
<div>root /usr/local/nginx/html;</div>
<div>fastcgi_pass 127.0.0.1:9000;</div>
<div>fastcgi_index index.php;</div>
<div>fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;</div>
<div>include fastcgi_params;</div>
<div>}</div>
<div>

8. 重启nginx

# service nginx reload

9. 测试是否成功

在/usr/local/nginx/html/新建index.php的测试页面,内容如下:

<"color: #ff0000">总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。

标签:
centos,编译安装lnmp,centos,6.8编译lnmp,centos安装lnmp环境

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com