Centos 编译安装LNMP环境(Nginx+MySQL+PHP)教程
1. 下载源码包
上Nginx官网,复制最新稳定版的下载地址过来,然后用wget下载
软件源代码包存放位置:/usr/local/src
源码包编译安装位置:/usr/local/软件名字
安装编译工具及库文件(使用CentOS yum命令安装)
# yum install make apr* autoconf automake curl-devel gcc gcc-c++ zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* mpfr cpp glibc libgomp libstdc++-devel ppl cloog-ppl keyutils-libs-devel libcom_err-devel libsepol-devel libselinux-devel krb5-devel zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses* libtool* libxml2 libxml2-devel patch
下载cmake(MySQL编译工具) http://www.cmake.org/files/v2.8/cmake-2.8.8.tar.gz
安装cmake
tar zxvf cmake-2.8.8.tar.gz
cd cmake-2.8.8
./configure
make
make install
下载libmcrypt(PHPlibmcrypt模块)
http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz(和cmake一样源码编译)
# cd /usr/local/src
# wget http://nginx.org/download/nginx-1.10.3.tar.gz
# tar xvf nginx-1.10.3.tar.gz
# yum groupinstall “Development tools”(批量安装软件)
cd /usr/local/src/nginx-1.10.3(进入下载的nginx)
配置nginx参数(包括log,安装路径等)
执行配置文件
./configure \
--prefix=/usr/local/nginx \(安装路径)
--sbin-path=/usr/sbin/nginx \(执行文件)(对应于下面的nginxd=/usr/sbin/nginx)
--conf-path=/etc/nginx/nginx.conf \(配置路径)
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--user=nginx \ (添加用户和用户组)
--group=nginx \
--with-pcre \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-ipv6 \
--with-http_v2_module \
--with-threads \
--with-stream \
--with-stream_ssl_module
//编译,安装(后面的mysql,php编译也是这个套路)
# make && make install
添加SysV启动脚本,设置nginx自启动,加入以下脚本
(注:启动脚步好像都是在这个/etc/init.d/下配的,包括php-fpm,这里面的有些参数和上面nginx配置参数一 一对应)
# vim /etc/init.d/nginx
vim /etc/init.d/nginx
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config:/etc/nginx/nginx.conf
nginxd=/usr/sbin/nginx
nginx_config=/etc/nginx/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog=$(basename $nginxd)(和上面的nginxd对应)
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
//权限执行
chmod 775 /etc/rc.d/init.d/nginx or chmod +x
添加至服务管理列表,设置开机自启:
# chkconfig –-add nginx
# chkconfig nginx on
# service nginx start
(报错:) [emerg]: getpwnam(“nginx”) failed
解决方案一
在nginx.conf中 把user nobody的注释去掉既可
解决方案二
错误的原因是没有创建nginx这个用户,应该在服务器系统中添加nginx用户组和用户nginx,如下命令:
1 2 | /usr/sbin/groupadd -f nginx /usr/sbin/useradd -g nginx nginx |
安装PHP
自行下载源码(wget)
安装依赖包
yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel(在配置过程中出现缺少的包,自行用yum install package 如:install -y libjpeg libjpeg-devel)
# tar xvf php-7.0.16.tar.bz2
# cd /usr/local/src/php-7.0.16
执行下面的配置文件:
# ./configure --prefix=/usr/local/php \
--with-config-file-scan-dir=/etc/php.d \
--with-config-file-path=/etc \
#--with-mysql=/usr/local/mysql \(注释调两行mysql相关的,现在没装mysql)
#--with-mysqli=/usr/local/mysql/bin/mysql_config \
--enable-fpm \
--enable-opcache \
--disable-fileinfo \
--with-jpeg-dir \
--with-iconv-dir=/usr/local \
--with-freetype-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-exif \
--with-curl \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-inline-optimization \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-ftp \
--with-gettext \
--enable-zip \
--enable-soap \
--with-bz2
# make && make install
添加php和php-fpm配置文件。
cp /usr/local/src/php-7.0.16/php.ini-production /usr/local/php/lib/php.ini
cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
4. 添加php-fpm启动脚本。
# cp /usr/local/src/php-7.0.16/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
5. # chmod +x /etc/init.d/php-fpm
添加php-fpm至服务列表并设置开机自启。
# chkconfig –add php-fpm
# chkconfig –list php-fpm
# chkconfig php-fpm on
6. 启动服务。
# service php-fpm start
、配置nginx支持php
Vim /etc/nginx.nginx.conf
Root 指定网站根目录
index index.php index.html index.htm; #添加index.php
user www www; #首行user去掉注释,修改Nginx运行组为www www;必须与/usr/local/php5/etc/php-fpm.conf中的user,group配置相同,否则php运行出错 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,或者使用绝对路径/etc/init.d/nginx restart #重启nginx