LNMP环境的基础搭建,即Linux、Nginx、MySQL、PHP环境

LNMP环境的基础搭建,即Linux、Nginx、MySQL、PHP环境



注意:#用“//---//” 标记的部分不要。


参考内容:
lnmp环境搭建完全手册(四)——lnmp搭建(源码安装)
http://blog.csdn.net/stuartjing/article/details/8124491




nginx的安装
http://www.cnblogs.com/kingtigerhu/articles/2271270.html




nginx php-fpm安装配置
http://linux.it.net.cn/e/server/nginx/2014/1208/9850.html





#查看linux版本
[root@localhost ~]# lsb_release -a
LSB Version:    :core-3.1-ia32:core-3.1-noarch:graphics-3.1-ia32:graphics-3.1-noarch
Distributor ID: CentOS
Description:    CentOS release 5.5 (Final)
Release:        5.5
Codename:       Final


#尝试用yum安装nginx,结果没有。
[root@localhost ~]# yum -y install nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * addons: mirrors.skyshe.cn
 * base: mirrors.skyshe.cn
 * extras: mirrors.163.com
 * updates: mirrors.163.com
addons                                                   | 1.9 kB     00:00     
base                                                     | 1.1 kB     00:00     
extras                                                   | 2.1 kB     00:00     
updates                                                  | 1.9 kB     00:00     
Setting up Install Process
No package nginx available.
Nothing to do


[root@localhost ~]# 




nginx: Linux packages
http://nginx.org/en/linux_packages.html#stable


选择CentOS 5的stable version


[root@localhost ~]# cd /mnt/hgfs/sharefiles/nginx
[root@localhost nginx]# ls
nginx-1.9.4.tar.gz  nginx-release-centos-5-0.el5.ngx.noarch.rpm
[root@localhost nginx]# rpm -ivh nginx-release-centos-5-0.el5.ngx.noarch.rpm 
warning: nginx-release-centos-5-0.el5.ngx.noarch.rpm: Header V3 RSA/SHA1 signature: NOKEY, key ID 7bd9bf62
Preparing...                ########################################### [100%]
   1:nginx-release-centos   ########################################### [100%]
[root@localhost nginx]# which nginx
/usr/bin/which: no nginx in (/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root@localhost nginx]# 


[root@localhost nginx-1.9.4]# ./configure


./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.




[root@localhost nginx-1.9.4]# yum -y install pcre
Updated:
  pcre.i386 0:6.6-9.el5                                                         


Complete!


[root@localhost nginx-1.9.4]# ./configure


./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.




nginx的安装
http://www.cnblogs.com/kingtigerhu/articles/2271270.html


[root@localhost nginx-1.9.4]# yum -y install pcre-devel
Installed:
  pcre-devel.i386 0:6.6-9.el5                                                   


Complete!




[root@localhost nginx-1.9.4]# yum -y install openssl-devel


....


Dependency Updated:
  e2fsprogs.i386 0:1.39-37.el5                                                  
  e2fsprogs-libs.i386 0:1.39-37.el5                                             
  krb5-libs.i386 0:1.6.1-80.el5_11                                              
  krb5-workstation.i386 0:1.6.1-80.el5_11                                       
  libselinux.i386 0:1.33.4-5.7.el5                                              
  libselinux-python.i386 0:1.33.4-5.7.el5                                       
  libselinux-utils.i386 0:1.33.4-5.7.el5                                        
  openssl.i686 0:0.9.8e-36.0.1.el5_11                                           
  zlib.i386 0:1.2.3-7.el5                                                       


Complete!




[root@localhost nginx-1.9.4]# ls
auto     CHANGES.ru  configure  html     Makefile  objs    src
CHANGES  conf        contrib    LICENSE  man       README




[root@localhost nginx-1.9.4]# ./configure --prefix=/usr/local/nginx




[root@localhost nginx-1.9.4]# make




[root@localhost nginx-1.9.4]# make install


[root@localhost nginx-1.9.4]# ls /usr/local/nginx/
conf  html  logs  sbin
[root@localhost nginx-1.9.4]# /usr/local/nginx/sbin/nginx


[root@localhost nginx-1.9.4]# netstat -antlp | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      12310/nginx         
tcp        0      0 :::22                       :::*                        LISTEN      3804/sshd   
        
[root@localhost nginx-1.9.4]# 




#浏览器
http://127.0.0.1




网页显示:
Welcome to nginx!


If you see this page, the nginx web server is successfully installed and working. Further configuration is required.


For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.


Thank you for using nginx.




[root@localhost /]# yum install mysql-server php php-devel php-mysql


Installed:
  mysql-server.i386 0:5.0.95-5.el5_9      php.i386 0:5.1.6-45.el5_11           
  php-devel.i386 0:5.1.6-45.el5_11        php-mysql.i386 0:5.1.6-45.el5_11     


Dependency Installed:
  mysql.i386 0:5.0.95-5.el5_9            perl-DBD-MySQL.i386 0:3.0007-2.el5     
  perl-DBI.i386 0:1.52-2.el5             php-cli.i386 0:5.1.6-45.el5_11         
  php-common.i386 0:5.1.6-45.el5_11      php-pdo.i386 0:5.1.6-45.el5_11         


Complete!


#首次启动mysql
[root@localhost /]# service mysqld start
Initializing MySQL database:  Installing MySQL system tables...
OK
Filling help tables...
OK


To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system


PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'


Alternatively you can run:
/usr/bin/mysql_secure_installation


which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.


See the manual for more instructions.


You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &


You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl


Please report any problems with the /usr/bin/mysqlbug script!


The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
                                                           [  OK  ]
Starting mysqld:                                           [  OK  ]
You have mail in /var/spool/mail/root




#mysql数据库端口
[root@localhost /]# netstat -antlp ¦ grep 3306
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      17863/mysqld        
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      3565/portmap        
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      12310/nginx         
tcp        0      0 0.0.0.0:821                 0.0.0.0:*                   LISTEN      3607/rpc.statd      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      3813/cupsd          
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      3914/sendmail: acce 
tcp        0      1 192.168.3.151:38521         74.125.203.190:443          SYN_SENT    12368/firefox       
tcp        0      0 :::22                       :::*                        LISTEN      3804/sshd           
tcp        0      0 ::ffff:192.168.3.151:22     ::ffff:192.168.3.10:55532   ESTABLISHED 4672/1              


#查看nginx配置
[root@localhost /]# cat /usr/local/nginx/conf/nginx.conf


#配置主机名
[root@localhost /]# vi /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1               localhost.localdomain localhost
::1             localhost6.localdomain6 localhost6
192.168.3.151   localhost.localdomain localhost


~
~
"/etc/hosts" 8L, 238C written


[root@localhost /]# 


#重启nginx
[root@localhost sbin]#  /usr/local/nginx/sbin/nginx -s reload


#重启nginx失败了,只好重启linux
[root@localhost /]# reboot




[root@localhost /]# 




#先检查一下是否有问题
[root@localhost ~]# /usr/local/nginx/sbin/nginx  -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful








//-----------------------------------------------------------------//
#编写nginx的启动脚本,并加入系统服务
#这个文件后来删掉了,因为发现随系统启动后,stop失败,也无法重启。只能reboot系统才行。




[root@localhost ~]# vi /etc/init.d/nginx


编写nginx的启动脚本,并加入系统服务
vi /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/var/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








保存后,更改/etc/init.d/nginx的权限
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on




//-----------------------------------------------------------------//


#启动nginx
[root@localhost ~]# service nginx start
Starting Nginx:                                            [  OK  ]


[root@localhost ~]# netstat -antlp | grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4724/nginx.conf 




#如果启动不了,请到/usr/local/nginx/logs/目录下查看nginx_error.log这个日志文件。若是没有这个日志文件,很有可能是那个目录没有写权限,请执行


chmod +w /usr/local/nginx/logs/
service  nginx  restart


[root@localhost ~]# chmod +w /usr/local/nginx/logs/






//-----------------------------------------------------------------//
#发现问题,nginx启动后网页无法访问
#关键文件要备份啊,幸好我有个备份nginx (copy).conf,后来是采用还原nginx.conf这个文件,nginx就恢复了。


[root@localhost local]# cd /usr/local/
[root@localhost local]# cat error.log 


#解决 connect() to unix:/var/run/php-fpm/php-fpm.sock failed (2: No such file or directory) 问题


[root@localhost local]# yum -y install gcc automake autoconf libtool make


[root@localhost local]# yum -y install gcc gcc-c++ glibc




[root@localhost local]# yum -y install libmcrypt-devel mhash-devel libxslt-devel \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel \
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel \
ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel \
krb5 krb5-devel libidn libidn-devel openssl openssl-deve


#php版本
[root@localhost sharefiles]# php -v
PHP 5.1.6 (cli) (built: Nov  6 2014 12:25:33) 
Copyright (c) 1997-2006 The PHP Group
Zend Engine v2.1.0, Copyright (c) 1998-2006 Zend Technologies


#想打个补丁,不过失败了
[root@localhost sharefiles]# gzip -cd php-5.2.17-fpm-0.5.14.diff.gz 
//-----------------------------------------------------------------//








php-fpm介绍及配置
http://blog.csdn.net/tonyxf121/article/details/7869187




#php-fpm配置


从php5.3.3版本开始就已经集成了fpm,不再是第三方的包了,如果使用的是php5.3.3以前的版本就得自己去安装fpm包了,对于php5.2.x的版本,fpm的配置文件还是xml格式,php5.3.x开始fpm配置文件


就已经支持ini格式了。




tar用法:
.tar.gz     格式解压为          tar   -zxvf   xx.tar.gz


.tar.bz2   格式解压为          tar   -jxvf    xx.tar.bz2


