天天看点

linux下搭建LNMP平台

yum安装

由于涉及到yum安装以下为yum的安装配置方法(直接挂载的本地镜像,也可以将镜像盘中的文件拷到其他盘中)

mount /dev/cdrom /media

rpm -ivh /media/Server/yum-

yum clean

vim /etc/yum.repos.d/rhel_local.repo

[rhel]

name=local ##随便起

baseurl=file:///media/Server

enabled=1

gpgcheck=0 ##1为启用验证,0为不启用

完成之后就可以yum安装了

yum -y install安装

yum -y remove卸载

预安装依赖包

yum -y installlibjpeg-devel libpng-devel libtiff-devel fontconfig-devel freetype-devellibXpm-devel gettext-devel openssl-devel libtool-ltdl-devel gcc* openssl*libxml*

mysql的安装

tar xvf mysql.tar.gz

cd mysql

./configure --prefix=/usr/local/mysql--with-charset=utf8 --with-extra-charsets=all && make && make install

groupadd mysql

useradd mysql -s /sbin/nologin -g mysql

/usr/local/mysql/bin/mysql_install_db--user=mysql

cd /usr/local/mysql

chown -R root:mysql .

cp share/mysql/my-medium.cnf /etc/my.cnf

cp share/mysql/mysql.server/etc/rc.d/init.d/mysqld

chmod +x /etc/rc.d/init.d/mysqld

/etc/init.d/mysqld restart

chmod 755 /etc/rc.d/init.d/mysqld

/etc/rc.d/init.d/mysqld start 如果启动失败报错则是权限问题以及初始化失败造成

需重复/usr/local/mysql/bin/mysql_install_db --user=mysql

查看报错视情况而定my.cnf配置如下:

[mysqld]

datadir=/usr/local/mysql/data

socket=/var/mysql.sock

[mysql]

socket=/tmp/mysql.sock

[mysql.server]

user=mysql

basedir=/usr/local/mysql

[safe_mysqld]

err-log=/usr/local/mysql/mysqld.log

pid-file=/usr/local/mysql/mysqld.pid

根据日志文件显示,首先检查运行权限,再看一下/var/mysql.sock,发现/var/mysql.sock目录下并没有该文件,是否是没有权限写/var目录?OK,那就用root用户运行一下safe_mysqld,启动正常。使用mysql连接数据库,提示错误,连接不到数据库服务器,/tmp目录下没有mysql.sock这个文件,到这里,基本清楚了Mysql客户端和服务器端在本机的通信方式了,通过一个名为mysql.sock文件来进行初始化通信的,因此该文件所存放的目录必须双方都有权限可以访问到,而且服务器在启动时需要写该文件,如果my.cnf中这两项配置不正确,就会出现Permissiondenied等类似错误。将配置文件修改如下,就没有问题了。

如果还是不行,那就需要chmod 777 /tmp

2 nginx的安装

安装依赖文件

安装openssl-0.9.8i.tar.gz软件包

./config --prefix=/usr/local/openssl

make &&make install

安装pcre-8.12.tar.gz软件包(可以安装光盘里面pcre和pcre-devel)

./configure --prefix=/usr/local/pcre

安装nginx软件包

/usr/sbin/groupadd nginx

/usr/sbin/useradd -g nginx nginx -s /sbin/nologin

tarzxvf nginx.tar.gz

./configure --prefix=/usr/local/nginx--with-http_stub_status_module --with-http_ssl_module && make && make install

验证nginx服务安装成功

/usr/local/nginx/sbin/nginx -t

启动nginx服务

/usr/local/nginx/sbin/nginx

查看启动状态

netstat -tlupn |grep 80

启动nginx的脚本

3 php的安装

安装之前先安装几个库 libiconv(--prefix=/usr/local/libiconv) libmcrypt mhashspawn-fcgi libevent gd(--prefix=/usr/local/gd2)

libiconv用于实现一个字符编码到另一个字符编码的转换

libmcrypt可以使php支持更多加密算法

mhash是一个哈稀演函数库,可以支持多种哈希演算法

spawn-fcgi原本是lighttpd的一个用来控制php-cgi的工具,现在已从lighttpd中独立出来成为一个开源项目。我们可以用它来启动,控制,管理php-cgi的进程。使用它有诸多好处,比如我们可以将运行php-cgi的主机与Nginx分离开来,以减轻Nginx服务器的负担,或者用来扩展架构,做更多的负载分担。减少进程的重复创建所带来的性能消耗等等

