源代码搭建LNMP
源代码搭建LNMP
开发环境:
Red Hat Enterprise Linux 5.4
# yum grouplist |less
确保以下包已被安装:
Development Libraries 开发库
Development Tools 开发工具
Legacy Software Development 老的软件开发
X Software Development X软件开发
GNOME Software Devleopment GNOME软件开发
KDE Software Development KDE软件开发
需要安装的源码包:
pcre-8.12.tar.gz
libevent-2.0.16-stable.tar.gz
nginx-1.0.11.tar.gz
php-5.4.0.tar.gz
mysql-5.5.22.tar.gz
cmake-2.8.7.tar.gz
步骤:
解压:
[[email protected] ~]# tar -zxvf pcre-8.12.tar.gz -C /usr/src
[[email protected] ~]# tar -zxvf libevent-2.0.16-stable.tar.gz -C /usr/src
[[email protected] ~]# tar -zxvf nginx-1.0.11.tar.gz -C /usr/src
[[email protected] ~]# tar -zxvf php-5.4.0.tar.gz -C /usr/src
[[email protected] ~]# tar -zxvf mysql-5.5.22.tar.gz -C /usr/src
[[email protected] ~]# tar -zxvf cmake-2.8.7.tar.gz -C /usr/src
安装pcre:
PCRE是perl所用到的正则表达式,目的是让所装的软件支持正则表达式。默认情况下,Nginx只处理静态的网页请求,也就是html.如果是来自动态的网页请求,比如*.php,那么Nginx就要根据正则表达式查询路径,然后把*.PHP交给PHP去处理
# 查询系统中有没有安装PCRE,一般装系统是默认装有,所以我们要删掉系统自带的
[[email protected] ~]# rpm -qa |grep pcre
pcre-6.6-2.el5_1.7
# 在删除系统自带的PCRE之前,要先备份一下libpcre.so.0这个文件,因为RPM包的关联性太强,在删除后没libpcre.so.0这个文件时我们装PCRE是装不上的
[[email protected] ~]# cp /lib/libpcre.so.0 /
[[email protected] lib]# mv libpcre.so.0 libpcre.so.0.0.1
[[email protected] lib]# ln -s libpcre.so.0.0.1 libpcre.so.0
[[email protected] ~]# cd /usr/src
[[email protected] src]# cd pcre-8.12
[[email protected] pcre-8.12]# ./configure //配置PCRE,因为PCRE是一个库,而不是像pache、php、postfix等这样的程序,所以我们安装时选择默认路径即可,这样会在后面安装其它东西时避免一些不必要的麻烦
[[email protected] pcre-8.12]# make //编译
[[email protected] pcre-8.12]# make install //安装
安装libevent:
[[email protected] src]# cd /usr/src/libevent-2.0.16-stable
[[email protected] libevent-2.0.16-stable]# ./configure
[[email protected] libevent-2.0.16-stable]# make
[[email protected] libevent-2.0.16-stable]# make install
[[email protected] src]# cd /usr/local
将库文件连接到系统默认的位置:
[[email protected] local]# vim /etc/ld.so.conf.d/libevent.conf
/usr/local/lib
测试是否成功导入
[[email protected] local]# ldconfig -v |grep libevent
libevent_core-2.0.so.5 -> libevent_core.so
libevent_pthreads-2.0.so.5 -> libevent_pthreads.so
libevent-2.0.so.5 -> libevent.so
libevent_extra-2.0.so.5 -> libevent_extra.so
libevent_openssl-2.0.so.5 -> libevent_openssl.so
libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2
[[email protected] local]# ldconfig -v |grep pcre
libpcreposix.so.0 -> libpcreposix.so.0.0.0
libpcrecpp.so.0 -> libpcrecpp.so.0.0.0
libpcre.so.0 -> libpcre.so.0.0.1
libpcre.so.0 -> libpcre.so.0.0.1
安装nginx:
[[email protected] ~]# cd /usr/src/nginx-1.0.11/
[[email protected] nginx-1.0.11]# groupadd -r nginx
[[email protected] nginx-1.0.11]# ./configure \
> --prefix=/usr \ 安装起始位置
> --sbin-path=/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/nginx.pid \ pid路径
> --lock-path=/var/lock/nginx.lock \ 锁文件存放位置
> --user=nginx \ 指定服务以nginx用户运行
> --group=nginx \ 指定服务以nginx组运行
> --with-http_ssl_module \ 起用ssl模块
> --with-http_flv_module \ 起用流模块
> --with-http_stub_status_module \
> --with-http_gzip_static_module \ 压缩模块,方便网络传输
> --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/ \
> --with-pcre
[[email protected] nginx-1.0.11]# make
[[email protected] nginx-1.0.11]# make install
[[email protected] nginx-1.0.11]# mkdir -pv /var/tmp/nginx/client/
mkdir: 已创建目录 “/var/tmp/nginx”
mkdir: 已创建目录 “/var/tmp/nginx/client”
[[email protected] nginx-1.0.11]# nginx
[[email protected] nginx-1.0.11]# netstat -tupln |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 20322/nginx: master
在启动完NGINX后,我们可以在浏览器中输入http://localhost查看,如下图:
安装mysql:
mysql5.5已经改用cmake编译,编译前安装cmake
安装cmake:
[[email protected] ~]# cd /usr/src
[[email protected] src]# cd cmake-2.8.7/
[[email protected] cmake-2.8.7]# ./configure
[[email protected] cmake-2.8.7]# gmake
[[email protected] cmake-2.8.7]# gmake install
安装mysql:
[[email protected] ~]# cd /usr/src
[[email protected] src]# cd mysql-5.5.22/
[[email protected] mysql-5.5.22]# groupadd mysql
[[email protected] mysql-5.5.22]# useradd -r -M -g mysql mysql
[[email protected] mysql-5.5.22]# cmake \
>-DDCMAKE_INSTALL_PREFIX=/=/usr/local/mysql \ 安装路径
>-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ 连接数据库socket的路径
>-DDEFAULT_CHARSET=utf8 \ 默认字符
>-DDEFAULT_COLLATION=utf8_general_ci \ 矫正字符集
>-DWITH_EXTRA_CHARSETS=all \ 额外字符集
>-DWITH_MYISAM_STORAGE_ENGINE=1 \ 储存引擎
>-DWITH_INNOBASE_STORAGE_ENGINE=1 \
>-DWITH_MEMORY_STORAGE_ENGINE=1 \
>-DWITH_READLINE=1 \ 使用readline library
>-DENABLED_LOCAL_INFILE=1 \ 允许访问本地数据
>-DMYSQL_DATADIR=/data/mysql \ 数据库路径
>-MYSQL_USER=mysql \ 用户
>-DMYSQL_TCP_PORT=3306 mysql端口
[[email protected] mysql-5.5.22]# make
[[email protected] mysql-5.5.22]# make install
[[email protected] local]# chmod +w /usr/local/mysql
[[email protected] local]# chown -R mysql:mysql /usr/local/mysql
[[email protected] local]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18
[[email protected] local]# mkdir -p /var/mysql
[[email protected] local]# mkdir -p /var/mysql/data/
[[email protected] local]# mkdir -p /var/mysql/log/
[[email protected] local]# chown -R mysql:mysql /var/mysql/
[[email protected] local]# cd /usr/local/mysql/support-files
mysql 初始化安装:
[[email protected] support-files]# /usr/local/mysql/scripts/mysql_install_db \
> --defaults-file=/etc /my.cnf \
> --basedir=/usr/local/mysql \
> --datadir=/var/mysql/data \
> --user=mysql
将 mysql 加入开机启动:
[[email protected] support-files]# cp mysql.server /etc/init.d/mysqld 设置启动配置文件
[[email protected] support-files]# chmod +x /etc/init.d/mysqld
[[email protected] support-files]# vi /etc/init.d/mysqld
[[email protected] support-files]# chkconfig --level 345 mysqld on
[[email protected] mysql]# chkconfig --list |grep mysql
mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
此后可以用 service mysqld 来管理mysqld服务
Starting MySQL.... [确定]
[[email protected] support-files]# netstat -tupln |grep mysqld
tcp 0 0 :::3306 :::* LISTEN 7636/mysqld
[[email protected] mysql]# vim /etc/profile 修改系统搜索变量,定义搜索路径
[[email protected] mysql]# . /etc/profile 重新读取
[[email protected] mysql]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin:/usr/local/mysql/bin
mysql的操作:
[[email protected] mysql]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show datebase;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'datebase' at line 1
mysql> use test;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> \q
Bye
安装php:
如果想让编译的php支持mcryp,此处还需要下载两个rpm包并安装之:
php-mcrypt-5.1.6-5.el5.i386.rpm
libmcrypt-2.5.7-5.el5.i386.rpm php-mcrypt 依赖的模块
[[email protected] ~]# yum localinstall php-mcrypt-5.1.6-5.el5.i386.rpm libmcrypt-2.5.7-5.el5.i386.rpm –nogpgcheck
--nogpgcheck 取消gpg签名验证
[[email protected] ~]# cd /usr/src
[[email protected] php-5.4.0]# cd php-5.4.0/
[[email protected] php-5.4.0]# ./configure \
>--prefix=/usr/local/php \
> --with-mysql=/usr/local/mysql \
> --with-openssl \
> --enable-fpm \
> --with-mysqli=/usr/local/mysql/bin/mysql_config \
> --enable-mbstring \
> --with-freetype-dir \
> --with-jpeg-dir \
> --with-png-dir \
> --with-zlib-dir \
> --with-libxml-dir=/usr \
> --enable-xml \
> --with-config-file-path=/etc/php \
> --with-config-file-scan-dir=/etc/php \
> --with-bz2 --with-curl
[[email protected] php-5.4.0]# make
[[email protected] php-5.4.0]# make test
[[email protected] php-5.4.0]# make install
为php提供配置文件:
[[email protected] php-5.4.0]# cp php.ini-production /etc/php.ini
为php-fpm提供Sysv init脚本,并将其添加至服务列表:
[[email protected] php-5.4.0]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[[email protected] php-5.4.0]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[[email protected] php-5.4.0]# chkconfig --add php-fpm
[[email protected] php-5.4.0]# chkconfig php-fpm on
为php-fpm提供配置文件:
[[email protected] php-5.4.0]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[[email protected] php-5.4.0]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
编辑php-fpm的配置文件:
[[email protected] php-5.4.0]# vim /usr/local/php/etc/php-fpm.conf
配置pm.的相关选项为你所需要的值,并启用pid文件:
201行 pm.max_children = 50
206行 pm.start_servers = 5
211行 pm.min_spare_servers = 2
216行 pm.max_spare_servers = 8
25行 pid = /var/run/php-fpm.pid
[[email protected] php-5.4.0]# vim /usr/local/php/etc/php-fpm.conf
配置pm.的相关选项为你所需要的值,并启用pid文件:
201行 pm.max_children = 50
206行 pm.start_servers = 5
211行 pm.min_spare_servers = 2
216行 pm.max_spare_servers = 8
25行 pid = /var/run/php-fpm.pid
用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):
[[email protected] php-5.4.0]# ps aux | grep php-fpm
root 20533 0.0 0.5 34364 2684 ? Ss 19:46 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
nobody 20534 0.0 0.4 34364 2308 ? S 19:46 0:00 php-fpm: pool www
nobody 20535 0.0 0.4 34364 2308 ? S 19:46 0:00 php-fpm: pool www
nobody 20536 0.0 0.4 34364 2308 ? S 19:46 0:00 php-fpm: pool www
nobody 20537 0.0 0.4 34364 2308 ? S 19:46 0:00 php-fpm: pool www
nobody 20538 0.0 0.4 34364 2308 ? S 19:46 0:00 php-fpm: pool www
root 20544 0.0 0.1 5016 688 pts/1 R+ 19:47 0:00 grep php-fpm
整合nginx和php:
编辑/etc/nginx/nginx.conf:
[[email protected] php-5.4.0]cd /etc/nginx
[[email protected] nginx]# cp nginx.conf nginx.conf.back
[[email protected] nginx]# vim nginx.conf
启用如下选项:
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;
}
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;
}
在所支持的主页面格式中添加php格式的主页,如下:
location / {
root html;
index index.php index.html index.htm;
}
root html;
index index.php index.html index.htm;
}
编辑/etc/nginx/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;
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;
[[email protected] nginx]# pkill nginx
[[email protected] nginx]# nginx
测试:
[[email protected] html]# cp index.html index.php
[[email protected] html]# vim index.php
接着就可以通过浏览器访问此测试页面了
和mysql连接:
[[email protected] html]# vim index.php
测试
[[email protected] html]# service mysqld stop 将连接失败
转载于:https://blog.51cto.com/lulu1101/829551