CentOS7.4中搭建lnmp环境

前端之家收集整理的这篇文章主要介绍了CentOS7.4中搭建lnmp环境前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1 lnmp搭建

1.1 基本环境

@H_404_59@

Nginx-1.12.2.tar.gz

PHP-7.1.18.tar.bz2

@H_404_59@

MysqL-boost-5.7.22.tar.gz

主机名

系统版本

IP地址

软件版本

web01

CentOS7.4

172.16.1.107

db01

CentOS7.4

172.16.1.51

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

猜你在找的CentOS相关文章