LNMP环境的搭建配置
概述:Linux + Nginx + MySQL + PHP。PHP是一种脚本语言,当前中国乃至世界上使用php语言开发网站非常普遍。Nginx是一个web服务软件,和apache是一类软件,目前使用nginx的企业越来越多,MySQL是当前最为流行的小型关系型数据库,LNMP就是一个支持解析php程序的环境。
一,安装Mysql:
mysql初始化
# cd /usr/local/src
# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
1,解压mysql:tar zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
2,更改位置:# mv mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql
3,创建mysql用户:# useradd -s /sbin/nologin mysql
4,创建datadir,mysql数据会放在这里:#cd /usr/local/mysql
# mkdir -p /data/mysql
5,更改权限:# chown -R mysql:mysql /data/mysql
6,指定mysql用户,mysql包所在的位置,datadir数据库的安装位置:# ./bin/mysqld/ --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/mysql
配置mysql
1,配置/etc/my.cnf文件
查看/usr/local/mysql/support-files下有没有my-default.cnf文件,
若有,则复制 #cp /etc/my.cnfcp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
若没有,则vim /etc/my.cnf,编辑
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data/mysql
port = 3306
server_id = 128
socket = /tmp/mysql.sock
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
2,复制启动脚本文件并修改其属性
# cp support-files/mysql.server /etc/init.d/mysqld
# chmod 755 /etc/init.d/mysqld
3,修改启动脚本/etc/init.d/mysqld中datadir(数据库文件储存的地方)的数据
# vim /etc/init.d/mysqld
修改:datadir=/data/mysql
4,把启动脚本加入系统服务项,设定开机启动
# chkconfig --add mysqld
# chkconfig mysqld on
5,启动mysql
# service mysqld start
6,检查mysql是否启动
# ps aux |grep mysqld //结果应该大于两行
# netstat -lnp|grep 3306 //看看有没有监听3306端口
安装PHP
1,针对Nginx的php安装和针对apache的php安装是有区别的
#cd /usr/local/src
#wget http://cn2.php.net/distributions/php-5.6.39.tar.bz2
2,解压源码包# tar zxf -xjf php-5.6.39.tar.bz2
3,创建php-fpm账户用来运行php-fpm服务。PHP以一个服务php-fpm的形式出现,独立存在于linux系统中,方便管理: #useradd -s /sbin/nologin php-fpm
4,配置编译选项
#cd php-5.3.27
# ./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/tmp/mysql.sock --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-ftp --enable-mbstring --enable-exif --disable-ipv6 --with-pear --with-curl --with-openssl
5,安装库
1) yum install -y libxml2-devel
2) yum install -y openssl openssl-devel
3) yum install -y bzip2 bzip2-devel
4) yum install -y libpng libpng-devel
5) yum install -y freetype freetype-devel
6) yum install -y epel-release
7) yum install -y libmcrypt-devel
8) yum -y install libjpeg-devel
注:可能会出现的问题:1,Linux执行./configuer 编译命令报configure: WARNING: unrecognized options: ....
checking for cc… no
checking for gcc… no
解决办法:原因是缺少gcc 编译环境
有yum源的直接执行 # yum -y install gcc gcc-c++
2,编译命令报checking libxml2 install dir... no
checking for xml2-config path...
configure: error: xml2-config not found. Please check your libxml2 installation.
解决办法:检查是否安装了libxm包: # rpm -qa |grep libxml2
libxml2-2.6.26-2.1.12
libxml2-python-2.6.26-2.1.12
重新安装libxml2和libxml2-devel包
# yum install libxml2
# yum install libxml2-devel -y
安装完之后查找xml2-config文件是否存在
# find / -name "xml2-config"
/usr/bin/xml2-config
4,编译命令报Php7 configure: error: Cannot find OpenSSL’s <ev.h>
解决办法:安装opensll:# yum install -y openssl openssl-devel
5,编译命令报If configure fails try --with-vpx-dir=<DIR> configure: error: jpeglib.h not
解决办法:# yum -y install libjpeg-devel
6,编译命令报 configure:error:mcrypt.h not found. Please reinstall libmcrypt
先安装epel,再安装libmcrypt
解决办法:# yum install -y epel-release
# yum install -y libmcrypt-devel
7,安装php:# make
# make install
8,修改配置文件:# cp php.ini-production /usr/local/php/etc/php.ini
# vim /usr/local/php/etc/php.ini
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024
保存配置文件之后检查配置是否正确
# /usr/local/php/sbin/php-fpm -t
9,启动php-fpm拷贝启动脚本:# cp /usr/local/src/php-5.3.27/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
修改启动脚本权限:# chmod 755 /etc/init.d/php-fpm
启动php:# service php-fpm start
设置开机启动:# chkconfig php-fpm on
检测php-fpm是否启动:# ps aux |grep php-fpm (执行这条命令大概可以看到20多个进程)
安装nginx
Nginx官方网站(http://nginx.org)
1,下载和解压Nginx:# cd /usr/local/src/
# wget http://nginx.org/en/download.html/nginx-1.20.0
# tar zxvf nginx-1.20.0.tar.gz
2,配置编译选项:# cd nginx-1.2.9
# ./configure --prefix=/usr/local/nginx
3,编译和安装Nginx
# make
# make install
4,编写启动脚本 # vim /etc/init.d/nginx //加入内容
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
5,保存改脚本之后修改权限 # chmod 755 /etc/init.d/nginx
# chkconfig --add nginx
加入开机启动 # chkconfig nginx on
6,更改Nginx配置文件
首先把原来的配置文件清空 : # /usr/local/nginx/conf/nginx.conf (重定向单独使用,快速清空)
# vim /usr/local/nginx/conf/nginx.conf
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
'$host "$request_uri" $status'
'"$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
include vhost/*.conf;
}
7,检查配置文件是否有误:# /usr/local/nginx/sbin/nginx -t
8,启动Nginx:# service nginx start
检查Nginx是否启动:# ps aux |grep nginx
9,测试是否正确解析PHP
创建测试文件:# vim /usr/local/nginx/html/2.php //内容如下
编辑内容如下:
<?php
echo "测试php是否解析";
?>
执行如下命令测试文件: curl localhost/2.php