License: Attribution-NonCommercial-ShareAlike 4.0 International
本文出自 Suzf Blog。 如未注明,均为 SUZF.NET 原创。
转载请注明:http://suzf.net/post/60
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Mysql是一个小型关系型数据库管理系统。
PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。
这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统。
方案一: 主机A Nginx(FastCGI) + PHP(php-fpm) + MySQL
方案二: 主机A Nginx(FastCGI) + 主机B PHP(php-fpm) + 主机C MySQL
静态页面.html PHP动态程序 数据库
注:建议Nginx和php运行在同一台主机上
nginx-1.4.4.tar.gz
php-5.4.5.tar.gz
cmake-2.8.10.2.tar.gz
mysql-5.5.33.tar.gz
一、安装Nginx
1. pcre: 支持正则表达式,地址重写rewrite
# tar xvf pcre-8.31.tar.gz
# cd pcre-8.31
# ./configure && make && make install
2. Nginx
# yum -y install openssl openssl-devel
# groupadd www
# useradd -g www www
# tar xf nginx-1.4.4.tar.gz
# cd nginx-1.4.4
# ./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre=pcre源程序目录
# make && make install#
# /usr/local/nginx/sbin/nginx
# netstat -tnlp |grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11051/nginx
# elinks -dump http://localhost
Welcome to nginx!
# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
二、安装PHP
1. 以php-fpm的方式安装php
# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \
libxml2 libxml2-devel libcurl libcurl-devel libxslt-devel
#tar zxf php-5.4.5.tar.gz
#./configure \
--prefix=/usr/local/php \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-jpeg-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysql \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip
# make && make install
2. php-fpm配置文件
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid
3. php主配置文件
# cp php.ini-production /usr/local/php/lib/php.ini
4. 添加到init启动
[root@Jeffery php-5.4.5]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@Jeffery php-5.4.5]# chmod a+x /etc/rc.d/init.d/php-fpm
[root@Jeffery php-5.4.5]# chkconfig --add php-fpm
[root@Jeffery php-5.4.5]# chkconfig php-fpm on
[root@Jeffery php-5.4.5]# service php-fpm start
Starting php-fpm done
三、整合Nginx和PHP
1. Nginx启用Fastcgi
# vim /usr/local/nginx/conf/nginx.conf
启用:
location / {
root html;
index index.php index.html index.htm;
}
去掉以下行的注释:
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;
}
2. fastcgi模块参数文件
# rm -rf /usr/local/nginx/conf/fastcgi_params
[root@tianyun ~]# vim /usr/local/nginx/conf/fastcgi_params
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
3. 最终确认并重启
# killall nginx; /usr/local/nginx/sbin/nginx
# /etc/init.d/php-fpm restart
# netstat -tnlp |grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17947/nginx
# netstat -tnlp |grep :9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 17961/php-fpm
三、安装Mysql
# yum -y remove mysql mysql-server cmake
# rm -rf /etc/my.cnf
# yum -y groupinstall "Development tools"
# yum -y install ncurses ncurses-devel openssl-devel bison gcc gcc-c++ make
Cmake:
# tar xvf cmake-2.8.10.2.tar.gz
# cd cmake-2.8.10.2
# ./bootstrap
# make
# make install
# cmake -version
MySQL:
# groupadd mysql
# useradd -M -g mysql -s /sbin/nologin mysql
# tar xvf mysql-5.5.33.tar.gz
# cd mysql-5.5.33
# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DSYSCONFDIR=/etc \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DINSTALL_MANDIR=/usr/share/man \
-DMYSQL_TCP_PORT=3306 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
# make
# make install
===MySQL初始化
1. 初始化
# cd /usr/local/mysql
# chown -R mysql:mysql .
# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
2. 生成MySQL主配置文件
# cp support-files/my-huge.cnf /etc/my.cnf mysql5.5
# cp support-files/my-default.cnf /etc/my.cnf mysql5.6
3. 生成MySQL Sys V脚本
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on
# service mysqld start
4. 配置MySQL环境变量
# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
# source /etc/profile
5. 登录MySQL:
# mysqladmin -uroot password 'jeffery'
# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# mysql -uroot -pjeffery
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
五、上传php动态网站测试
1. 测试php文件能否执行
#cd /usr/local/nginx/html/
# rm -rf *
# vim index.php
< ?php phpinfo(); ?>
2. 测试php连接MySQL是否正常
# vim index2.php
< ?php $link=mysql_connect('172.16.7.20','root','jeffery'); if ($link) echo "Successfuly"; else echo "Faile"; mysql_close(); ?>
3. 测试Discuz
略