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

CentOS 6.6部署MySQL+Nginx+PHP环境

217次阅读
没有评论

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

CentOS 6.6 部署 MySQL+Nginx+PHP 环境

mysql 安装

下载 mysql 安装包

http://dev.mysql.com/downloads/mysql/
选择 Linux-Generic
选择 Linux – Generic (glibc 2.5) (x86, 64-bit), RPM

下载完毕后放到自定义放置目录,本例中放置在 /usr/local/software/mysql 目录下

解压缩

tar -xf MySQL-5.6.22-1.linux_glibc2.5.x86_64.rpm-bundle.tar

在安装 MySQL 之前,先检查 CentOS 系统中是否已经安装了一个 MySQL,如果已经安装先卸载,不然会导致安装新的 MySQL 失败。

rpm -qa | grep mysql   -- 查看系统之前是否已安装 MySQL。

mysql-libs-5.1.47-4.el6.i686

显示结果说明 CentOS6.0 系统自带了一个 MySQL,我们需要删除这个老版本,用 root 用户执行下面语句

rpm -e --nodeps mysql-libs-5.1.47-4.el6.i686  

在删除 MySQL 的 rpm 后,还要进行一些扫尾操作
第一种善后处理:使用下面命令进行处理。

rm -rf /var/lib/mysql*

rm -rf /usr/share/mysql*
另一种善后处理:卸载后 /var/lib/mysql 中的 /etc/my.cnf 会重命名为 my.cnf.rpmsave,/var/log/mysqld.log 会重命名为 /var/log/mysqld.log.rpmsave,如果确定没用后就手工删除。

开始安装 mysql 服务器

执行命令:

rpm -ivh MySQL-server-5.6.22-1.linux_glibc2.5.x86_64.rpm 

结果如下:

Preparing… ########################################### [100%]
1:MySQL-server ########################################### [100%]
warning: user mysql does not exist – using root
warning: group mysql does not exist – using root
2014-08-10 22:43:44 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).
2014-08-10 22:43:44 23012 [Note] InnoDB: Using atomics to ref count buffer pool pages
2014-08-10 22:43:44 23012 [Note] InnoDB: The InnoDB memory heap is disabled
2014-08-10 22:43:44 23012 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2014-08-10 22:43:44 23012 [Note] InnoDB: Memory barrier is not used
2014-08-10 22:43:44 23012 [Note] InnoDB: Compressed tables use zlib 1.2.3
2014-08-10 22:43:44 23012 [Note] InnoDB: Using Linux native AIO
2014-08-10 22:43:44 23012 [Note] InnoDB: Not using CPU crc32 instructions
2014-08-10 22:43:44 23012 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2014-08-10 22:43:44 23012 [Note] InnoDB: Completed initialization of buffer pool
2014-08-10 22:43:45 23012 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2014-08-10 22:43:45 23012 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2014-08-10 22:43:45 23012 [Note] InnoDB: Database physically writes the file full: wait…
2014-08-10 22:43:45 23012 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2014-08-10 22:43:46 23012 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2014-08-10 22:43:46 23012 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2014-08-10 22:43:46 23012 [Warning] InnoDB: New log files created, LSN=45781
2014-08-10 22:43:46 23012 [Note] InnoDB: Doublewrite buffer not found: creating new
2014-08-10 22:43:46 23012 [Note] InnoDB: Doublewrite buffer created
2014-08-10 22:43:46 23012 [Note] InnoDB: 128 rollback segment(s) are active.
2014-08-10 22:43:46 23012 [Warning] InnoDB: Creating foreign key constraint system tables.
2014-08-10 22:43:47 23012 [Note] InnoDB: Foreign key constraint system tables created
2014-08-10 22:43:47 23012 [Note] InnoDB: Creating tablespace and datafile system tables.
2014-08-10 22:43:47 23012 [Note] InnoDB: Tablespace and datafile system tables created.
2014-08-10 22:43:47 23012 [Note] InnoDB: Waiting for purge to start
2014-08-10 22:43:47 23012 [Note] InnoDB: 5.6.22 started; log sequence number 0
A random root password has been set. You will find it in‘/root/.mysql_secret’.
2014-08-10 22:43:48 23012 [Note] Binlog end
2014-08-10 22:43:48 23012 [Note] InnoDB: FTS optimize thread exiting.
2014-08-10 22:43:48 23012 [Note] InnoDB: Starting shutdown…
2014-08-10 22:43:50 23012 [Note] InnoDB: Shutdown completed; log sequence number 1625977

