侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

php  /  管理员 发布于 7年前   333

前言

对于新手的一点建议:

  • 最好熟悉一下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最坑。

一 准备工作

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

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

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

执行完成。

进入解压后的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 0nginx="/usr/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/etc/nginx/nginx.conf"[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxstart() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 echo -n $"Starting $prog: "  daemon $nginx -c $NGINX_CONF_FILE retval=$? echo  [ $retval -eq 0 ] && touch $lockfile return $retval}stop() { echo -n $"Stopping $prog: "  killproc $prog -QUIT retval=$? echo  [ $retval -eq 0 ] && rm -f $lockfile return $retvalkillall -9 nginx}restart() { configtest || return $? stop sleep 1 start}reload() { configtest || return $? echo -n $"Reloading $prog: "  killproc $nginx -HUPRETVAL=$? echo }force_reload() { restart}configtest() {$nginx -t -c $NGINX_CONF_FILE}rh_status() { status $prog}rh_status_q() { rh_status >/dev/null 2>&1}case "$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 2esac

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

赋予脚本执行权限:

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

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

# chkconfig Cadd nginx# chkconfig nginx on

4. 启动服务。

# service nginx start

出现这玩意说明成功了!

注:如果报错 [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 Cinitialize Cuser=mysql Cbasedir=/usr/local/mysql Cdatadir=/mnt/data/

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

# /usr/local/mysql/scripts/mysql_install_db Cuser=mysql Cdatadir=/mnt/data/

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

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 Cadd mysqld

查看是否设置成功:

# chkconfig Clist 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

这样基本上,这个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,才是正确的,才可以开始编译,如果出问题,就解决,一般是少了什么库。


执行编译:

# 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 Cadd php-fpm# chkconfig Clist 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 fileERROR: failed to post process the configurationERROR: 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的测试页面,内容如下:

<?phpphpinfo();?>

如果出现这个熟悉的界面,说明就大功告成了!Linux下一个基本的LNMP就搭建完毕了。

总结

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


  • 上一条:
    Centos6.4 编译安装 nginx php的方法
    下一条:
    MySQL PHP语法浅析
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • Laravel 11.15版本发布 - Eloquent Builder中添加的泛型(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客