搭建LNMP
先安装mysql—->安装php—–>安装nginx
版本:
mysql 5.7.19
php 7.1.7
nginx 1.12.1
本次搭建除mysql外全部采用源码方式安装,因为采用源码的方式安装可以更好的对功能模块是否需要做定制。
安装依赖
1 | yum -y install gcc gcc-devel gcc-c++ gcc-c++-devel libaio-devel boost boost-devel autoconf* automake* zlib* libxml* ncurses-devel ncurses libgcrypt* libtool* cmake openssl openssl-devel bison bison-devel unzip numactl-devel |
创建mysql软件源
vim /etc/yum.repo.d/mysql.repo
1 | [mysql] |
1 | yum install mysql-community-libs mysql-community-common mysql-community-client mysql-community-devel mysql-community-server |
启动mysql
systemctl start mysqld
mysql初始化
mysqld –initialize –user=mysql
查找默认密码
grep “temporary password” /var/log/mysqld.log
默认密码策略设置密码需要大写、小写、特殊符号。关闭默认密码策略
编辑/etc/my.cnf
validate_password=off
重启mysql
systemctl restart mysqld
用初始密码登录mysql重启密码
1 | alter user root@localhost identified by 'passw0rd'; |
修改默认编码
由下图可见database和server的字符集使用了latin1编码方式,不支持中文,即存储中文时会出现乱码。以下是命令行修改为utf-8编码的过程,以支持中文
修改为utf-8打开,编辑/etc/my.cnf.d/server.cnf
在mysqld里面添加
character_set_server = utf8
重启mysql
systemctl restart mysqld
再次查看
安装php7
php7在性能上比php5.x有2倍多的提升,同时兼容性也特别好。同时php5.x很多openssl和openssh的漏洞
安装依赖包
1 | yum -y install libmcrypt-devel mcrypt mhash gd-devel ncurses-devel libxml2-devel bzip2-devel libcurl-devel curl-devel libjpeg-devel libpng-devel freetype-devel net-snmp-devel openssl-devel |
安装libconv
创建目录
mkdir /lnmp
进入目录
cd /lnmp
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
解压包
1 | tar xvf libiconv-1.14.tar.gz |
make时包此错
解决方法
1 | vi libiconv-1.14/srclib/stdio.in.h |
重新make && make install
保存动态链接库
1 | echo "/usr/local/lib64/" > /etc/ld.so.conf.d/lnmp.conf |
刷新
ldconfig
下载php7.1.7源码包
cd /lnmp
wget http://cn2.php.net/distributions/php-7.1.7.tar.gz
解压
tar -xvf php-7.1.7.tar.gz
配置和检查依赖php7.1.7
1 | ./configure --prefix=/usr/local/php7.1.7 --with-config-file-path=/usr/local/php7.1.7/etc --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir=/usr/local/libiconv1.14 --with-pcre-regex --with-zlib --with-bz2 --enable-calendar --with-curl --enable-dba --with-libxml-dir --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-native-ttf --with-mhash --enable-mbstring --with-mcrypt --enable-pcntl --enable-xml --disable-rpath --enable-shmop --enable-sockets --enable-zip --enable-bcmath --with-snmp --disable-ipv6 --with-gettext --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-openssl |
make && make install
复制php配置文件
1 | cp /lnmp/php-7.1.7/php.ini-production /usr/local/php7.1.7/etc/php.ini |
配置php
修改时区
1 | sed -i 's#;date.timezone =#date.timezone = Asia/Shanghai#g' /usr/local/php7.1.7/etc/php.ini |
隐藏php版本号
1 | sed -i 's#expose_php = On#expose_php = Off#g' /usr/local/php7.1.7/etc/php.ini |
配置启动脚本
1 | [root@localhost php-7.1.7]# cp /lnmp/php-7.1.7/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm |
配置fast-cgi
1 | cp /usr/local/php7.1.7/etc/php-fpm.d/www.conf /usr/local/php7.1.7/etc/php-fpm.conf |
修改fast-cgi
vim /usr/local/php7.1.7/etc/php-fpm.conf
修改已下参数
rlimit_files = 65535
创建www用户和组
1 | useradd www -s /sbin/nologin |
启动php-fpm
/etc/init.d/php-fpm start
设置开机自启
/sbin/chkconfig –add php-fpm
修改php.ini以满足zabbix要求
vim /usr/local/php7.1.7/etc/php.ini
限制执行目录,nginx网页放在/var/www/html下
1 | open_basedir = "/var/www/html/:/tmp" |
修改php连接mysql
pdo_mysql.default_socket= /var/lib/mysql/mysql.sock
mysqli.default_socket = /var/lib/mysql/mysql.sock
安装nginx
wget http://nginx.org/download/nginx-1.12.1.tar.gz
解压
[root@localhost lnmp]# tar -xvf nginx-1.12.1.tar.gz
[root@localhost lnmp]# cd nginx-1.12.1
1 | [root@localhost lnmp]#./configure --prefix=/usr/local/nginx1.12.1 --user=www --group=www --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module |
[root@localhost lnmp]#make && make install
配置nginx
1 | cp /usr/local/nginx1.12.1/conf/nginx.conf /usr/local/nginx1.12.1/conf/nginx.conf.bak |
vim /usr/local/nginx1.12.1/conf/nginx.conf
1 | user www www; |
修改ulimit
ulimit -n 65535
编辑
/etc/systemd/system.conf
设置DefaultLimitNOFILE=65535
重启系统
启动nginx
/usr/local/nginx1.12.1/sbin/nginx
重载nginx
/usr/local/nginx1.12.1/sbin/nginx -s reload
关闭nginx
/usr/local/nginx1.12.1/sbin/nginx -s stop
重启nginx
/usr/local/nginx1.12.1/sbin/nginx -s reopen
设置nginx启动脚本
编辑/etc/init.d/nginx
注意PATH和NAME变量
1 | #!/bin/bash |
添加执行权限
chmod a+x /etc/init.d/nginx
注册成服务
/sbin/chkconfig –add nginx
添加开机自启
/sbin/chkconfig nginx on
查看
创建目录
mkdir -p /var/www/html
修改用户和属组
chown www:www /var/www/html
重启测试
启动nginx
systemctl start nginx
重启
systemctl restart nginx
关闭
systemctl stop nginx
测试php
在/var/www/html下编写index.php文件
1 | <?php |
修改权限
chown www:www /var/www/html/index.php
重启nginx和php-fpm
打开浏览器输入地址
至此LNMP搭建完毕。