2014-08-10 22:43:50 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use –explicit_defaults_for_timestamp server option (see documentation for more details).
2014-08-10 22:43:50 23039 [Note] InnoDB: Using atomics to ref count buffer pool pages
2014-08-10 22:43:50 23039 [Note] InnoDB: The InnoDB memory heap is disabled
2014-08-10 22:43:50 23039 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2014-08-10 22:43:50 23039 [Note] InnoDB: Memory barrier is not used
2014-08-10 22:43:50 23039 [Note] InnoDB: Compressed tables use zlib 1.2.3
2014-08-10 22:43:50 23039 [Note] InnoDB: Using Linux native AIO
2014-08-10 22:43:50 23039 [Note] InnoDB: Not using CPU crc32 instructions
2014-08-10 22:43:50 23039 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2014-08-10 22:43:50 23039 [Note] InnoDB: Completed initialization of buffer pool
2014-08-10 22:43:50 23039 [Note] InnoDB: Highest supported file format is Barracuda.
2014-08-10 22:43:50 23039 [Note] InnoDB: 128 rollback segment(s) are active.
2014-08-10 22:43:50 23039 [Note] InnoDB: Waiting for purge to start
2014-08-10 22:43:50 23039 [Note] InnoDB: 5.6.22 started; log sequence number 1625977
2014-08-10 22:43:50 23039 [Note] Binlog end
2014-08-10 22:43:50 23039 [Note] InnoDB: FTS optimize thread exiting.
2014-08-10 22:43:50 23039 [Note] InnoDB: Starting shutdown…
2014-08-10 22:43:52 23039 [Note] InnoDB: Shutdown completed; log sequence number 1625987

A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in‘/root/.mysql_secret’.

You must change that password on your first connect,
no other statement but‘SET PASSWORD’will be accepted.
See the manual for the semantics of the‘password expired’flag.

Also, the account for the anonymous user has been removed.

In addition, you can run:

/usr/bin/mysql_secure_installation

which will also give you the option of removing the test database.
This is strongly recommended for production servers.

See the manual for more instructions.

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

New default config file was created as /usr/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

安装 mysql 客户端

rpm -ivh MySQL-client-5.6.22-1.linux_glibc2.5.x86_64.rpm

Preparing… ########################################### [100%]

1:MySQL-client ^C########################################### [100%] 安装完成

MySQL 的几个重要目录。

MySQL 安装完成后不像 SQL Server 默认安装在一个目录,它的数据库文件、配置文件和命令文件分别在不同的目录,了解这些目录非常重要,尤其对于 Linux 的初学者,因为 Linux 本身的目录结构就比较复杂,如果搞不清楚 MySQL 的安装目录那就无从谈起深入学习。

a、数据库目录 /var/lib/mysql/

b、配置文件 /usr/share/mysql(mysql.server 命令及配置文件)

c、相关命令 /usr/bin(mysqladmin mysqldump 等命令)

d、启动脚本 /etc/rc.d/init.d/(启动脚本文件 mysql 的目录)

如:/etc/rc.d/init.d/mysql start/restart/stop/status

更改 MySQL 目录。由于 MySQL 数据库目录占用磁盘比较大,而 MySQL 默认的数据文件存储目录为 /”var/lib/mysql”,也可以把要把数据目录移到“/”根目录下的“mysql_data”目录中(如果做测试用就不用移动了)。

