Linux_LEMP

目录

  • 目录
  • LEMP
    • Nginx
    • mysql
    • PHP
      • php-fpm Script

LEMP

Nginx是一个高性能的HTTP和反向代理服务器,也是一个 IMAP/POP3/SMTP 服务器,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、新浪、网易、腾讯等。

Nginx

step1. Install Nginx

yum install -y pcre-devel zlib-devel gcc openssl-devel
useradd -M -s /sbin/nologin nginx
tar zxvf nginx-XXX -C /usr/local/
./configure -prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-md5=/usr/lib --with-sha1=/usr/lib --with-http_gzip_static_module
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin
nginx -v #check nginx version

step2. Use nginx running script to change nginx service status.

##################################  Script 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: /opt/nginx/conf/nginx.conf
# pidfile: /opt/nginx/logs/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    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
    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
#############################SCRIPT END
cp nginx /etc/init.d
chmod +x /etc/init.d/nginx

step3. Edit nginx configure file
vim /usr/local/nginx/conf/nginx.conf

main     #Global config label
server   #virtual machine label
#For example:
########################## server section
server {
    listen       80;
    server_name  www.baidu.com;     #domainName
    charset utf-8;
    access_log  logs/baidu.access.log  main;
     location / {
        root   html;                    #root: website directory; html:website root directory url:/usr/local/nginx/html
        index  index.html index.htm index.php;     #welcome peger
    }
    error_page  404              /404.html;        #error peger
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
server {   --> add access flow monitor function
    listen        80;
    server_name www.nnice.com;
    location ~/status {      #'~': the meaning is domain(IP) or nginx service root directory
        stub_status    on;
        access_log    off;
    }
}
###########################
#events --> module setting
#For example:
########################### events section
events {
    worker_connections  1024;
    use epoll;    #use epoll kernel
}

mysql

step1. Install MySQL

tar zxvf cmake-XXX.tar.gz -C /usr/local
cd cmake
./configure && make && make install
tar zxvf mysql-XXX.tar.gz -C /usr/local
mv /usr/local/mysql-XXX /usr/local/mysql
cd mysql
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_SSL=yes  -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_READLINE=on

step2. initialization mysql

cp mysql/support-files/my-medium.cnf /etc/my.cnf
cp mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
/usr/local/mysql/scripts/mysql_install_db --user=mysql --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --> DB file store to /usr/local/mysql/data
useradd -M -s /sbin/nologin/ mysql
ln -s /usr/local/mysql/bin/*  /usr/local/bin
service mysqld restart
mysql_secure_installation  #optimization and set the password for mysql service
echo "//usr/local/mysql/lib" >> /etc/ld.so.conf
ldconfig

PHP

step1. Install PHP plugin

Install libmcrypt,mhash,mcrypt
tar jxvf php-XXX.tar.bz2
mv php-XXX.tar.bz2 php
cd php
./configure --prefix=/usr/local/php --with-gd --with-mcrypt --with-zlib --with-mysql=/usr/local/mysql --with-config-file-path=/usr/local/php/etc --enable-mbstring --enable-fpm && make && make install

step2. Initialization PHP

cp php/php.ini.development /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

vim php-fpm.conf

pid=run/php-fpm.pid #close php-fpm service by find the pid number
user=nginx
group=nginx
pm.max_spare_servers=35
/usr/local/php/sbin/php-fpm  #start php-fpm service
cp php-fpm /etc/init.d             #php-fpm script is php-fpm service status script

php-fpm Script

#! /bin/sh
#chkconfig:35 25 12
#description:php-fpm
set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="php-fpm daemon"
NAME=php-fpm
DAEMON=/usr/local/php/sbin/$NAME
PIDFILE=/usr/local/php/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed.

d_start() {
  /usr/local/php/sbin/php-fpm > /dev/null 2>&1
}

d_stop() {
  /bin/kill -SIGINT `cat /usr/local/php/logs/php-fpm.pid` > /dev/null 2>&1
}

d_restart() {
  /bin/kill -SIGUSE2 `cat /usr/local/php/logs/php-fpm.pid`  > /dev/null 2>&1
}

case "$1" in
  start)
        echo -n "Starting $DESC: $NAME"
        d_start
        echo "."
        ;;
  stop)
        echo -n "Stopping $DESC: $NAME"
        d_stop
        echo "."
        ;;
  restart)
        echo -n "Restarting $DESC: $NAME"
        d_stop
        sleep 1
        d_start
        echo "."
        ;;
  *)
          echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
          exit 3
        ;;
esac

exit 0
##################################SCRIPT END
chown +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig --list php-fpm
chkconfig php-fpm on

step4. Add php plugin to nginx service
vim /usr/local/nginx/conf/nginx.conf

Enable extra "location ~\.php$" section ,use php proxy
    location ~\.php$ {
         proxy_pass http://NginxServerIP:80
     }

Edit original “localtion \” section to “location ~.php$” , do not use proxy

service {
     location ~\.php$ {
     root    html
     include        fastcgi.conf
     }
     Index    index.php
 }
        service nginx restart
        service php-fpm restart
        service nginx restart
时间: 2024-10-03 00:36:39

Linux_LEMP的相关文章