Press "Enter" to skip to content

RedHat5.5配置安装lnmp全过程(Linux+Nginx+Mysql+PHP)

一,安装必要的库和相关软件

# rpm -e mysql-server php-mysql php perl-DBD-MySQL mysql httpd
# yum -y install gcc gcc-c++ autoconf libtool* ncurses-devel pcre-devel zlib-devel libxml2-devel libpng-devel libjpeg-devel-6b freetype-devel gd-devel curl-devel libmcrypt-devel bison

# wget http://www.cmake.org/files/v2.8/cmake-2.8.4.tar.gz   #下载cmake
# tar -zxvf cmake-2.8.4.tar.gz
# cd cmake-2.8.4
# ./configure && make && make install

二,安装mysql

添加mysql用户及用户组
# groupadd mysql && useradd -r -g mysql mysql -s /sbin/nologin -d /dev/null

解压并安装
# tar zxvf mysql-5.5.27.tar.gz && cd mysql-5.5.27
# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock  #最新版mysql已经使用cmake进行配置
# make && make install            #需要10分钟时间
# chown -R mysql:mysql /usr/local/mysql
# cd /usr/local/mysql/
# scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data &
# chown -R mysql:mysql /usr/local/mysql/data

配置mysql
# cp support-files/my-medium.cnf /etc/my.cnf && vi /etc/my.cnf
在[client]区域添加一行default-character-set=utf8
在[mysqld]区域添加如下两行
character-set-server=utf8
collation-server=utf8_general_ci

将mysql加入系统服务
# cp support-files/mysql.server /etc/init.d/mysqld
# chkconfig --add mysqld && chkconfig --level 2345 mysqld on
# service mysqld restart

一些基本设置
# /usr/local/mysql/bin/mysqladmin -u root password 'new-password'  #修改mysql root密码
# /usr/local/mysql/bin/mysql -u root -p  #进入控制台
# cp /usr/local/mysql/bin/mysql /usr/bin  #方便以后直接调用mysql命令
# cp /usr/local/mysql/bin/mysqladmin /usr/bin
# export PATH=$PATH:/usr/local/mysql/bin

三,安装Nginx

添加nginx用户及用户组
# groupadd nginx && useradd -r -g nginx nginx -s /sbin/nologin -d /dev/null

解压并安装
# tar -zxvf nginx-1.2.2.tar.gz && cd nginx-1.2.2
# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx
# make && make install
# chown -R nginx:nginx /usr/local/nginx

将nginx加入系统服务
把"nginx启动脚本"复制至/root/nginx文件中
# chmod +x /root/nginx
# cp /root/nginx /etc/init.d/
# chkconfig --add nginx && chkconfig --level 2345 nginx on
# service nginx restart

四,安装php

添加php-fpm用户及用户组
# groupadd php && useradd -r -g php php -s /sbin/nologin -d /dev/null

解压并安装
安装前需要确认执行了export PATH=$PATH:/usr/local/mysql/bin,否则会报错
# tar -zxvf php-5.3.19.tar.gz && cd php-5.3.19
# ./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=php --with-fpm-group php --with-config-file-path=/etc/php.ini --with-zlib --with-gd --enable-mbstring --with-mysql=/usr/local/mysql --with-mysqli --with-pdo-mysql=/usr/local/mysql --enable-sockets --enable-sqlite-utf8 --enable-zip
# make && make install   #需要5分钟时间

复制主配置文件  
# cp /root/php-5.3.19/php.ini-production /etc/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
 
服务脚本  
# cp /root/php-5.3.19/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm  
# chmod +x /etc/rc.d/init.d/php-fpm   
 
修改php-fpm配置文件   
# vim /usr/local/php/etc/php-fpm.conf  
pid = run/php-fpm.pid  
pm.max_children = 50   最多多少子进程  
pm.start_servers = 5   刚开始启动多少进程  
pm.min_spare_servers = 2  最少多少空闲进程  
pm.max_spare_servers = 8  最多多少空闲进程  

# service php-fpm start   #如果提示pid错误,再执行一次即可

五,配置nginx(支持php,添加pathinfo)

# vi /usr/local/nginx/conf/fastcgi_params 添加下面一行:
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

# vi /usr/local/nginx/conf/nginx.conf  
启用下面几行(去掉#号)
    location ~ \.php$ {  
                root           html;  
                fastcgi_pass   127.0.0.1:9000;  
                fastcgi_index  index.php;  
                fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
                include        fastcgi_params;  
            } 
找到找到 server {……} 段内的 location / {……} 段(第45行),将 index 后的默认首页文件添加一个 index.php,形成如下所示的格式
index  index.php index.html index.htm;

# service nginx restart

测试配置
vi /usr/local/nginx/html/index.php并添加下面内容
[code lang=”php”]


在浏览器里打开http://localhost/能显示出来内容,说明配置OK!

参考文章
http://songy.sinaapp.com/main/article-629
http://peaceweb.blog.51cto.com/3226037/930286
http://www.j8.mu/article-11.html
http://www.cnblogs.com/CosX/archive/2011/08/28/2155889.html

Leave a Reply

Your email address will not be published. Required fields are marked *