lnmp搭建与调优
lnmp搭建与调优LNMP 代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
LNMP和LAMP不同的是LNMP中的N指的是Nginx(类似于Apache的一种web服务软件)其他都一样。目前这种环境应用的也是非常之多。Nginx设计的初衷是提供一种快速高效多并发的web服务软件。在静态页面的处理上Nginx的确胜Apache一筹,然而在动态页面的处理上Nginx并不比Apache有多少优势。但是,目前还是有很多爱好者对Nginx比较热衷,随着Nginx的技术逐渐成熟,它在web服务软件领域的地位越来越高。
一,安装mysql
1.下载mysql到/usr/local/src/
mkdir -p /usr/local/src
cd /usr/local/src
wget http://mirrors.sohu.com/mysql/MySQL-5.1/mysql-5.1.73-linux-i686-glibc23.tar.gz
2.解压
tar zxvf mysql-5.1.73.tar.gz
3.把mysql数据移动到/usr/local/mysql
mv mysql-5.1.73 /usr/local/mysql
4.建立mysqlyonghu
useradd -s /sbin/nologin mysql
5.初始化mysql,创建myql安装路径,并赋予mysql权限
cd /usr/local/mysql
mkdir -p /data/mysql
chown -R mysql.mysql /data/mysql
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
--user 定义数据库的所属主, --datadir 定义数据库安装到哪里,建议放到大空间的分区上,这个目录需要自行创建。这一步骤很关键,如果你看到两个 “OK” 说明执行正确,否则请仔细查看错误信息.
6.拷贝配置文件
cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
7.拷贝启动脚本,并修改属性
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
8.修改启动脚本的数据
vim /etc/init.d/mysqld
需要修改的地方有 datadir=/data/mysql
9.把脚本加入到启动项,并开机启动
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
10.安装配置好后,可以查看程序是否启动或者启动的端口
pa aux |grep mysqld
netstart -ntlp |grep mysqld
注意:如果启动不了,可以查看/data/mysql下面的错误日志,还可以在安装的时候,使用echo $?检验安装是否正确。
二.安装php
注意:在安装之前,先安装php所依赖的包
yum install -y openssl openssl-devel pcre pcre-devel libpng libpng-devel libjpeg libjpeg-devel freetype freetype-devel gcc gcc-c++ libmcrypt libmcrypt-devel libxml2 libxml2-devel curllibcurl libcurl-devel
解决办法一
1、安装第三方yum源
wget http://www.atomicorp.com/installers/atomic
sh ./atomic
2、使用yum命令安装
yuminstallphp-mcryptlibmcryptlibmcrypt-devel
1.下载php包到/usr/local/src
wget http://am1.php.net/distributions/php-5.3.27.tar.gz
2.解压包,并创建相关账户
tar zxvf php.5.3.27.tar.gz
useradd -s /sbin/nologin php-fpm
3.配置编译参数
cd php-5.3.27
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/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 \
--enable-zend-multibyte \
--disable-ipv6 \
--with-pear \
--with-curl \
--with-openssl
注意:在编译参数的时候,遇到什么问题,请根据提示相应解决。
4.编译php和安装php
make
make install
5.在编辑、编译和安装php时,用echo $? 检查配置时候有错误
6.修改配置文件
cp php.ini-production /usr/local/php/etc/php.ini
vim /usr/local/php/etc/php-fpm.conf
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
listen = /tmp/php-fcgi.sock
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
7.启动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
service php-fpm start
8.设置开机启动,并检查是否启动
chkconfig php-fpm on
ps aux |grep php-fpm
注意这里的难点就是编译安装的时候容易出现问题,但根据问题和日志也很容易解决的。
安装nginx
1.下载nginx到/usr/local/src
cd /usr/local/src
wget http://nginx.org/download/nginx-1.4.4.tar.gz
2.解压,并编译参数
tar zxvf nginx-1.4.4.tar.gz
cd nginx-1.4.4
./configure \
--prefix=/usr/local/nginx \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_stub_status_module\
--with-pcre
3.编译nginx和安装nginx
make
make install
4.编写nginx启动脚本,并加入到系统系统服务
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
5.更改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;
}
}
}
6.启动nginx
service nginx start
查看服务是否启动了
ps aux |grep nginx
7.测试是否解析php文件
vim /usr/local/nginx/html/2.php
<?php
phpinfo();
?>
8.通过网页进行访问http://192.168.103.104/2.php
配置文件更改
1.改写配置文件
vim /usr/local/nginx/conf/nginx.conf
可以把server的删除,添加为
include vhosts/*./conf;
在/usr/local/nginx/conf/创建vhosts目录
mkdir -p /usr/local/nginx/conf/vhosts
touch default.conf
2.更改default.conf文件
server
{
listen 80 default_server;
server_name localhost;
index index.html index.htm index.php;
root /tmp/123;
deny all;
}
3.创建/tmp/123文件
mkdir /tmp/123
4.检查配置是否有错误
/usr/local/nginx/sbin/nginx -t
service nginx reload
5.重新建立新的文件
vim /usr/local/nginx/conf/vhosts/xcb.conf
server
{
listen 80;
server_name xcb.com;
index index.html index.htm index.php;
root /data/www;
location ~ \.php$ {
include fastcgi_params;
#fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
si
}
6.检查配置文件
/usr/local/nginx/sbin/nginx -t
service nginx reload
7.建立/data/www目录,并吧站点放在该目录下面,
mkdir -p /data/www
8.在访问的时候,如遇到权限的问题,请根据日志错误解决。
页:
[1]