阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

CentOS7.2编译安装LNMP

114次阅读
没有评论

共计 9544 个字符,预计需要花费 24 分钟才能阅读完成。

一、前言

1、环境说明

基础环境 Linux+Nginx+MySQL+PHP

Linux:CentOS7.2

Nginx:1.10.2

MySQL:5.6.17

PHP:5.5.12

2、部署说明

php 安装目录:/usr/local/php

php.ini 配置文件路径:/usr/local/php/etc/php.ini

Nginx 安装目录:/usr/local/nginx

Nginx 配置文件路径:/etc/nginx/nginx.conf

Nginx 网站根目录:/usr/local/nginx/html

二、准备工作

1、最小化安装 Centos7.2

# 建立一个软件包目录存放 
mkdir -p /usr/local/src/ 
# 清理已经安装包 
rpm -e httpd 
rpm -e mysql 
rpm -e php 
yum -y remove httpd 
yum -y remove mysql 
yum -y remove php 
# 搜索 apache 包 
rpm -qa http* 
# 强制卸载 apache 包 
rpm -e –nodeps 查询出来的文件名 
# 检查是否卸载干净 
rpm -qa|grep http*

selinux 可能会致使编译安装失败,我们先禁用它。

永久禁用,需要重启生效

sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/g’ /etc/selinux/config

临时禁用,不需要重启

1 setenforce 0

2、安装必备工具

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

三、安装 MySQL5.6.17

1、准备工作

按照标准需要给 mysql 创建所属用户和用户组

# 创建群组
groupadd mysql
# 创建一个用户,不允许登陆和不创主目录
useradd -s /sbin/nologin -g mysql -M mysql 
# 检查刚创建的用户
tail -1 /etc/passwd

centos 最小化安装后,会有 mysql 的库因此先卸载!

# 检查安装与否
rpm -qa|grep mysql
# 强制卸载
rpm -erpm -e mysql-libs-5.1.73-3.el6_5.x86_64 –nodeps

MySQL 从 5.5 版本开始,通过./configure 进行编译配置方式已经被取消,取而代之的是 cmake 工具。因此,我们首先要在系统中源码编译安装 cmake 工具。

wgethttp://www.cmake.org/files/v2.8/cmake-2.8.12.2.tar.gz 
tar zxvf cmake-2.8.12.2.tar.gz 
cd cmake-2.8.12.2 
./configure 
make && make install

2、使用 cmake 来编译安装 MySQL 5.6.17

wgethttp://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.17.tar.gz 
tar zxvf mysql-5.6.17.tar.gz 
cd mysql-5.6.17
cmake \ 
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ 
-DMYSQL_DATADIR=/usr/local/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 \ 
-DMYSQL_USER=mysql \ 
-DWITH_DEBUG=0 \ 
-DWITH_SSL=system 
 
make && make install 
 
# 修改 /usr/local/mysql 权限
chmod +w /usr/local/mysql 
chown -R mysql:mysql /usr/local/mysql

关于 my.cnf 配置文件:

在启动 MySQL 服务时,会按照一定次序搜索 my.cnf,先在 /etc 目录下找,找不到则会搜索”$basedir/my.cnf”就是安装目录下 /usr/local/mysql/my.cnf,这是新版 MySQL 的配置文件的默认位置!注意:在最小安装完成后,在 /etc 目录下会存在一个 my.cnf,需要将此文件更名为其他的名字。如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的 MySQL 的正确配置,造成无法启动。由于我们已经卸载了最小安装完成后的 mysql 库所以,就没必要操作了。

cd support-files/ 
# 如果还有 my.cnf 请备份
mv /etc/my.cnf /etc/my.cnf.bak 
cp my-default.cnf /etc/my.cnf 
# 执行初始化配置脚本,创建系统自带的数据库和表,注意配置文件的路径
/usr/local/mysql/scripts/mysql_install_db–defaults-file=/etc/my.cnf –basedir=/usr/local/mysql–datadir=/usr/local/mysql/data –user=mysql
# 拷贝 mysql 安装目录下 support-files 服务脚本到 init.d 目录 
cp support-files/mysql.server /etc/init.d/mysqld 
# 赋予权限、设置开机自启并启动
chmod +x /etc/init.d/mysqld 
chkconfig mysqld on 
service mysqld start 
# 或者 
/etc/init.d/mysql start

MySQL5.6.x 启动成功后,root 默认没有密码,我们需要设置 root 密码。设置之前,我们需要先设置 PATH,否则不能直接调用 mysql

# 修改 /etc/profile 文件 
vim /etc/profile 
# 在文件末尾添加 
PATH=/usr/local/mysql/bin:$PATH 
export PATH 
# 让配置立即生效 
source /etc/profile 
# 登陆测试,默认是没有密码, 直接回车就可进入 
mysql -uroot -p 
# 设置 mysql 密码 
/usr/local/mysql/bin/mysqladmin -uroot -p password ‘ 你的密码 ’ 
# 登陆进命令行模式 
mysql -uroot -p 
# 查看用户 
>select user,host from mysql.user; 
# 删除不必要的用户 
>drop user “”@localhost;
> drop user root@’::1′;
# 赋予账号远程访问的权限
>GRANT ALL PRIVILEGES ON *.* TO ‘root’@’127.0.0.1’ IDENTIFIED BY ‘ 你的密码 ’;
>GRANT ALL PRIVILEGES ON *.* TO ‘root’@’localhost’ IDENTIFIED BY ‘ 你的密码 ’;
# 其它一些信息查询:
# 检查 mysql 版本 
mysql -uroot -p” 密码 ” -e “select version();”

MySQL 安装完毕!

四、安装 PHP5.5.12

