资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Centos6.8编译安装LNMP环境的方法-创新互联

本篇内容主要讲解“Centos6.8编译安装LNMP环境的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Centos6.8编译安装LNMP环境的方法”吧!

在连平等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、网站设计 网站设计制作按需设计,公司网站建设,企业网站建设,高端网站设计,全网营销推广,成都外贸网站建设,连平网站建设费用合理。

Centos6.8编译安装LNMP环境的方法一 准备工作

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

Centos6.8编译安装LNMP环境的方法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 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=$?
 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 $retval
killall -9 nginx
}
restart() {
 configtest || return $?
 stop
 sleep 1
 start
}
reload() {
 configtest || return $?
 echo -n $"reloading $prog: " 
 killproc $nginx -hup
retval=$?
 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 2
esac

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


赋予脚本执行权限:


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

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


# chkconfig –add 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 –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登录密码,你要用这个密码登录,然后修改(必须修改生成的随机密码不然无法后续操作)。

Centos6.8编译安装LNMP环境的方法

8. 复制配置文件


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

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

[mysqld]
basedir = /usr/local/mysql
datadir = /mnt/data
port = 3306
socket = /ultrapower/test/mysql/tmp/mysql.sock
sql_mode=no_engine_substitution,strict_trans_tables
[client]
socket = /ultrapower/test/mysql/tmp/mysql.sock

如果添加[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

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

执行编译:


# 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格式的主页,类似如下:

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

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

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

8. 重启nginx


# service nginx reload

9. 测试是否成功


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


到此,相信大家对“Centos6.8编译安装LNMP环境的方法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联建站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


网页标题:Centos6.8编译安装LNMP环境的方法-创新互联
本文网址:http://cdkjz.cn/article/jogjc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220