LNMP环境是指Linux下搭建Nginx+MySQL+PHP.
Nginx是一个高性能的HTTP和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。不仅可以作为web服务器,也可以作为负载均衡器。
MySQL是一款开源免费的数据软件,MySQL是一个小型关系型数据库管理系统,其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库。
PHP,是英文超级文本预处理语言Hypertext Preprocessor的缩写。PHP 是一种 HTML 内嵌式的语言,是一种在服务器端执行的嵌入HTML文档的脚本语言,语言的风格有类似于C语言,被广泛的运用。
一、安装前准备工作
1.下载安装包,英文官方找起来很费劲,这是阿里云的安装包
cd /usr/src # 一般软件源码放在这个目录下
wget http://pan.baidu.com/s/1501Oy #下载nginx-1.4.4.tar.gz
wget http://pan.baidu.com/s/1mgoleIW #下载mysql-5.0.95.tar.gz
wget http://pan.baidu.com/s/1o6uS9j8 #下载php-5.5.7.tar.gz
2.安装依赖包,避免安装过程中会报错,这是已知必须要安装的,其他的出错自行百度。
yum -y install zlib-devel pcre-devel openssl-devel ncurses-devel libcurl-devel
libtool-ltdl-devel
二、安装Nginx
1、解压源码包
tar -zxvf nginx-1.0.13.tar.gz
2、进入源码包目录进行预编译
cd nginx-1.0.13
./configure --prefix=/usr/local/nginx\ # 指定安装目录为/usr/local/nginx--with-openssl=/usr/include/openssl\ # 启用ssl--with-pcre\ # 启用正规表达式--with-http_stub_status_module # 安装可以查看nginx状态的程序
3、预编译完成后进行编译
make #编译
如果没有报错就可以进行安装执行
4、预编译完成后进行编译和安装
make install #安装
5、编写启动脚本
vim /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/logs/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
保存后,更改权限:
chmod 755 /etc/init.d/nginx
6、更改nginx配置
首先把原来的配置文件清空:
> /usr/local/nginx/conf/nginx.conf
vim /usr/local/nginx/conf/nginx.conf
写入如下内容:
user nobody nobody; worker_processes 2; error_log /usr/local/nginx/logs/nginx_error.log crit; pid /usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile 51200; events { use epoll; worker_connections 6000; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' '$host "$request_uri" $status' '"$http_referer" "$http_user_agent"'; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3m; client_body_timeout 3m; send_timeout 3m; connection_pool_size 256; client_header_buffer_size 1k; large_client_header_buffers 8 4k; request_pool_size 4k; output_buffers 4 32k; postpone_output 1460; client_max_body_size 10m; client_body_buffer_size 256k; client_body_temp_path /usr/local/nginx/client_body_temp; proxy_temp_path /usr/local/nginx/proxy_temp; fastcgi_temp_path /usr/local/nginx/fastcgi_temp; fastcgi_intercept_errors on; tcp_nodelay on; gzip on; gzip_min_length 1k; gzip_buffers 4 8k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm application/xml; server { listen 80; server_name localhost; index index.html index.htm index.php; root /usr/local/nginx/html; location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/tmp/php-fcgi.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; } } }
保存配置后,先检验一下配置文件是否有错误存在:
/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
7、启动Nginx
Service nginx start
查看端口看nginx是否启动成功
netstat -antlp ¦ grep 80tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5946/nginx
可以看到80端口是开放的
然后打开浏览器访问localhost会看到Welcome to nginx!
8、加入开机启动
chkconfig --add nginx
chkconfig nginx on
三、安装MySQL
1、创建MySQL用户:
useradd -M -s /sbin/nologin mysql # -M不创建home目录,-s指定shell为不登录
2、解压安装包
tar -zxvf mysql-5.0.95.tar.gz
3、进入源码包目录进行预编译
cd /usr/local/mysql ./configure --prefix=/usr/local/mysql \ --localstatedir=/usr/local/mysql/var/ \ --with-server-suffix=-enterprise-gpl \ --without-debug --with-big-tables \ --with-extra-charsets=latin1,gb2312,big5,utf8,GBK \ --with-extra-charsets=all --with-pthread --enable-static \ --enable-thread-safe-client --with-client-ldflags=-all-static \ --with-mysqld-ldflags=-all-static --enable-assembler \ --without-innodb --without-ndb-debug --without-isam \ --with-federated-storage-engine
4、编译、
make #编译
5、如果没有报错就进行安装
make install #安装
6、安装完成后复制配置文件和启动脚本:
cp support-files/my-medium.cnf /etc/my.cnf # 复制配置文件cp support-files/mysql.server /etc/init.d/mysqld # 复制启动脚本chmod +x /etc/init.d/mysqld # 给启动脚本执行权限
为所有的二进制可执行文件和动态链接库文件做一个软连接:
ln -s /usr/local/mysql/bin/* /usr/local/bin/ # 为可执行的二进制文件做软连接ln -s /usr/local/mysql/lib/mysql/lib* /usr/lib/ # 为动态链接库做一个软连接
初始化数据库:
mysql_install_db --user=mysql # 用MySQL用户安装数据库
7、更改MySQL安装目录和MySQL的数据库目录的属主和属组:
chown -R root.mysql /usr/local/mysql/ # 更改安装目录属主为root,属组为mysqlchown -R mysql.mysql /usr/local/mysql/var/ # 更改数据库目录属主和属组都为mysql
8、启动mysql:
service mysqld start
查看MySQL是否启动成功
netstat -antlp ¦ grep 3306tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 32143/mysqld
9、然后通过mysql命令来连接mysql
mysql #连接到mysql
显示如下内容表示已经成功启动MySQL并已经连接上
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1Server version: 5.0.95-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates.
Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help.
Type '\c' to clear the current input statement.mysql>
10、加入开机启动
chkconfig --add mysqld
chkconfig mysqld on
四、安装PHP
我们使用nginx调用php的时候使用fpm的方式,.安装PHP:
1.解压php
tar zxf php-5.3.27.tar.gz
2.创建相关账户
useradd -s /sbin/nologin php-fpm
3.配置编译参数进行
cd php-5.3.27
./configure \ --prefix=/usr/local/php \ --with-config-file-path=/usr/local/php/etc \ --enable-fpm \ --with-fpm-user=php-fpm \ --with-fpm-group=php-fpm \ --with-mysql=/usr/local/mysql \ --with-mysql-sock=/tmp/mysql.sock \ --with-libxml-dir \ --with-gd \ --with-jpeg-dir \ --with-png-dir \ --with-freetype-dir \ --with-iconv-dir \ --with-zlib-dir \ --with-mcrypt \ --enable-soap \ --enable-gd-native-ttf \ --enable-ftp \ --enable-mbstring \ --enable-exif \ --enable-zend-multibyte \ --disable-ipv6 \ --with-pear \ --with-curl \ --with-openssl
4.编译php
make
5.安装php
make install
以上每一个步骤,如果没有完全执行正确,那么下一步是无法进行的, 使用 echo $? 看结果是否为 “0” , 如果不是,就是没有执行正确。
6.修改配置文件
cp php.ini-production /usr/local/php/etc/php.ini
vim /usr/local/php/etc/php-fpm.conf
把如下内容写入该文件:
[global] pid = /usr/local/php/var/run/php-fpm.pid error_log = /usr/local/php/var/log/php-fpm.log [www] listen = /tmp/php-fcgi.sock user = php-fpm group = php-fpm pm = dynamic pm.max_children = 50 pm.start_servers = 20 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 rlimit_files = 1024
保存配置文件,检验配置是否正确
/usr/local/php/sbin/php-fpm -t
如果出现诸如 “test is successful” 字样,说明配置没有问题。
7.启动php-fpm
cp /usr/local/src/php-5.3.27/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod 755 /etc/init.d/php-fpm
service php-fpm start
检测是否启动:
ps aux |grep php-fpm
8.加入开机启动
chkconfig --add php-fpm
chkconfig php-fpm on
测试是否解析php
创建测试文件:
vim /usr/local/nginx/html/index.php
内容如下:
<?php
echo "测试php是否解析";
?>
测试:
[root@localhost nginx]# curl localhost/
测试php是否解析[root@localhost nginx]#
2014-12-22 09:32