1、安装依赖关系

yum install libmcrypt libmcrypt-devel mcrypt mhash

2、下载并编译安装 php

wget http://mirrors.sohu.com/php/php-5.5.12.tar.gz 
tar zxvf php-5.5.12.tar.gz 
cd php-5.5.12 
./configure –prefix=/usr/local/php–with-config-file-path=/usr/local/php/etc –enable-fpm –with-fpm-user=www–with-fpm-group=www –with-mysql=mysqlnd –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-magic-quotes –enable-safe-mode –enable-bcmath –enable-shmop–enable-sysvsem –enable-inline-optimization –with-curl –with-curlwrappers–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 
make && make install

下面的警告说明已自带不需启用或安装,可忽略:

CentOS7.2 编译安装 LNMP

继续:

# 修改 fpm 配置 php-fpm.conf.default 文件名称 
mv /usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf 
# 复制 php.ini 配置文件 
cp php.ini-production /usr/local/php/etc/php.ini 
# 复制 php-fpm 启动脚本到 init.d 
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 
# 赋予执行权限 
chmod +x /etc/init.d/php-fpm 
# 添加为启动项 
chkconfig –add php-fpm 
# 设置开机启动 
chkconfig php-fpm on 
# 按照标准,给 php-fpm 创建一个指定的用户和组 
# 创建群组 
groupadd www 
# 创建一个用户,不允许登陆和不创主目录 
useradd -s /sbin/nologin -g www -M www 
# 立即启动 php-fpm 
/etc/init.d/php-fpm start

PHP 安装完毕!

五、安装 Nginx1.10.2

1、准备工作

# 添加用户与属组
groupadd -r nginx 
useradd -s /sbin/nologin -g nginx -M nginx
# 创建目录
mkdir -pv /var/tmp/nginx/client/ 
# 解压安装包
tar zxvf nginx-1.10.2.tar.gz

2、编译安装

./configure \ 
 –prefix=/usr/local/nginx \ 
 –sbin-path=/usr/local/nginx/bin/nginx \ 
 –conf-path=/usr/local/nginx/conf/nginx.conf \ 
 –error-log-path=/var/log/nginx/error.log \ 
 –http-log-path=/var/log/nginx/access.log \ 
 –pid-path=/var/run/nginx/nginx.pid  \ 
 –lock-path=/var/lock/nginx.lock \ 
 –user=nginx \ 
 –group=nginx \ 
 –with-http_ssl_module \ 
 –with-http_flv_module \ 
 –with-http_stub_status_module \ 
 –with-http_gzip_static_module \ 
 –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 \ 
 –with-pcre

敲黑板:注意 sbin、conf、pid、local 的 path 要和后续的控制脚本保持一致!

make && make install

3、为 Nginx 提供 SysV init 脚本

cat /etc/init.d/nginx

#!/bin/sh

#

# nginx – this script starts and stops the nginx daemin

#

# chkconfig:  – 85 15

# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \

#              proxy and IMAP/POP3 proxy server

# processname: nginx

# config:      /usr/local/nginx/conf/nginx.conf

# pidfile:    /run/nginx/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=”/usr/local/nginx/conf/nginx.conf “

 lockfile=/var/lock/nginx.lock

 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

}

 restart() {

    configtest || return $?

    stop

    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

类似的脚本网上很多,注意 config、pid、sbin 的路径保持和编译一致即可。

chmod +x /etc/init.d/nginx 
chkconfig –add nginx 
chkconfig nginx on
# 然后就可以启动服务了
systemctl restart nginx.service 
# 或 
/etc/init.d/nginx start

Nginx 安装完毕,下面开始整合 Nginx 与 PHP

六、整合 Nginx 与 PHP

1、编辑 Nginx.conf,启用如下选项:

cd /usr/local/nginx/conf/ 
cat nginx.conf

location ~ \.php$ {

            root          html;

            fastcgi_pass  127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

            include        fastcgi_params;

        }
 

2、编辑 /etc/nginx/fastcgi_params,将其内容更改为如下内容:

# mv fastcgi_paramsfastcgi_params.bak 
# cat fastcgi_params

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING      $query_string;
fastcgi_param  REQUEST_METHOD    $request_method;
fastcgi_param  CONTENT_TYPE      $content_type;
fastcgi_param  CONTENT_LENGTH    $content_length;
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI      $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

在所支持的主页面格式中添加 php 格式的主页,类似如下:

location / {

            root  html; 

            index  index.php index.html index.htm; 

        }

# 重新载入配置
# /etc/init.d/nginx reload

3、在 /usr/local/nginx/html 目录下新建 index.php 测试页面

cat > /usr/local/nignx/htmlindex.php << EOF

<?php

phpinfo();

?>

可通过浏览器 访问此测试页面,验证 nginx 连接 php 配置是否成功。页面应该如下图

CentOS7.2 编译安装 LNMP

至此 CenOS72 下 LNMP 编译安装完毕!

LNMP 安装参考如下文章

Ubuntu 16.04 下源码配置 LNMP 开发环境 http://www.linuxidc.com/Linux/2016-09/135381.htm

Ubuntu 14.04 搭建 LNMP  http://www.linuxidc.com/Linux/2015-05/116933.htm

Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL  http://www.linuxidc.com/Linux/2014-05/102351.htm

在部署 LNMP 的时候遇到 Nginx 启动失败的 2 个问题 http://www.linuxidc.com/Linux/2013-03/81120.htm

Ubuntu 安装 Nginx php5-fpm MySQL(LNMP 环境搭建) http://www.linuxidc.com/Linux/2012-10/72458.htm

更多 CentOS 相关信息见 CentOS 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=14

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2016-12/138668.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计9544字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中