12.1-12.6 LNMP介绍,mysql,php,nginx安装。
LNMP
LNMP指的是linux,nginx,mysql和php。其中nginx取代了apache的作用。nginx相比于apache,他在静态资源的支持方面,速度比apache更快,且支持的并发访问量也更大,所以nginx的流行速度非常快。虽然目前市场份额并没有超过apache。
nginx也是开源软件,淘宝根据其业务特点在nginx的基础上开发了tengine用于阿里的业务。tengine在安全和限速方面表现突出,同时也支持js,css和图片的合并,可以减少请求数,提高网站的性能。
相比于LNMP,php作为apache的一个模块存在,他们必须安装在同一台主机上。但是LNMP的架构不一样,php作为一个独立的模块存在,负责连接mysql与nginx之间的通信。
一般来说,apache的主机只能支持4000个并发连接,但是nginx可以支持2-3万的并发连接。nginx在静态资源的支持上,速度比apache快,但是动态资源的支持还是不如apache。
mysql安装
cd /usr/local/src
wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql //二进制包移到local/mysql目录下
cd /usr/local/mysql
useradd mysql
mkdir /data/mysql
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
vim /etc/init.d/mysqld //定义basedir和datadir
chkconfig --add mysqld //添加服务
chkconfig mysqld on //开机启动
service mysqld start //开启服务
ps aux|grep mysqld //查看进程是否已开启
php安装
cd /usr/local/src/
wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
tar zxf php-5.6.30.tar.gz
useradd php-fpm
cd php-5.6.30
./configure --prefix=/usr/local/php-fpm --with-config-file-path=/usr/local/php-fpm/etc //安装路径是/local/php-fpm,也多了一个--enable-fpm,不然不会有php-fpm文件生成
--enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-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 --with-pear --with-curl --with-openssl
//出现报错
yum install libcurl libcurl-devel -y
echo $?
make
echo $?
make install
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm //复制启动脚本
chmod 755 /etc/init.d/php-fpm
cp php.ini-production /usr/local/php-fpm/etc/php.ini
vim /usr/local/php/etc/php-fpm.conf
//添加以下内容
[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-fpm/sbin/php-fpm -t //测试配置是否正确
chkconfig --add php-fpm
chkconfig php-fpm on
service php-fpm start
ps aux |grep php-fpm //查看php-fpm是否已开启
安装nginx
nginx的版本更新很快,但是建议安装最近的stable版。
cd /usr/local/src
wget http://nginx.org/download/nginx-1.12.1.tar.gz
tar zxf nginx-1.12.1.tar.gz
./configure --prefix=/usr/local/nginx
make
make install
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
chmod 755 /etc/init.d/nginx //更改启动脚本的权限
chkconfig --add nginx
chkconfig nginx on
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;
}
}
}
/usr/local/nginx/sbin/nginx -t //测试配置是否正确
service nginx start
ps aux|grep nginx
测试php是否正确解析
vim /usr/local/nginx/html/2.php
//添加如下内容
<?php
echo "test php";
?>
curl localhost/2.php
test php //显示这条内容说明php解析成功