234rr 发表于 2016-2-18 08:35:51

LNMP搭建

安装mysql
# wgethttp://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.47-linux2.6-x86_64.tar.gz
一、解压
# tar zxf mysql-5.5.47-linux2.6-x86_64.tar.gz
# mv mysql-5.5.47-linux2.6-x86_64 /usr/local/mysql

二、建立mysql用户
# useradd -s/sbin/nologin –Mmysql

三、初始化数据库# mkdir -p /data/mysql ; chown -R mysql:mysql /data/mysql
# yum -y installlibaio-*(apt-*)
#./scripts/mysql_install_db--user=mysql --datadir=/data/mysql

四、拷贝启动脚本和配置文件
# cp support-files/my-large.cnf /etc/my.cnf
# cp support-files/mysql.server /etc/init.d/mysqld
# chmod 755 /etc/init.d/mysqld

五、修改配置文件
# vim /etc/init.d/mysqld
46basedir=/usr/local/mysql
47 datadir=/data/mysql

六、添加开机启动,启动mysql
# chkconfig --add mysqld
# chkconfig mysqld on
#service mysqld start

七、查看mysql服务和端口有没启动
# netstat –lnp
# ps aux |grep mysqld

安装php
# wget http://cn2.php.net/distributions/php-5.5.30.tar.gz


[*]解压。

#tar zxf php-5.5.30.tar.gz
#mv /usr/local/src/php-5.5.30 /usr/local/php

二、安装所需要的包
# yum -y install gcc   libxml2-*curl-devellibjpeg-*libpng-*freetype-* openssl-*(libcurl-devel)
# rpm -ivh http://www.aminglinux.com/bbs/data/attachment/forum/month_1211/epel-release-6-7.noarch.rpm

# yum install -ylibmcrypt-devel

三、创建用户
# useradd -s /sbin/nologin -M php-fpm

四、配置编译
#./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

五、进行编译和安装
#make&&make install
/usr/bin/ld: cannot find -lltdlcollect2: ld returned 1 exit statusmake: *** 错误 1解决方法:yum install -y libtool-ltdl-devel
四、修改配置文件
# cp php.ini-production /usr/local/php/etc/php.ini
[
root@hong php]# vim /usr/local/php/etc/php.ini

1933
1934 pid = /usr/local/php/var/run/php-fpm.pid
1935 error_log = /usr/local/php/var/log/php-fpm.log
1936
1937 listen = /tmp/php-fcgi.sock
1938 user = php-fpm
1939 group = php-fpm
1940 pm = dynamic
1941 pm.max_children = 50
1942 pm.start_servers = 20
1943 pm.min_spare_servers = 5
1944 pm.max_spare_servers = 35
1945 pm.max_requests = 500
1946 rlimit_files = 1024

六、复制启动脚本,开机自动加载
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod 755 /etc/init.d/php-fpm
# chkconfig php-fpm on
# mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

七、启动服务并查看端口
# service php-fpm start
# ps aux |grep php-fpm
# netstat –lnp


Nginx安装
#wget http://nginx.org/download/nginx-1.8.0.tar.gz

[*]解压

# tar zxf nginx-1.8.0.tar.gz
# cd nginx-1.8.0

二、配置编译
# yum -y install pcre-*
#./configure --prefix=/usr/local/nginx --conf-path=/usr/local/nginx/conf/nginx.conf--with-http_realip_module --with-http_sub_module --with-http_gzip_static_module--with-http_stub_status_module --with-pcre

三、编译安装
#make && make install

四、启动nginx
# /usr/local/nginx/sbin/nginx
注意:要关闭httpd。

五、配置nginx文件
# vim /usr/local/nginx/nginx.conf

65         location ~ \.php$ {
66             root         html;
67             fastcgi_pass   127.0.0.1:9000;
68             fastcgi_indexindex.php;
69fastcgi_paramSCRIPT_FILENAME/usr/local/nginx/html$fastcgi_script_name;   //这个地方如果没更改路径,访问时就会是404
70            include       fastcgi_params;
71         }

六、测试php
# cd /usr/local/nginx/html

1.编辑一个php文件
#vim info.php
<?php
phpinfo();
?>

2.检查配置文件
~# /usr/local/nginx/sbin/nginx-t

3.启动nginx服务
~# /usr/local/nginx/sbin/nginx -s reload

~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

赋予权限755
# chmod 755 /etc/init.d/nginx

添加成开机自启动
# chkconfig --add nginx
# chkconfignginxon
# service nginx restart

