LNMP的搭建(linux+Nginx+MysqL+PHP)
简介
Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。
这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。
特点
Nginx是一个小巧而高效的Linux下的Web服务器软件,由 Igor Sysoev 为俄罗斯访问量第二的 Rambler 站点开发,在一些俄罗斯的大型网站上运行多年,相当稳定。
Nginx性能稳定、功能丰富、运维简单、处理静态文件速度快且消耗系统资源极少
系统环境
系统平台:centos6
安装准备
如未安装则使用yum install安装(系统默认安装)。
#yum install
gcc gcc-c++ ---编译器
autoconf ---shell脚本工
libjpeg libjpeg-devellibpng libpng-devel
freetype freetype-devel ---字体引擎
libxml2 libxml2-devel --- C语言的XML程式库
zlib zlib-devel ---提供数据压缩用的函式库
glibc glibc-devel ---lib库(c运行库),linux中最底层api
glib2 glib2-devel
bzip2 bzip2-devel ---无损压缩软件
ncurses ncurses-devel ---提供字符终端处理库
curl curl-devel ---利用URL语法在命令行方式下工作的开源文件传输工具
e2fsprogs e2fsprogs-devel ---Ext2(及Ext3/4)文件系统工具集,包含了创建、修复、配置、调试文件系统
krb5 krb5-devel ---客户端/服务器结构与DES加密技术
libidn libidn-devel ---实施字符串预处理、Punycode 和 IDNA规格定义的工具
openssl openssl-devel ---加密传输
openldap openldap-devel ---轻型目录访问协议的自由和开发的实现
openldap-clientsopenldap-servers --openldap服务器客户端
安装并编译libmcrypt
#tar �Czxvf libmcrypt-2.5.7.tar.gz
#cd libmcrypt-2.5.7
#./configure --prefix=/usr/local/libmcrypt
#make && make install
安装并编译pcre
#tar zxvf pcre-8.31.tar.gz
#cd pcre-8.31
#./configure--prefix=/usr/local/pcre
#make
#make install
安装MysqL
1.先下载MysqL-5.5.10.tar.gz和cmake-2.8.4.tar.gz两个源件
先安装cmake
#tar -zxv -fcmake-2.8.5.tar.gz
#cd cmake-2.8.5
#./configure
#make
#make install
#mkdir -p/usr/local/MysqL
#mkdir -p/usr/local/MysqL/data
# groupadd MysqL
安装MysqL
#cd ~
#tar -zxv -fMysqL-5.5.20.tar.gz
#cd MysqL-5.5.20
#cmake .
#make install
参数说明:
-DCMAKE_INSTALL_PREFIX=/usr/local/MysqL//安装目录
-DINSTALL_DATADIR=/usr/local/MysqL/data//数据库存放目录
-DDEFAULT_CHARSET=utf8 //使用utf8字符
-DDEFAULT_COLLATION=utf8_general_ci//校验字符
-DEXTRA_CHARSETS=all //安装所有扩展字符集
-DENABLED_LOCAL_INFILE=1 //允许从本地导入数据
注意事项:
重新编译时,需要清除旧的对象文件和缓存信息。
# make clean
# rm -fCMakeCache.txt
# rm -rf /etc/my.cnf
2.配置
1)设置目录权限
#cd/usr/local/MysqL
#chown-Rroot:MysqL.//(把当前目录中所有文件的所有者所有者设为root,所属组为MysqL)
2)#cpsupport-files/my-medium.cnf /etc/my.cnf
3)创建系统数据库的表
# cd /usr/local/MysqL
#scripts/MysqL_install_db --user=MysqL
4)设置环境变量
# vi/root/.bash_profile
# .bash_profile
# Get the aliasesand functions
if [ -f ~/.bashrc]; then
. ~/.bashrc
fi
# User specificenvironment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/MysqL/bin:/usr/local/MysqL/lib
export PATH
5)手动启动MysqL
# cd/usr/local/MysqL/
#./bin/MysqLd_safe--user=MysqL&(启动MysqL,但不能停止)
6))另一种简单的启动MysqL的方法(MysqL已经被添加到系统服务中)
# service MysqL.server start
如果上述命令出现:MysqL.server 未识别的服务
# cp support-files/MysqL.server/etc/init.d/MysqL
然后再用#service MysqL start 来启动MysqL即可.
查看一下MysqL是否启动成功
#netstat -nltp
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1288/MysqLd
设置MysqL密码
/usr/local/MysqL/bin/MysqLadmin-u root -p password "123456"
输入;123456 密码就是123456
做个软连接:
ln-s /usr/local/MysqL/bin/MysqL /usr/bin/
Nginx的安装
1.需要三个安装包
1)openssl-fips-ecp-2.0.1.tar
2)pcre-8.31.tar
3)zlib-1.2.7.tar
2.依次安装openssl-fips-ecp-2.0.1.tar,zlib-1.2.7.tar,pcre-8.31.tar
1)安装openssl-fips-ecp-2.0.1.tar
#tar -zxvf openssl-fips-ecp-2.0.1.tar.gz
#cd openssl-fips-ecp-2.0.1
#./config
#make
#make install
安装zlib-1.2.7.tar
#cd ~
# tar -zxvf zlib-1.2.7.tar.gz
# cd zlib-1.2.7
# ./configure
#make
pcre-8.31.tar
# tar -zxvf pcre-8.31.tar.gz
# cd pcre-8.31
#make install
4)安装Nginx
# tar -zxvfNginx-1.2.7.tar.gz
# cd Nginx-1.2.7
#./configure--with-pcre=../pcre-8.31 --with-zlib=../zlib-1.2.7--with-openssl=../openssl-fips-ecp-2.0.1
到此Nginx安装完成
5)检测是否安装成功
# cd /usr/local/Nginx/sbin/
#./Nginx �Ct --查看是否安装成功
启动Nginx
#./Nginx
或者/etc/init.d/Nginx start
查看端口
# netstat �Cnltp
LISTEN 26557/Nginx tcp 0 0 0.0.0.0:21 0.0.0.0:*
# vim /usr/local/Nginx/conf/Nginx.conf
user www www;
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 {
use epoll;
worker_connections 2048;
}
http {
include mime.types;
default_type application/octet-stream;
#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 on;
keepalive_timeout 65;
# gzip压缩功能设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;
# http_proxy 设置
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /usr/local/Nginx/proxy_temp 1 2;
upstream backend {
#ip_hash;
server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
}
# 很重要的虚拟主机配置
server {
listen 80;
server_name itoatest.example.com;
root /apps/oaapp;
charset utf-8;
access_log logs/host.access.log main;
#对 / 所有做负载均衡+反向代理
location / {
root /apps/oaapp;
index index.jsp index.html index.htm;
proxy_pass http://backend;
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
#静态文件,Nginx自己处理,不去backend请求tomcat
location ~* /download/ {
root /apps/oa/fs;
}
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /apps/oaapp;
expires 7d;
location /Nginx_status {
stub_status on;
access_log off;
allow 192.168.10.0/24;
deny all;
location ~ ^/(WEB-INF)/ {
deny all;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
## 其它虚拟主机,server 指令开始
}
同时查看下iptables是否开放了80端口。
#vim /etc/sysconfig/iptables
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
# service iptables restart 重启iptables
# /etc/init.d/Nginx restart 重启Nginx
安装并编译PHP
#tar -zvxf PHP-5.3.10.tar.gz
#cd PHP-5.3.10
#mkdir -p /usr/local/PHP5 #建立PHP安装目录
#./configure --prefix=/usr/local/PHP5
--with-config-file-path=/usr/local/PHP5/etc ---设置PHP.ini的搜索路径(默认
PREFIX/lib)
--with-MysqL-sock=/tmp/MysqL.sock
--with-gd ---处理图形的拓展库
--with-iconv ---函数
--with-zlib ---打开zlib库的支持
--enable-xml
--enable-magic-quotes ---默认启用magic quotes
--enable-safe-mode ---启用安全模式
--enable-bcmath ---函数
--enable-shmop ---允许PHP读、写、创建、删除UNIX共享内存段
--enable-sysvsem
--enable-inline-optimization
--with-curlwrappers ---运用curl工具打开url流
--enable-mbregex
--enable-fpm
--enable-mbstring ---多字节,字符串的支持
--enable-ftp ---打开ftp的支持
--enable-gd-native-ttf ---支持TrueType字符串函数库
--with-openssl --- openssl的支持,加密传输
--enable-pcntl --- freeTDS需要,链接MysqL
--enable-sockets ---
--with-xmlrpc ---打开xml-rpc的c语言
--enable-zip ---打开对zip的支持
--enable-soap
--with-pear
--with-gettext ---打开gnu 的gettext 支持,编码库用到
--enable-session
--with-mcrypt ---算法
--with-curl ---打开curl浏览工具的支持
#make install
这时会报错configure:error: mcrypt.h not found. Please reinstall libmcrypt
1.解决#使用wget可以通过以下路径下载
2.wgetftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
3.
4.#解压
5.tar-zxvflibmcrypt-2.5.7.tar.gz
6.
7.#进入目录
8.cdlibmcrypt-2.5.7
9.
10.#编译(默认安装到/usr/local/lib/)
11../configure--prefix=/usr/local/libmcrypt
12.
13.#执行安装
14.make&&makeinstall
15.
下载libmcrypt-2.5.8.tar.gz
#tar zxvf libmcrypt-2.5.8.tar.gz
#cd libmcrypt-2.5.8
#./configure--prefix=/usr/local/libmcrypt
#make
#make install
=========================================
mhash-0.9.9.9.tar.gz
#tar zxvf mhash-0.9.9.9
#cd mhash-0.9.9.9
#./configrue --prefix=/usr/local/mhash
#make install
=========================================
#tar zxvf mcrypt-2.6.8.tar.gz
#cd mcrypt-2.6.8
exportLD_LIBRARY_PATH=/usr/local/libmcrypt/lib:/usr/local/mhash/lib
export LDFLAGS="-L/usr/local/mhash/lib-I/usr/local/mhash/include/"
exportCFLAGS="-I/usr/local/mhash/include/"
#./configure --prefix=/usr/local/mcrypt--with-libmcrypt-prefix=/usr/local/libmcrypt
#make install
或者:yuminstall epel-release //扩展包更新包
#yumupdate//更新yum源
#yum installlibmcrypt libmcrypt-devel mcrypt mhash
修改配置文件
#cp PHP.ini-production /usr/local/PHP5/etc/PHP.ini #复制PHP配置文件到安装目录
#rm -rf /etc/PHP.ini #删除系统自带配置文件
#ln -s /usr/local/PHP5/etc/PHP.ini /etc/PHP.ini #添加软链接
#cp /usr/local/PHP5/etc/PHP-fpm.conf.default/usr/local/PHP5/etc/PHP-fpm.conf #拷贝模板文件为PHP-fpm配置文件
#vim /usr/local/PHP5/etc/PHP-fpm.conf #编辑
user = www #设置PHP-fpm运行账号为www
group = www #设置PHP-fpm运行组为www
pid = run/PHP-fpm.pid #取消前面的分号设置PHP-fpm开机启动
#chmod +x /etc/rc.d/init.d/PHP-fpm #添加执行权限
#chkconfig PHP-fpm on #设置开机启动
或vim /etc/rc.local
#vim /usr/local/PHP5/etc/PHP.ini #编辑配置文件
找到:;open_basedir = (379行)
修改为:open_basedir = .:/tmp/ #防止PHP木马跨站
找到:disable_functions = (385行)
修改为:disable_functions =
passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,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
#列出PHP可以禁用的函数,如果某些程序需要用到这个函数,可以删除,取消禁用。
找到:;date.timezone =
修改为:date.timezone = PRC #设置时区
找到:expose_PHP = On
修改为:expose_PHP = OFF #禁止显示PHP版本的信息
找到:display_errors = On
修改为:display_errors = OFF #关闭错误提示
启动方式
#cp /root/lnmp/PHP-5.5.12/sapi/fpm/init.d.PHP-fpm /etc/init.d/PHP-fpm
#/etc/init.d/PHP-fpm restart
修改配置文件
#vim /usr/local/Nginx/conf/Nginx.conf
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
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;
}
报错解决
http://www.oschina.net/question/878142_106780
报错:
2016/08/31 13:56:25 [error]15197#0: *1 FastCGI sent in stderr: "PHP message: PHP Warning: Unknown: open_basedir restriction in effect.File(/usr/local/Nginx/html/index.PHP) is not within the allowed path(s):(.:/tmp/) in Unknown on line 0
PHP message: PHPWarning: Unknown: Failed to open stream:Operation not permitted in Unknown on line 0
Unable to open primaryscript: /usr/local/Nginx/html/index.PHP (Operation not permitted)" whilereading response header from upstream,client: 192.168.1.206,server:localhost,request: "GET /index.PHP HTTP/1.1",upstream:"fastcgi://127.0.0.1:9000",host: "192.168.1.188"
解决:修改/etc/PHP.ini下open_basedir
改成open_basedir = /usr/local/Nginx/html/:/tmp/
重启服务
#/etc/init.d/Nginx restart #重启Nginx
测试
#cd /usr/local/Nginx/html/ #进入Nginx默认网站根目录
#rm -rf /usr/local/Nginx/html/* #删除默认测试页
<?PHP
PHPinfo();
?>
设置权限
#chown www:www /usr/local/Nginx/html/ -R #设置目录所有者
#chmod 700 /usr/local/Nginx/html/ -R #设置目录权限
在客户端浏览器输入服务器IP地址,可以看到相关的配置信息!