libevent 是一个事件触发的网络库,适用于windows、linux、bsd等多种平台,内部使用select、epoll、kqueue等系统调用管理事件机制

spawn-fcgi制定安装路径/usr/local下

编译php

./configure --prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir--with-freetype-dir --with-jpeg-dir --with-png-dir --with-gd=/usr/local/gd2/--with-zlib --with-libxml-dir --with-curl --with-curlwrappers --with-openssl--with-mhash --with-xmlrpc --with-mcrypt --enable-xml --enable-bcmath--enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex--enable-mbstring --enable-gd-native-ttf --enable-ftp --enable-pcntl--enable-sockets --enable-zip --enable-fpm--disable-debug --disable-ipv6 --with-iconv=/usr/local/libiconv

安装php扩展模块工具

(1)安装memcache

tarxvf memcache-3.0.4.tgz

cdmemcache-3.0.4

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-configmake && make install

安装eaccelerator 优化php

tar -jxvf eaccelerator-0.9.6.tar.bz2

cd eaccelerator-0.9.6.1/

./configure --enable-eaccelerator=shared--with-php-config=/usr/local/php/bin/php-config

添加PHP配置文件

cp php.ini-production /usr/local/php/etc/php.ini

vi /usr/local/php/etc/php.ini

添加date.timezone = Asia/Shanghai

更改short_open_tag = Off 为short_open_tag = On (开启短格式支持)

更改expose_php = on 为expose_php = off (在curl中隐藏php版本号)

添加php-fpm配置文件

cp /usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf

vim /usr/local/php/etc/php-fpm.conf 取消以下;分号表示注释

;pid = run/php-fpm.pid

;pm.start_servers = 20

;pm.min_spare_servers = 5

;pm.max_spare_servers = 35

设置php-fpm启动文件

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chkconfig --add php-fpm

chkconfig --level 3 php-fpm on

chmod +x /etc/init.d/php-fpm

/etc/init.d/php-fpm start

Starting php-fpm done

接下来整合nginx和php

以下为nginx的配置文件

vim /usr/local/nginx/conf/nginx.conf

#user nginx nginx;

worker_processes 2;

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

#pid logs/nginx.pid;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

server_names_hash_bucket_size 128;

client_header_buffer_size 16k;

large_client_header_buffers 4 256k;

server_tokens off;

#sendfile on;

tcp_nopush off;

tcp_nodelay on;

keepalive_timeout 60;

client_header_timeout 30;

client_body_timeout 30;

send_timeout 30;

client_max_body_size 30M;

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '

# '$status $body_bytes_sent "$http_referer" '

# '"$http_user_agent" "$http_x_forwarded_for"';

#access_log logs/access.log main;

# sendfile on;

# tcp_nopush off;

# tcp_nodelay on;

# keepalive_timeout 60;

gzip on;

gzip_min_length 1000;

gzip_buffers 4 16k;

#gzip_http_version 1.0;

gzip_comp_level 9;

gzip_types text/plain text/javascript application/x-javascript text/css application/xml text/xml application/xml+rss;

gzip_vary on;

fastcgi_intercept_errors on;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for" "$request_time"';

#error_page 500 502 503 504 /50x.html;

#error_page 402 403 404 500 502 503 504 http://www.test.com/application/controllers/error.php;

error_page 402 403 502 503 504 /error;

server {

listen 80;

server_name www.dz.dfbs.com;

index index.html index.htm index.php;

#charset koi8-r;

#access_log logs/host.access.log main;

location ~ .*.(php|php5)?$

{

#fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fcgi.conf;

location / {

root html;

index index.html index.htm;

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

# proxy the PHP scripts to Apache listening on 127.0.0.1:80

#location ~ \.php$ {

# proxy_pass http://127.0.0.1;

#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

# root html;

# fastcgi_pass 127.0.0.1:9000;

# fastcgi_index index.php;

# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

# include fastcgi_params;

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#location ~ /\.ht {

# deny all;

# another virtual host using mix of IP-, name-, and port-based configuration

#server {

# listen 8000;

# listen somename:8080;

# server_name somename alias another.alias;

# location / {

# root html;

# index index.html index.htm;

# }

# HTTPS server

# listen 443;

# server_name localhost;

# ssl on;

# ssl_certificate cert.pem;

# ssl_certificate_key cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;

# ssl_ciphers HIGH:!aNULL:!MD5;

# ssl_prefer_server_ciphers on;