1 lnmp搭建

1.1 基本环境

主机名

系统版本

IP地址

软件版本

web01

CentOS7.4

172.16.1.107

nginx-1.12.2.tar.gz

php-7.1.18.tar.bz2

db01

CentOS7.4

172.16.1.51

mysql-boost-5.7.22.tar.gz

1.2 nginx安装配置

tar xf nginx-1.12.2.tar.gz

cd nginx-1.12.2/

yum -y install pcre-devel openssl-devel

useradd -s /sbin/nologin -M www

./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module

make

make install

ln -s /application/nginx-1.12.2 /application/nginx

echo "PATH=$PATH:/application/nginx/sbin/" >>/etc/profile

source /etc/profile

nginx

1.3 php配置

yum -y install libxml2-devel curl-devel openjpeg openjpeg-devel openjpeg-libs libjpeg libpng freetype libjpeg libpng-devel freetype-devel  mcrypt php-mcrypt libmcrypt libmcrypt-devel libxslt-devel*

tar xf php-7.1.18.tar.bz2

cd php-7.1.18/

./configure --prefix=/application/php-7.1.18 \

--with-curl \

--with-freetype-dir \

--with-gd \

--with-gettext \

--with-iconv-dir \

--with-kerberos \

--with-libdir=lib64 \

--with-libxml-dir \

--with-mysqli \

--with-openssl \

--with-pcre-regex \

--with-pdo-mysql \

--with-pdo-sqlite \

--with-pear \

--with-png-dir \

--with-xmlrpc \

--with-xsl \

--with-zlib \

--enable-fpm \

--enable-bcmath \

--enable-libxml \

--enable-inline-optimization \

--enable-gd-native-ttf \

--enable-mbregex \

--enable-mbstring \

--enable-opcache \

--enable-pcntl \

--enable-shmop \

--enable-soap \

--enable-sockets \

--enable-sysvsem \

--enable-xml \

--enable-zip

make

make install

cp php.ini-production /application/php-7.1.18/lib/php.ini

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod +x /etc/init.d/php-fpm

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

cp /application/php-7.1.18/etc/php-fpm.d/www.conf.default /application/php-7.1.18/etc/php-fpm.d/www.conf

ln -s /application/php-7.1.18/ /application/php

启动php服务

方法一

# /etc/init.d/php-fpm start

方法二

# /application/php-7.1.18/sbin/php-fpm

1.4 nginxPHP结合

# egrep -v "#|^$" /application/nginx/conf/nginx.conf.default >/application/nginx/conf/nginx.conf

# cat /application/nginx/conf/nginx.conf

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  localhost;

        root   html;

        index  index.php index.html index.htm;

        location ~* .*\.(php|php5)?$ {

             fastcgi_pass  127.0.0.1:9000;

             fastcgi_index index.php;

             include fastcgi.conf;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

        }

    }

}

创建测试文件phpinfo.php

# cat /application/nginx/html/phpinfo.php

<?php

phpinfo();

?>

浏览器输入http://10.0.0.107/phpinfo.php,查看能否正常显示php信息的页面

1.5 mysql安装配置

db01上操作

1.5.1 卸载自带的mariadb相关软件包

# rpm -qa|grep mariadb

mariadb-libs-5.5.56-2.el7.x86_64

# rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps

# yum install libaio-devel ncurses-devel cmake gcc gcc-c++ bison -y

1.5.2 创建相关目录

# mkdir -p /application/mysql/tmp -p

# mkdir -p /application/mysql/log -p

# useradd -s /sbin/nologin -M mysql

# cd /server/tools/

解压mysql-boost-5.7.22.tar.gz包,该包里包含了boost包和mysql包,从MySQL 5.7.5开始Boost库是必需的

# tar xf mysql-boost-5.7.22.tar.gz

1.5.3 编译安装

# cd mysql-5.7.22/

cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql/ \

-DMYSQL_DATADIR=/application/mysql/data \

-DDOWNLOAD_BOOST=1 \

-DWITH_BOOST=boost \

-DSYSCONFDIR=/etc \

-DWITH_INNOBASE_STORAGE_ENGINE=1 \

-DWITH_PARTITION_STORAGE_ENGINE=1 \

-DWITH_FEDERATED_STORAGE_ENGINE=1 \

-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

-DWITH_MYISAM_STORAGE_ENGINE=1 \

-DENABLED_LOCAL_INFILE=1 \

-DENABLE_DTRACE=0 \

-DDEFAULT_CHARSET=utf8 \

-DWITH_EMBEDDED_SERVER=1

# make

# make install

# echo "export PATH=$PATH:/application/mysql/bin/" >>/etc/profile

# source /etc/profile

1.5.4 拷贝启动脚本,设为开机自启动

# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld

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

# chkconfig --add mysqld

# chkconfig mysqld on

1.5.5 创建mysql配置文件

cat >/etc/my.cnf <<EOF

[mysqld]

basedir = /application/mysql/

datadir = /application/mysql/data/

tmpdir = /application/mysql/tmp/

pid-file = /application/mysql/data/my.pid

port = 3306

 

default_storage_engine = InnoDB

innodb_autoinc_lock_mode = 2

 

explicit_defaults_for_timestamp = true

character-set-client-handshake = FALSE

character_set_server = utf8mb4

skip-name-resolve

max_connect_errors = 1000000

max_allowed_packet = 1G

 

connect_timeout = 3600

wait_timeout = 3600

interactive_timeout = 3600

innodb_lock_wait_timeout = 10

slave-skip-errors  = 1032,1062

log-error = /application/mysql/log/error.log

 

slow_query_log = on

slow_query_log_file = /application/mysql/log/slow-query-log.log

long_query_time = 1

log-queries-not-using-indexes

log-slow-admin-statements

log-slow-slave-statements

 

server-id = 100

log-bin = log-bin

binlog-format = ROW

EOF

1.5.6 初始化数据库

之前版本mysql_install_db是在mysql_basedir/script下,5.7放在了mysql_install_db/bin目录下,且已被废弃

"--initialize"会生成一个随机密码(~/.mysql_secret),而"--initialize-insecure"不会生成密码

--datadir目标目录下不能有数据文件

# chown -R mysql.mysql /application/mysql/

# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql/ --datadir=/application/mysql/data

# mysqld_safe --defaults-file=/etc/my.cnf &   #启动mysql服务

# /etc/init.d/mysqld start                  #启动mysql服务,同上条命令一样

# mysql   #登录mysql,没有密码

# mysqladmin -uroot password 123456   #设置root密码为123456

# mysql -uroot -p123456 -e "grant all on *.* to root@'10.0.0.%' identified by '123456';"

1.6 测试PHP连接mysql

web01上操作

创建连接mysql的测试文件

# cat /application/nginx/html/connect_mysql.php

<?php

    $mysqli = new mysqli("10.0.0.151", "root", "123456");

    if(!$mysqli)  {

        echo"database error";

    }else{

        echo"connect mysql is successful";

    }

    $mysqli->close();

?>

浏览器中输入http://10.0.0.107/connect_mysql.php,显示connect mysql is successful,说明连接成功。


作者:茁壮的小草  QQ:1198143315 网址http://blog.51cto.com/wn2100