Lnmp簡介
LNMP代表的就是:Linux系統下Nginx+MySQL+PHP這種網站服務器架構。
Nginx是一個小巧而高效的Linux下的Web服務器軟件,Nginx性能穩定、功能豐富、運維簡單、處理靜態文件速度快且消耗系統資源極少。
作爲 Web 服務器:相比 Apache,Nginx 使用更少的資源,支持更多的併發連接,體現更高的效率。
作爲負載均衡服務器:Nginx 既可以在內部直接支持 Rails 和 PHP,也可以支持作爲 HTTP代理服務器對外進行服務。Nginx 用C編寫,不論是系統資源開銷還是CPU使用效率都比Perlbal要好的多。
作爲郵件代理服務器:Nginx同時也是一個非常優秀的郵件代理服務器(最早開發這個產品的目的之一也是作爲郵件代理服務器),Last/fm 描述了成功並且美妙的使用經驗。
Nginx 安裝非常的簡單,配置文件非常簡潔(還能夠支持perl語法)。Nginx支持平滑加載新的配置,還能夠在不間斷服務的情況下進行軟件版本的升級。
實驗環境
- [root@localhost ~]# uname -r
- 2.6.18-164.el5
- [root@localhost ~]# cat /etc/redhat-release
- Red Hat Enterprise Linux Server release 5.4 (Tikanga)
一、安裝nginx
1.解決依賴關係
編譯安裝nginx需要事先需要安裝開發包組"Development Tools"和 "Development Libraries"。同時,還需要專門安裝pcre-devel包:
[root@localhost ~]# yum grouplist |less
[root@localhost ~]# yum list all |grep pcre
This system is not registered with RHN.
RHN support will be disabled.
pcre.i386 6.6-2.el5_1.7 installed
pcre-devel.i386 6.6-2.el5_1.7 rhel-server
[root@localhost ~]# yum install pcre-devel –y ##安裝pcre-devel
2、解壓縮
[root@localhost ~]# tar -zxvf nginx-1.0.11.tar.gz -C /usr/local/src
3.首先添加用戶nginx,實現以之運行nginx服務進程
[root@localhost ~]# groupadd -r nginx
[root@localhost ~]# useradd -r -g nginx -s /bin/false -M nginx
4.編譯和安裝
[root@localhost ~]# cd /usr/local/src/nginx-1.0.11/
[root@localhost 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 \
--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 \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
[root@localhost nginx-1.0.11]# make && make install
5.爲nginx提供sysv服務腳本
[root@localhost ~]# vim /etc/rc.d/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/subsys/nginx
-
- make_dirs() {
- # make required directories
- user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
- 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.添加至服務列表
[root@localhost ~]# chmod +x /etc/rc.d/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig nginx on ##設置開機啓動
[root@localhost ~]# service nginx start ##啓動
7.查看端口
[root@localhost ~]# netstat -tupln |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5712/nginx.conf
##可以看見,80端口已經開啓
8.nginx的測試
二、安裝mysql
1.解壓縮mysql-5.5.15-linux2.6-i686
[root@localhost ~]# tar zxvf mysql-5.5.15-linux2.6-i686.tar.gz -C /usr/local/
2.新建用戶以安全方式運行進程
[root@localhost ~]# groupadd -r mysql
[root@localhost ~]# useradd -g mysql -r -s /sbin/nologin -M -d /mydat/data mysql
[root@localhost ~]# mkdir -pv /mydata/data
[root@localhost ~]# chown -R mysql:mysql /mydata/data
3.安裝並初始化數據庫
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.5.15-linux2.6-i686 mysql
##建立一個軟連接
[root@localhost local]# cd mysql
[root@localhost mysql]# chown -R mysql:mysql .
[root@localhost mysql]# scripts/mysql_install_db --user=mysql
--datadir=/mydata/data/
##--user定義數據庫的所屬主,--datadir定義數據庫安裝到哪裏
##./bin/mysqladmin -u root password 'new-password'
##./bin/mysqladmin -u root -h localhost.localdomain password 'new-password'
##Alternatively you can run:
##./bin/mysql_secure_installation
[root@localhost mysql]# chown -R root .
4.爲mysql拷貝配置文件
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf
[root@localhost mysql]# vim /etc/my.cnf
38 # Try number of CPU's*2 for thread_concurrency
39 thread_concurrency = 2
##thread_concurrency的值爲你的CPU個數的2倍
40 datadir = /mydata/data
##mysql數據文件的存放位置
5.爲mysql提供sysv服務腳本
[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
6.添加至服務列表
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# service mysqld start
Starting MySQL................. [ OK ]
[root@localhost mysql]# chkconfig mysqld on
##設置開機啓動
三、安裝php
1.解壓縮php-5.3.7
[root@localhost ~]# tar -jxvf php-5.3.7.tar.bz2 -C /usr/local/src
2.編譯並安裝
[root@localhost ~]# cd /usr/local/src/php-5.3.7/
[root@localhost php-5.3.7]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm
--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 --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl
[root@localhost php-5.3.7]# make && make test && make install
3.爲php提供配置文件
[root@localhost php-5.3.7]# cp php.ini-production /etc/php.ini
4.爲php-fpm提供Sysv init腳本,並將其添加至服務列表
[root@localhost php-5.3.7]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@localhost php-5.3.7]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-5.3.7]# chkconfig --add php-fpm
[root@localhost php-5.3.7]# chkconfig php-fpm on
5.爲php-fpm提供配置文件並編輯配置文件
[root@localhost]# cp /usr/local/php/etc/php-fpm.conf.default
/usr/local/php/etc/php-fpm.conf
[root@localhost]# vim /usr/local/php/etc/php-fpm.conf
修改的行如下(配置pm.的相關選項爲你所需要的值,並啓用pid文件)
25 pid = /usr/local/php/var/run/php-fpm.pid
161 pm.max_children = 50
166 pm.start_servers = 20
171 pm.min_spare_servers = 5
176 pm.max_spare_servers = 35
6.接下來就可以啓動php-fpm了
[root@localhost]# service php-fpm start
使用如下命令來驗正(如果此命令輸出有中幾個php-fpm進程就說明啓動成功了):
[root@localhost]# ps aux | grep php-fpm
四、整合nginx和php
1.編輯nginx配置文件,配置如下
[root@localhost ~]# vim /etc/nginx/nginx.conf
43 location / {
44 root html;
45 index index.php index.html index.htm;
46 }
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
70 include fastcgi_params;
71 }
2.編輯fastcgi_params,內容如下
[root@localhost ~]# vim /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;
五、測試
[root@localhost ~]# cd /usr/html/
[root@localhost html]# mv index.html index.php
[root@localhost html]# vim index.php
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
<?php
phpinfo();
?>
[root@localhost html]# vim index.php
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
<?php
$link=mysql_connect('127.0.0.1','root','');
if($link)
echo "hello ,it is ok !";
else "sorry ,it is fail !";
?>