七、修改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{    useepoll;   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 hong '$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/htmapplication/xml;    includevhosts/*.conf;    #nginx支持默认的虚拟主机}
然后编辑虚拟主机配置文件# cd /usr/local/nginx/conf# mkdir vhosts# vim default.conf
server{   listen 80 default_server;   server_name localhost;   index index.html index.htm index.php;root /tmp/123;   //限制第一个默认主机为403deny all;}

创建限制访问的目录# mkdir /tmp/123
检查配置是否有错
# /usr/local/nginx/sbin/nginx –t

访问本地时
# curl -x127.0.0.1:80www.dshjkf.com


虚拟主机添加你需要的网站如:
# vim hong.conf

server
{
    listen 80;
    server_name www.hong.com;
    index index.html index.htmindex.php;
    root/data/www;   #网站所在路径

    location ~ \.php$ {
      include fastcgi_params;
       # fastcgi_passunix:/tmp/php-fcgi.sock;
                  fastcgi_pass127.0.0.1:9000;            #这个地方不添加端口可能会报502错误
      fastcgi_index index.php;
      fastcgi_paramSCRIPT_FILENAME/data/www$fastcgi_script_name;
    }
}
# /usr/local/nginx/sbin/nginx -t
# /etc/init.d/nginx reload

测试网站访问
# curl -x127.0.0.1:80 hong.com -I
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.8.0
Date: Sun, 13 Dec 2015 21:09:07 GMT
Content-Type: text/html
Connection: keep-alive
X-Powered-By: PHP/5.5.30
location: install


PHP-fpm配置文件
/usr/local/php/etc/php-fpm.conf   //php服务配置
/usr/local/php/etc/php.ini      //php全局配置

[*]清空配置文件

# > /usr/local/php/etc/php-fpm.conf
# 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/www.sock(可自定义)
user =php-fpm         (可自定义)
group =php-fpm         (可自定义)
pm = dynamic         //动态加载管理以下模块
pm.max_children= 50   //子进程最大50
pm.start_servers= 20      //刚开始启动20
pm.min_spare_servers= 5    //最小5
pm.max_spare_servers= 35   //最多
pm.max_requests= 500      //在生命周期之类处理多少个请求,自动销毁
rlimit_files= 1024         //每一个进程描述的限制
slowlog = /tmp/www_slow.log//查找网站速度慢
request_slowlog_timeout = 1//超时1分钟
php_admin_value=/data/www/:/tmp/ //多个路径用:冒号隔开


建立多个pool池子(可选)

listen =/tmp/php-fcgi1.sock(可自定义)
user =php-fpm         (可自定义)
group =php-fpm         (可自定义)
pm = dynamic         //动态加载管理以下模块
pm.max_children= 50   //子进程最大50
pm.start_servers= 20      //刚开始启动20
pm.min_spare_servers= 5    //最小5
pm.max_spare_servers= 35   //最多
pm.max_requests= 500      //在生命周期之类处理多少个请求,自动销毁
rlimit_files= 1024         //每一个进程描述的限制

检查是否有错
# /usr/local/php/sbin/php-fpm -t

重启服务
# /etc/init.d/php-fpm restart

建立池子的好处:可以把权限分开,不同域名在不同池子也可以到同一个池子;其次如果一个池子挂了,整个池子的网站都挂了,分开多个池子网站可以分开


解决nginx502
1.编辑虚拟配置文件
# vim /usr/local/nginx/conf/vhosts/hong.conf

server
{
    listen 80;
    server_name www.hong.com;
    index index.html index.htmindex.php;
    root /data/www;

    location ~ \.php$ {
      include fastcgi_params;
      fastcgi_pass unix:/tmp/www.sock;
      #fastcgi_pass127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_paramSCRIPT_FILENAME /data/www$fastcgi_script_name;
    }
}

检查错误
# /usr/local/nginx/sbin/nginx -t
重新启动或加载配置文件
# /etc/init.d/nginx reload

访问出现502,排查步骤:
1.查看错误日志
# cat /usr/local/nginx/logs/nginx_error.log
2.查看sock文件权限,权限的属主和属组都是root 并且只有读写权限,而nginx的用户是nobody

# ll -d /tmp/php-fcgi.sock
srw-rw---- 1 root root 0 12月 14 05:26/tmp/php-fcgi.sock

3.所以要修改配置。进入php配置文件
# vim /usr/local/php/etc/php-fpm.conf
6 user = php-fpm7 group = php-fpm
8 listen.owner = nobody      //监听的用户
9 listen.group = nobody         //监听的组
(listen.mode =444)
4.启动php服务
# /usr/local/php/sbin/php-fpm -t
NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test issuccessful

# /etc/init.d/php-fpm restart
Gracefullyshutting down php-fpm . done
Startingphp-fpmdone

5.在访问域名看能否访问www.hong.com

页: [1]
查看完整版本: LNMP搭建