大纲:
一、前言
二、系统环境与软件版本
三、编译环境的准备
四、编译安装nginx及其配置
五、编译安装、配置mysql
六、编译安装PHP
七、整合nginx与PHP
八、安装配置PHP加速器xcache
九、安装配置memcached
十、安装memcached的PHP扩展
一、前言
由于公司的服务器采用的是LNMP的架构,平时接触相对较多,今天会系统的把LNMP的安装配置过程写成博文,有关nginx的其他高级功能的配置,mysql的相关知识,会在后面的时间里陆续写成博客。
二、系统环境与软件版本
操作系统:CentOS 5.8 内核版本号:2.6.18-308.el5
平台 :x86_64
软件版本
nginx-1.6.1.tar.gz
mysql-5.6.20.tar.gz
php-5.4.32.tar.bz2
xcache-3.2.0.tar.gz
memcached-1.4.20.tar.gz
三、编译环境的准备
1、使用yum安装开发包
[[email protected]_101 ~]# yum -y groupinstall "Development Tools" "Development Libraries"
2、安装nginx的依赖包
[[email protected]_101 ~]# yum -y install pcre pcre-devel zlib zlib-devel
四、编译安装nginx及其配置
1、解压包
[[email protected]_101 ~]# tar xf nginx-1.6.1.tar.gz -C /opt/ [[email protected]_101 ~]# cd /opt/nginx-1.6.1/
2、为nginx创建用户与用户组
[[email protected]_101 nginx-1.6.1]# groupadd -r nginx [[email protected]_101 nginx-1.6.1]# useradd -r -g nginx -s /sbin/nologin nginx
3、创建nginx的安装目录
[[email protected]_101 nginx-1.6.1]# mkdir /usr/local/nginx
4、编译安装nginx
[[email protected]_101 nginx-1.6.1]# ./configure --prefix=/usr/local/nginx --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 --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --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/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --with-pcre ………… Configuration summary + using system PCRE library + using system OpenSSL library + md5: using OpenSSL library + sha1: using OpenSSL library + using system zlib library nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/sbin/nginx" nginx configuration prefix: "/etc/nginx" nginx configuration file: "/etc/nginx/nginx.conf" nginx pid file: "/var/run/nginx/nginx.pid" nginx error log file: "/var/log/nginx/error.log" nginx http access log file: "/var/log/nginx/access.log" nginx http client request body temporary files: "/var/tmp/nginx/client/" nginx http proxy temporary files: "/var/tmp/nginx/proxy/" nginx http fastcgi temporary files: "/var/tmp/nginx/fcgi/" nginx http uwsgi temporary files: "/var/tmp/nginx/uwsgi" nginx http scgi temporary files: "scgi_temp" ###如果出现报错的话,应该是由于软件包的依赖关系所导致的,自行安装相应的软件包即可 [[email protected]_101 nginx-1.6.1]# make && make install
5、为nginx提供启动脚本
在nginx的官网上提供了相应的启动脚本(http://wiki.nginx.org/InitScripts),结合刚才的配置参数做出相应的修改即可:
[[email protected]_101 nginx-1.6.1]# vim /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/nginx.lock make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -` if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep ‘configure arguments:‘` for opt in $options; do if [ `echo $opt | grep ‘.*-temp-path‘` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
6、为启动脚本添加执行权限
[[email protected]_101 nginx-1.6.1]# chmod +x /etc/init.d/nginx
7、启动nginx
[[email protected]_101 nginx-1.6.1]# /etc/init.d/nginx start Starting nginx: [ OK ]
8、将nginx添加进服务列表,并设置为开机自启
[[email protected]_101 nginx-1.6.1]# chkconfig --add nginx [[email protected]_101 nginx-1.6.1]# chkconfig nginx on [[email protected]_101 nginx-1.6.1]# chkconfig nginx --list nginx 0:off1:off2:on3:on4:on5:on6:off
9、查看端口号
[[email protected]_101 nginx-1.6.1]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 17211 root 6u IPv4 124804 0t0 TCP *:http (LISTEN) nginx 17212 nginx 6u IPv4 124804 0t0 TCP *:http (LISTEN) [[email protected]_101 nginx-1.6.1]# netstat -tunlp | grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 17211/nginx
10、测试一下(根据服务器的IP地址进行访问)
注:由于做测试的主机是公司内网的测试机,博主现在周末在家休息时写的博客,所以这个测试就在本地使用curl工具给大家演示一下了。
[[email protected]_101 nginx-1.6.1]# curl http://192.168.0.101/
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
可以发现网页访问正常。
注:如果使用其他主机请求,导致无法访问,请检查iptables的配置信息,或者直接关闭iptables
11、配置nginx
1、编辑nginx的主配置文件
[[email protected]_101 ~]# vim /etc/nginx/nginx.conf ##在HTTP段添加一条配置项: http { include mime.types; include server.conf; ###添加这一行 default_type application/octet-stream;
2、在/etc/nginx/目录下创建server.conf文件
[[email protected]_101 ~]# vim /etc/nginx/server.conf server { listen 80; server_name www.test.com; location / { root /home/html; index index.html; } } [[email protected]_101 ~]# vim /home/html/index.html <h1>test file</h1>
3、本地绑定hosts
win7的路径为:C:\Windows\System32\drivers\etc\hosts
Linux的路径为: /etc/hosts
在上述文件中加入:
192.168.0.101 www.test.com
[[email protected]_101 ~]# /etc/init.d/nginx reload nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful Reloading nginx: [ OK ] [[email protected]_101 ~]# curl http://www.test.com <h1>test file</h1>
正常访问。
五、编译安装、配置mysql
1、创建用户与相应的目录
[[email protected]_101 ~]# groupadd -r mysql [[email protected]_101 ~]# useradd -r -g mysql -s /sbin/nologin mysql [[email protected]_101 ~]# mkdir /home/mysql #mysql的数据目录 [[email protected]_101 ~]# chown -R mysql:mysql /home/mysql/ [[email protected]_101 ~]# mkdir /usr/local/mysql #mysql的安装目录
2、解压缩
[[email protected]_101 ~]# tar zxvf mysql-5.6.20.tar.gz -C /opt/ [[email protected]_101 ~]# cd /opt/mysql-5.6.20/
3、编译安装
从MySQL5.5开始就要用cmake安装了,已不能用./configure编译安装,我们查看一下mysql5.6.12的安装目录,从下面的安装目录我们可以看到,里面根本没有configure文件,下面我们来说说cmake,
cmake的重要特性之一是其独立于源码(out-of-source)的编译功能,即编译工作可以在另一个指定的目录中而非源码目录中进行,这可以保证源码目录不受任何一次编译的影响,因此在同一个源码树上可以进行多次不同的编译,如针对于不同平台编译。
cmake指定编译选项的方式不同于make,其实现方式对比如下
./configure 对应的是 cmake . #注意:Linux命令行下cmake后面是需要带一个"."的
./configure --help 对应的是 cmake . -LH 或者是 ccmake
编译安装前,请确保自己的服务器已经安装过cmake
如果没有请执行:yum -y install cmake
[[email protected]_101 mysql-5.6.20]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/home/mysql -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL:STRING=bundled -DWITH_ZLIB:STRING=bundled -DENABLE_DOWNLOADS=1 -DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
出现报错:
-- Library mysqlclient depends on OSLIBS -lpthread;m;rt;dl -- Download failed, error: 7;"couldn‘t connect to server" -- To enable google test, please download http://googlemock.googlecode.com/files/gmock-1.6.0.zip to the directory /opt/mysql-5.6.20/source_downloads -- If you are inside a firewall, you may need to use an http proxy: export http_proxy=http://example.com:80 -- Library mysqlserver depends on OSLIBS -lpthread;m;rt;crypt;dl -- Configuring done -- Generating done -- Build files have been written to: /opt/mysql-5.6.20
自己手动下载(可能需要翻墙)该文件,上传至相应的目录,并删除CMakeCache.txt文件,重新执行cmake命令,又出现报错:
CMake Error: Problem with tar_extract_all(): Invalid argument CMake Error: Problem extracting tar: /usr/local/src/mysql-5.6.13/source_downloads/gmock-1.6.0.zip
谷歌了一下,需要个手动解压该文件:
[[email protected]_101 mysql-5.6.20]# cd source_downloads/ [[email protected]_101 source_downloads]# unzip gmock-1.6.0.zip [[email protected]_101 source_downloads]# cd gmock-1.6.0 [[email protected]_101 source_downloads]# cd gmock-1.6.0 [[email protected]_101 gmock-1.6.0]# ./configure [[email protected]_101 gmock-1.6.0]# make
由于博客的字数限制,剩下的内容将在下一篇博客中展示: