CentOS 6.5安装配置LNMP服务器(Nginx+PHP+MySQL)

时间:2023-03-08 17:57:55
CentOS 6.5安装配置LNMP服务器(Nginx+PHP+MySQL)

CentOS 6.5安装配置LNMP服务器(Nginx+PHP+MySQL)
一.准备篇:

 /etc/init.d/iptables stop          #关闭防火墙
关闭SELINUX
vi /etc/selinux/config
#SELINUX=enforcing   #注释掉
#SELINUXTYPE=targeted     #注释掉
SELINUX=disabled  #增加
:wq
shutdown -r now #重启系统  

二.安装篇

1.安装nginx

 yum remove httpd* php*           #删除系统自带的软件包
安装步骤:
因为nginx模块需要第三方库的支持,需要安装下列库.
安装gcc编译器及相关工具
#yum -y install gcc gcc-c++ autoconf automake make
安装相关依赖的模块
#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
创建nginx系统账户:
由于nginx是系统服务,运行此服务需要系统账户。
# groupadd nginx
# useradd -r -g nginx -s /sbin/nologin -M nginx
软件源码包存放位置:/opt/
#cd /opt/
# tar -zxvf nginx-1.2.4.tar
#cd nginx-1.2.4
#./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module //通过编译源码的方式进行安装
#make
#make install
启动Nginx
#启动nginx
#/usr/local/nginx/sbin/nginx -t
显示以下信息为正确的 the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok configuration file /usr/local/nginx/conf/nginx.conf test is successful
#/usr/local/nginx/sbin/nginx

启动成功后,查看nginx进程信息: # ps -ef | grep nginx ,看是否存在nginx的进程来确认是否成功启动。

2.安装MySQL(也可以使用系统自带mysql)

1、安装MySQL

 yum install mysql mysql-server                   #输入Y即可自动安装,直到安装完成
/etc/init.d/mysqld start #启动MySQL chkconfig mysqld on #设为开机启动 cp /usr/share/mysql/my-medium.cnf /etc/my.cnf #拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)
为root账户设置密码 mysql_secure_installation #回车,根据提示输入Y,输入2次密码,回车,根据提示一路输入Y,最后出现:Thanks for using MySQL! MySql密码设置完成,重新启动 MySQL: /etc/init.d/mysqld restart #重启 /etc/init.d/mysqld stop #停止 /etc/init.d/mysqld start #启动

3、安装PHP5

1、安装PHP5

 yum install php php-fpm                      #根据提示输入Y直到安装完成

 2、安装PHP组件,使 PHP5 支持 MySQL

 yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt  php-bcmath php-mhash libmcrypt

 #这里选择以上安装包进行安装,根据提示输入Y回车

 chkconfig php-fpm on                          #设置php-fpm开机启动

 /etc/init.d/php-fpm start                      #启动php-fpm

配置篇

一、配置nginx支持php

 cp  /usr/local/nginx/conf/nginx.conf   /usr/local/nginx/conf/nginx.confbak       #备份原有配置文件
vim /usr/local/nginx/conf/nginx.conf
user www ; #修改nginx运行账号为:www用户
:wq #保存退出
vim /usr/local/nginx/conf/nginx.conf index index.php index.html index.htm; #增加index.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;
} #取消FastCGI server部分location的注释,并要注意fastcgi_param行的参数,改为$document_root$fastcgi_script_name,或者使用绝对路径
service nginx restart #重启nginx

二、php配置

 vi /etc/php.ini  

 date.timezone = PRC    #在946行 把前面的分号去掉,改为date.timezone = PRC

 disable_functions =

 passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,ope

 nlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdns

 rr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,

 posix_getegid,posix_geteuid,posix_getgid,

 posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,

 posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty,

 posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,
posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
#在386行列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
找到:date.timezone =
修改为:date.timezone = PRC #设置时区
找到:expose_php = On
修改为:expose_php = OFF #禁止显示php版本的信息
找到:short_open_tag = Off
修改为:short_open_tag = ON #支持php短标签
:wq! #保存退出

测试篇

cd  /usr/local/nginx/html
vim index.php <?php echo phpinfo();
?> :wq! chown www.www /usr/local/nginx/html -R #设置权限
service nginx restart #重启nginx
service php-fpm restart #重启php-fpm

在客户端浏览器输入服务器IP地址,可以看到相关的配置信息!

说明lnmp配置成功!
至此,CnetOS
6.4安装配置LNMP(Nginx+PHP+MySQL)配置完成。

号外:

PHP启动脚本:对照自己的目录,直接放置在/etc/init.d/下给执行权限即可运行.

#! /bin/sh
#
# chkconfig: -
# description: PHP FastCGI Process Manager
# processname: php-fpm
# config: /etc/php-fpm.conf
# config: /etc/sysconfig/php-fpm
# pidfile: /var/run/php-fpm/php-fpm.pid
#
### BEGIN INIT INFO
# Provides: php-fpm
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network
# Short-Description: start and stop PHP FPM
# Description: PHP FastCGI Process Manager
### END INIT INFO # Standard LSB functions
#. /lib/lsb/init-functions # Source function library.
. /etc/init.d/functions # Check that networking is up.
. /etc/sysconfig/network # Additional environment file
if [ -f /etc/sysconfig/php-fpm ]; then
. /etc/sysconfig/php-fpm
fi if [ "$NETWORKING" = "no" ]
then
exit
fi RETVAL=
prog="php-fpm"
pidfile=${PIDFILE-/var/run/php-fpm/php-fpm.pid}
lockfile=${LOCKFILE-/var/lock/subsys/php-fpm} start () {
echo -n $"Starting $prog: "
dir=$(dirname ${pidfile})
[ -d $dir ] || mkdir $dir
daemon --pidfile ${pidfile} /usr/sbin/php-fpm --daemonize
RETVAL=$?
echo
[ $RETVAL -eq ] && touch ${lockfile}
}
stop () {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} php-fpm
RETVAL=$?
echo
if [ $RETVAL -eq ] ; then
rm -f ${lockfile} ${pidfile}
fi
} restart () {
stop
start
} reload () {
echo -n $"Reloading $prog: "
if ! /usr/sbin/php-fpm --test ; then
RETVAL=
echo $"not reloading due to configuration syntax error"
failure $"not reloading $prog due to configuration syntax error"
else
killproc -p ${pidfile} php-fpm -USR2
RETVAL=$?
fi
echo
} # See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} php-fpm
RETVAL=$?
;;
restart)
restart
;;
reload|force-reload)
reload
;;
configtest)
/usr/sbin/php-fpm --test
RETVAL=$?
;;
condrestart|try-restart)
[ -f ${lockfile} ] && restart || :
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart|try-restart|configtest}"
RETVAL=
;;
esac exit $RETVAL

PHP启动脚本