CentOS5.5 安装 Nginx+PHP+MariaDB(主备同步)
0.前期准备(目录设定,yum安装相关依赖包)
# /usr/local/ 安装目录 # /data/src/ 源文件包目录 # yum install gcc gcc-c++ automake autoconf libtool make cmake
1.安装nginx
nginx依赖以下模块:
-
rewrite模块需要 pcre 库
-
ssl 功能需要openssl库
-
gzip模块需要 zlib 库
1.1安装pcre
# tar -zxvf pcre-8.38.tar.gz # cd pcre-8.38 # ./configure # make & make install
1.2安装openssl
# tar -zxvf openssl-1.0.1c.tar.gz # cd openssl-1.0.1c # ./config # make & make install
1.3安装zlib
# tar -zxvf zlib-1.2.8.tar.gz # cd zlib-1.2.8 # ./configure # make & make install
1.4安装nginx
先建立www用户组,www用户
# groupadd www # useradd -g www www
接着配置时,直接指定pcre,zlib,openssl的路径
# tar -zxvf nginx-1.4.2.tar.gz # cd nginx-1.4.2 # ./configure --with-http_ssl_module \--with-pcre=/data/src/pcre-8.38 \--with-zlib=/data/src/zlib-1.2.8 \--with-openssl=/data/src/openssl-1.0.1c # make & make install
没有报错的话,就说明安装成功了。接下来配置nginx。路径位于/usr/local/nginx/conf/nginx.conf,路径不同的话,可以用命令查找一下 locate nginx.conf,贴上我的配置:
nginx.conf
user www www; worker_processes 10; pid /var/run/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; server_tokens off; expires 1h; sendfile on; tcp_nopush on; keepalive_timeout 60; tcp_nodelay on; error_page 404 /404.jpg; fastcgi_connect_timeout 20; fastcgi_send_timeout 30; fastcgi_read_timeout 120; fastcgi_buffer_size 256k; fastcgi_buffers 8 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_temp_path /dev/shm; gzip on; gzip_min_length 2048; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_types text/plain text/css application/xml application/x-javascript ; log_format access '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" $http_x_forwarded_for'; server { listen 80; server_name localhost; index index.html index.htm index.php; root /data/www; charset utf-8; expires 2h; location ~ .*\.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; expires off; } if ( $request_uri ~* (\.svn|\.swp) ) { return 404; } access_log off; } }
测试配置文件是否正确
配置文件没问题,确保系统的 80 端口没被其他程序占用,启动nginx
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ps命令查看一下启动结果
2.安装PHP
安装php之前,先安装以下依赖包:
# 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
接着安装
# tar -zxvf php-5.5.30.tar.gz # cd php-5.5.30 # ./configure --prefix=/usr/local/php --with-config-file-path=/etc --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-iconv-dir=/usr/local/iconv --with-freetype-dir --with-jpeg-dir --with-png-dir --enable-zip --with-zlib --with-gd --enable-pcntl --disable-debug --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --disable-ipv6 --enable-static --enable-maintainer-zts --enable-sockets --enable-soap --with-openssl --enable-fpm # make & make install
没有报错的话,就说明安装成功了。再接下来复制php.ini,php-fpm.conf到/etc/下并修改之:
cp /data/src/php-5.5.30/php.ini-development /etc/php.ini cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
php.ini
date.timezone = "Asia/Shanghai"
php-fpm.conf
[global] pid = /var/run/php-fpm.pid error_log = log/php-fpm.log [www] user = www group = www pm.max_children = 256 pm.start_servers = 256 pm.min_spare_servers = 5 pm.max_spare_servers = 256
OK,配置完成,启动php-fpm
#/usr/local/php/sbin/php-fpm -c /etc/php.ini -y /usr/local/php/etc/php-fpm.conf
ps命令查看一下启动结果
至此,nginx和php都安装成功了,在/data/www/分别建立index.html,index.php,看是否能正常访问。
3.安装mariaDB(主从同步)
3.1建立mysql用户
# groupadd -r mysql # useradd -g mysql -r -M -s /sbin/nologin mysql //这个mysql用户不能登录
3.2安装主库,端口设置为3306
# tar -zxvf mariadb-5.5.46.tar.gz # cd mariadb-5.5.46 # cmake . -DMYSQL_UNIX_ADDR=/tmp/mysql1.sock -DSYSCONFDIR=/usr/local/db1 -DMYSQL_TCP_PORT=3306 -DEXTRA_CHARSETS=all -DMYSQL_USER=mysql -DCMAKE_INSTALL_PREFIX=/usr/local/db1 -DMYSQL_DATADIR=/home/data/db1 -DWITH_XTRADB_STORAGE_ENGINE=1 -DWITH_FEDERATEDX_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STPRAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWIYH_READLINE=1 -DWIYH_SSL=system -DVITH_ZLIB=system -DWITH_LOBWRAP=0 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci # make -j 4 # make install
复制配置文件
# cp /usr/local/db1/support-files/my-large.cnf /usr/local/db1/my.cnf
初始化数据库
# /usr/local/db1/scripts/mysql_install_db --basedir=/usr/local/db1 --datadir=/home/data/db1 --user=mysql --defaults-file=/usr/local/db1/my.cnf
复制服务启动脚本
# cp /usr/local/db1/support-files/mysql.server /etc/init.d/mysql1
编辑启动脚本mysql1(路径:/etc/init.d/mysql1)
找到 $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 & 行,改为 $bindir/mysqld_safe --defaults-file=/usr/local/db1/my.cnf --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 & 即添加了 --defaults-file=/usr/local/db1/my.cnf
赋予执行权限
# chmod +x /etc/init.d/mysql1 # chkconfig mysql1 on
修改配置文件my.cnf(路径:/usr/local/db1/my.cnf)
[mysqld] log-error = /var/log/mysql_error.log pid-file =/usr/local/db1/mysql1.pid user=mysql datadir = /home/data/db1 basedir = /usr/local/db1 [mysqld_safe] log-error= /var/log/mysql_error.log pid-file=/usr/local/db1/mysql1.pid
更改 目录权限
# chown -R mysql:mysql /home/data/db1 # chown -R mysql:mysql /home/data/db1/. # chown -R mysql:mysql /usr/local/db1 # chown -R mysql:mysql /usr/local/db1/.
启动MariaDB:
# /etc/init.d/mysql1 start
设置root的密码
# /usr/local/db1/bin/mysqladmin -u root password 'root'
3.3安装备库,端口设置为3307
重复3.2的步骤,把db1替换成db2,mysql1替换成mysql2,3306替换成3307,
至此,主备库都安装好了,并且成功启动。ps命令查看结果如下:
3.4设置主备同步
建立文件目录,用于主备同步二进制文件存放,并设置权限
# mkdir -p /home/data/binlogs/ # chown -R mysql:mysql /home/data/binlogs/
修改主库配置文件my.cnf(路径:/usr/local/db1/my.cnf)
[mysqld] log-bin=/home/data/binlogs/master-bin #设置用于存放二进制文件的路径以及文件名(master-bin.000001) server-id = 1 #主备库的id不能相同
修改备库配置文件my.cnf(路径:/usr/local/db2/my.cnf)
[mysqld] #log-bin=mysql-bin #注释掉这一行 server-id = 2 #主备库的id不能相同
重启主库,备库
# /etc/init.d/mysql1 restart # /etc/init.d/mysql2 restart
进入主库,建立backup用户(指定IP地址,用户名,密码,分配权限),并查看master状态。
# /usr/local/db1/bin/mysql -uroot -proot MariaDB [(none)]> CREATE USER 'backup'@'127.0.0.1' IDENTIFIED BY 'root'; MariaDB [(none)]> GRANT RELOAD,LOCK TABLES, REPLICATION CLIENT ,REPLICATION SLAVE ON *.* TO 'backup'@'127.0.0.1'; MariaDB [(none)]> FLUSH PRIVILEGES; MariaDB [(none)]> SHOW MASTER STATUS; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000002 | 245 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
进入备库,进行设置。
# /usr/local/db2/bin/mysql -uroot -proot MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='127.0.0.1',MASTER_USER='backup',MASTER_PASSWORD='root',MASTER_PORT=3306,MASTER_LOG_FILE='master-bin.000002',MASTER_LOG_POS=245; MariaDB [(none)]> START SLAVE; MariaDB [(none)]> SHOW SLAVE STATUS\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 127.0.0.1 Master_User: backup Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000002 Read_Master_Log_Pos: 245 Relay_Log_File: mysql2-relay-bin.000005 Relay_Log_Pos: 530 Relay_Master_Log_File: master-bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 245 Relay_Log_Space: 1110 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec)
说明:
MASTER_HOST,指的是主库用于同步的IP地址,因为本演示主库备都在同一台机子,所以是127.0.0.1。这个IP地址要和主库所指的IP地一致'backup'@'127.0.0.1'
MASTER_USER,指的是主库用于同步的用户名,就是主库所设置的backup
MASTER_PASSWORD,指的是主库用于同步的密码,root
MASTER_PORT,指的是主库用于同步的端口,3306
MASTER_LOG_FILE,指的是主库用于同步的二进制文件名称,通过主库SHOW MASTER命令得到,FILE字段值
MASTER_LOG_POS,指的是主库用于同步的二进制文件的偏移量,通过主库SHOW MASTER命令得到,Position字段值
Slave_IO_Running: Yes,Slave 的 I/O 状态
Slave_SQL_Running: Yes,SQL 线程状态
只有当Slave 的 I/O状态和SQL 线程状态,都为YES时,才说明主备同步成功了。如果失败了,就查看Last_SQL_Error的提示信号,再具体问题具体分析,解决了。
另外:
MariaDB [mysql]>use mysql; //选择系统数据库MySQL MariaDB [mysql]>select Host,User,Password from user; //查看所有用户 MariaDB [mysql]>delete from user where password=""; //删除所有密码为空的用户 MariaDB [mysql]>GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY 'root'; //为root添加远程连接的能力 MariaDB [mysql]>flush privileges; //刷新MySQL的系统权限 MariaDB [mysql]>exit;
设置防火墙,以便局域网内的其它服务器可以访问(路径:/etc/sysconfig/iptables),添加以下内容:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 3307 -j ACCEPT
重启防火墙
# /etc/init.d/iptables restart