把“/var/lib/mysql”整个目录移到“/mysql_data”

mv /var/lib/mysql /mysql_data

找到 my.cnf 配置文件

如果”/etc/”目录下没有 my.cnf 配置文件,请到“/usr/share/mysql/”下找到 my-default.cnf 文件,拷贝其中一个合适的配置文件到“/etc/”并改名为“my.cnf”中。命令如下:

cp /usr/share/mysql/my-medium.cnf  /etc/my.cnf

编辑 MySQL 的配置文件“/etc/my.cnf”为保证 MySQL 能够正常工作,需要指明”mysql.sock”文件的产生位置,以及默认编码修改为 UTF-8。用下面命令:

vim /etc /my.cnf

添加以下命令

[mysqld]

socket = /mysql_data/mysql/mysql.sock

datadir=/mysql_data/mysql

如果你的数据存储位置要改变的话需要在这里指定存储目录

lower_case_table_names=1

(注意 linux 下 mysql 安装完后是默认:区分表名的大小写,不区分列名的大小写;# lower_case_table_names = 0 0:区分大小写,1:不区分大小写)

character-set-server=utf8

[client]

socket = /mysql_data/mysql/mysql.sock

最后,需要修改 MySQL 启动脚本 /etc/rc.d/init.d/mysql,

vim /etc/rc.d/init.d/mysql

修改 datadir=/mysql_data/mysql。


vi  /etc/selinux/config

把 SELINUX=enforcing 改为 SELINUX=disabled 后存盘退出重启机器试试,必须要重启,很关键

机器重启后,配置防火墙,开放 3306 端口

vim /etc/sysconfig/iptables

新增

-A INPUT -m state –state NEW -m tcp -p tcp –dport 3306 -j ACCEPT

内容编辑后如下:

# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

启动 mysql

service mysql start

1 mysql 装完之后,默认的初始化 root 密码在 /root/.mysql_secret 里面

所以你登录的时候用 mysql -u root - p 密码,就可以了

2 登录之后,必须密码

mysql> set password=password('新的密码');

3 目前为止 root 只有本地登录权限,没有远程登录权限

至此,mysql 安装完毕。

接下来,要开放一个用户可以通过工具可以连接到服务器的数据库,

 创建用户命令如下:CREATE USER 'username'@'%' IDENTIFIED BY 'password';

授权如下:GRANT ALL ON *.* TO 'username'@'%';

Nginx 安装

安装 nginx 需要的环境,pcre(作用 rewrite)、zlib(作用压缩)、ssl,这个也可以自己下载编译安装

minimal 下要先安装如下支持

yum -y install gcc gcc-c++ autoconf automake openssl openssl-devel pcre-devel zlib-devel zlib pcre

执行以上代码,环境安装完毕。

下载对应的 nginx 安装包(nginx-*.tar.gz)

下载地址:http://nginx.org/en/download.html

本文中下载的是 1.11.1 版本

解压压缩包

tar –zxvf nginx-1.11.1.tar.gz

进入解压后的目录

cd nginx-1.11.1;

然后执行

./congigure --prefix=/usr/local/nginx;

继续执行代码

make && make install;

为了方便执行 nginx 命令,这里加入对应的环境变量

执行命令:

vim /ect/profile

新增如下内容

PATH=$PATH:/usr/local/nginx/sbin
export PATH

加好后如下:

# /etc/profile
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if ["$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [-x /usr/bin/id]; then
    if [-z "$EUID" ]; then
        # ksh workaround
        EUID=`id -u`
        UID=`id -ru`
    fi
    USER="`id -un`"
    LOGNAME=$USER
    MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if ["$EUID" = "0" ]; then
    pathmunge /sbin
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
    pathmunge /sbin after
fi

HOSTNAME=`/bin/hostname 2>/dev/null`
HISTSIZE=1000
if ["$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
    pathmunge /sbin
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
    pathmunge /sbin after
fi

HOSTNAME=`/bin/hostname 2>/dev/null`
HISTSIZE=1000
if ["$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

PATH=$PATH:/usr/local/nginx/sbin
export PATH

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [$UID -gt 199 ] && ["`id -gn`" = "`id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh ; do
    if [-r "$i" ]; then
        if ["${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null 2>&1
        fi
    fi
done

unset i
unset -f pathmunge

按 ESC 键退出 vim 编辑模式,输入

:wq

保存退出

让修改后的环境变量生效:

source /etc/profile

这样环境变量就加好了,测试一下呗。
输入命令:

nginx -v

成功配置后结果显示如下:

nginx version: nginx/1.11.1

接下来修改防火墙配置,开放 80 端口

执行命令:

vim /etc/sysconfig/iptables

新增内容

-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

修改后的内容如下:

**filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT*

:wq 保存退出

重启防火墙配置

执行命令:

/etc/init.d/iptables restart

防火墙重启成功后,启动 nginx
输入命令:

nginx

打开浏览器访问 ip,看见 nginx 欢迎页面说明成功。

php 环境的搭建

nginx 本身不能处理 PHP,它只是个 web 服务器,当接收到请求后,如果是 php 请求,则发给 php 解释器处理,并把结果返回给客户,接下来以 php-fpm 为例介绍如何使 nginx 支持 PHP。

新版 PHP 已经集成 php-fpm 了,不再是第三方的包了,推荐使用

安装前的环境准备:

yum -y install gcc automake autoconf libtool make
yum -y install gcc gcc-c++ glibc
yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel

centos 源不能安装 libmcrypt-devel,由于版权的原因没有自带 mcrypt 的包

使用 wget 可以通过以下路径下载

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz

或者单独去下载压缩包

下载好后解压

tar -zxvf libmcrypt-2.5.7.tar.gz   

进入解压后的目录

cd libmcrypt-2.5.7

# 编译(默认安装到 /usr/local/lib/)

执行代码:

./configure --prefix=/usr/local/libmcrypt

执行代码安装:

make && make install  

接下来新版 php-fpm 的安装

解压 php 安装包

tar -zvxf php-5.4.7.tar.gz

进入解压后的目录

cd php-5.4.7

执行命令:

./configure --prefix=/usr/local/php  --enable-fpm --with-mcryp=/usr/local/libmcrypt --enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath --enable-inline-optimization --with-bz2  --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli 

然后输入命令:

make all install

以上就完成了 php-fpm 的安装。

进入 php 目录

cd /usr/local/php

将对应的配置文件 copy
执行命令:

cp etc/php-fpm.conf.default etc/php-fpm.conf

修改 nginx 配置以支持 php-fpm

vim /usr/local/nginx/conf/nginx.conf

做如下修改:

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

修改配置文件后重启 nginx

nginx -s reload

在 /usr/local/nginx/html 下创建 index.php 文件,输入如下内容

<?php
    echo phpinfo();
?>

启动 php-fpm

执行命令:

/usr/local/php/sbin/php-fpm

访问 ip/index.php 出现 php 配置,成功搞定。

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

Ubuntu 13.04 安装 LAMP\Vsftpd\Webmin\phpMyAdmin 服务及设置 http://www.linuxidc.com/Linux/2013-06/86250.htm

CentOS 6.4 下的 LNMP 生产环境搭建及安装脚本 http://www.linuxidc.com/Linux/2013-11/92428.htm

生产环境实用之 LNMP 架构的编译安装 +SSL 加密实现 http://www.linuxidc.com/Linux/2013-05/85099.htm

LNMP 全功能编译安装 for CentOS 6.3 笔记 http://www.linuxidc.com/Linux/2013-05/83788.htm

CentOS 6.3 安装 LNMP (PHP 5.4,MyySQL5.6) http://www.linuxidc.com/Linux/2013-04/82069.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-09/135320.htm

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