[root@localhost local]# 


#不打升级包了,直接更新php




[root@localhost sharefiles]# tar -jxvf php-5.6.13.tar.bz2 


[root@localhost sharefiles]#ls


[root@localhost sharefiles]#cd php-5.6.13


#一开始我是用./configure 和 make && make install安装,后来发现不行。
#./configure 后面一定要带参数


[root@localhost php-5.6.13]# mkdir /etc/php /etc/php5.d
[root@localhost php-5.6.13]# cp php.ini-production /etc/php
[root@localhost php-5.6.13]#  chmod a+x /etc/init.d/php-fpm


[root@localhost php-5.6.13]# ./configure --prefix=/usr/local/php  --enable-fpm --with-mcrypt \
--enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath \
--enable-inline-optimization --with-bz2  --with-zlib --enable-sockets \
--enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex \
--with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli \
--with-gd --with-jpeg-dir


[root@localhost php-5.6.13]# make all install




nginx 服务器重启命令,关闭 
http://blog.chinaunix.net/uid-24960107-id-341855.html






[root@localhost ~]# cd /usr/local/nginx/


[root@localhost nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp


[root@localhost nginx]# cd sbin


[root@localhost sbin]# ls
nginx
[root@localhost sbin]# ./nginx 


[root@localhost sbin]# netstat -antlp ¦ grep 80
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      3564/portmap        
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      4339/nginx          
tcp        0      0 0.0.0.0:820                 0.0.0.0:*                   LISTEN      3606/rpc.statd      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      3812/cupsd          
tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      3912/sendmail: acce 
tcp        0      0 :::22                       :::*                        LISTEN      3803/sshd           
tcp        0    596 ::ffff:192.168.3.151:22     ::ffff:192.168.3.10:52560   ESTABLISHED 4293/1              




#停止niginx
[root@localhost sbin]# ./nginx -s stop


#
[root@localhost sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
 










下面是对php-fpm运行用户进行设置


cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc/php-fpm.conf  
修改
user = oracle
group = oracle


如果oracle用户不存在,那么先添加oracle用户
groupadd oracle
useradd -g oracle oracle




修改nginx配置文件以支持php-fpm


nginx安装完成后,修改nginx配置文件为:nginx.conf 


其中server段增加如下配置,注意标红内容配置,否则会出现No input file specified.错误


# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}






#以防以后忘记了,这里把server这段内容贴出来
##开始
   server {
        listen       80;
        server_name  localhost;


        #charset koi8-r;


        #access_log  logs/host.access.log  main;


        location / {
            root   html;
            index  index.html index.htm;   (这里漏了加index.php,不过php页面也正确显示出来了。)




# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}




        }


##结束








启动php-fpm和nginx


#重启nginx
[root@localhost nginx]# ./sbin/nginx  -s stop
[root@localhost nginx]# ./sbin/nginx




#找php-fpm目录
[root@localhost php]# ls
bin  etc  include  lib  man  php  php.ini  sbin  var
[root@localhost php]# cd /usr/local/php/
[root@localhost php]# ls
bin  etc  include  lib  man  php  php.ini  sbin  var
[root@localhost php]# cd sbin/
[root@localhost sbin]# ls
php-fpm


#启动php-fpm
[root@localhost sbin]# ./php-fpm 
[root@localhost sbin]# 




浏览器:http://127.0.0.1/index.php
页面显示:PHP Version 5.6.13






mysql 的设置和LAMP中的设置一样,设置用户密码,删除匿名用户。
测试PHP与mysql连接。
这里就不再重复了。








不过以后要记得LNMP需要启动的服务:


./nginx
./php-fpm
/etc/init.d/mysqld start (mysql登录: mysql -u root)




结束测试:

[root@localhost ~]# halt


Broadcast message from root (pts/4) (Fri Sep 11 06:48:59 2015):


The system is going down for system halt NOW!




总结:

1. 手动启服务

#启动mysql
[root@localhost /]# service mysqld start

#nginx启动
[root@localhost sbin]# ./nginx 


#停止niginx

[root@localhost sbin]# ./nginx -s stop


#启动php-fpm
[root@localhost sbin]# ./php-fpm 




2.检查nginx

#检查nginx是否有问题
[root@localhost ~]# /usr/local/nginx/sbin/nginx  -t


3.配置文件:

nginx配置文件:/etc/init.d/nginx



//-----------------------------------------------------------------------------------------------------//

本文只是简单搭建了一个测试环境。如果需要用好nginx还需要不断学习和研究。


详解Nginx配置文件结构

http://www.linuxidc.com/Linux/2013-11/92594.htm



























#nginx启动
[root@localhost sbin]# ./nginx 


#停止niginx

[root@localhost sbin]# ./nginx -s stop


#启动php-fpm
[root@localhost sbin]# ./php-fpm 


#启动mysql

/etc/init.d/mysqld start


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值