CentOS 6.6部署MySQL+Nginx+PHP环境
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
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配置,成功搞定。