Web服务器-部署企业LNMP架构
一、搭建Nginx服务
1、安装支持软件
pcre-devel
zlib-devel
gcc
gcc-c++
make
[root@www ~]# rpm -q pcre-devel zlib-devel gcc gcc-c++ make
package pcre-devel is not installed
package zlib-devel is not installed
gcc-4.4.7-4.el6.x86_64
gcc-c++-4.4.7-4.el6.x86_64
make-3.81-20.el6.x86_64
[root@www ~]# yum -y install pcre-devel zlib-devel openssl-devel vim lrzsz wget gcc c++ make
[root@www ~]# rpm -q pcre-devel zlib-devel gcc gcc-c++ make
pcre-devel-7.8-6.el6.x86_64
zlib-devel-1.2.3-29.el6.x86_64
gcc-4.4.7-4.el6.x86_64
gcc-c++-4.4.7-4.el6.x86_64
make-3.81-20.el6.x86_64
2、创建程序用户和组
[root@www ~]# useradd -M -s /sbin/nologin nginx
[root@www ~]# tail -1 /etc/passwd ;tail -1 /etc/group
nginx:x:1000:1000::/home/nginx:/sbin/nologin
nginx:x:1000:
3、编译安装Nginx
[root@www ~]# tar xf nginx-1.14.2.tar.gz -C /usr/src/
[root@www ~]# cd /usr/src/nginx-1.14.2/
[root@www nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install……
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre && make && make install
[root@www nginx-1.14.2]# cd /usr/local/nginx/
[root@www nginx]# ls
conf html logs sbin
[root@www nginx]#
[root@www nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
4、编写Nginx服务控制脚本
[root@www ~]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: 35 90 15
# description: Nginx Server Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
if [ -f $PIDF ]; then
echo "Nginx is running.. Start it is error"
else
$PROG
fi
;;
stop)
if [ -f $PIDF ]; then
kill -s QUIT $(cat $PIDF)
rm -rf $PIDF
else
echo "Nginx is stopping .. Stop it is error"
fi
;;
restart)
$0 stop
$0 start
;;
reload)
if [ -f $PIDF ]; then
kill -s HUP $(cat $PIDF)
else
echo "Nginx is stopping . reload it is error"
fi
;;
status)
if [ -f $PIDF ]; then
echo "Nginx is running"
else
echo "Nginx is stopping"
fi
;;
*)
echo "Usage: $0 (start|stop|restart|reload|status)"
exit 1
esac
exit 0
5、配置Nginx服务开机自启
[root@www ~]# echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local
[root@www ~]# chmod +x /etc/rc.d/rc.local
[root@www ~]# nginx
[root@www ~]# netstat -anpt |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6519/nginx
6、修改nginx.conf主配置文件,添加两个虚拟主机
[root@www ~]# cd /usr/local/nginx/conf/
[root@www conf]# cp -p nginx.conf nginx.conf.origin
[root@www conf]# vim nginx.conf
user nginx nginx;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name www.amber.com;
charset utf-8;
access_log logs/www.amber.com.access.log main;
location / {
root html/www.amber.com;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.movies.com;
charset utf-8;
access_log logs/www.movies.com.access.log main;
location / {
root /www.movies.com;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
nginx配置优化最终配置文件详情
[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf
user nginx;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
client_header_timeout 60;
client_header_buffer_size 32k;
large_client_header_buffers 4 128k;
client_body_timeout 60;
client_max_body_size 512m;
open_file_cache max=65535 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
gzip on;
gzip_static on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_vary on;
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss;
#gzip_vary on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 512k;
fastcgi_buffers 6 512k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;
fastcgi_intercept_errors on;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
proxy_buffer_size 32k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 54k;
proxy_temp_file_write_size 2m;
proxy_ignore_client_abort on;
proxy_cache_path /usr/local/nginx/cache_temp levels=2:2 keys_zone=cache_temp:128m inactive=30m max_size=2g;
proxy_cache_valid 200 302 10m;
include /usr/local/nginx/conf/conf.d/*.conf;
}
nginx主配置文件下include子配置文件详情--
也就是说当访问127.0.0.1/index.php的时候,需要读取网站根目录下面的index.php文件,如果没有配置这一配置项时,nginx不回去网站根目录下访问.php文件,所以返回空白
[root@localhost conf.d]# cat www.amber.conf
server {
listen 80;
server_name www.amber.com;
charset utf-8;
access_log logs/www.amber.com.access.log main;
location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {
expires 1d;
}
location / {
root html/www.amber.com;
index index.php 1.html index.html index.htm;
}
location ~* \.(jpg|gif|png|swf)$ {
valid_referers none blocked *.amber.com amber.com;
if ($invalid_referer) {
rewrite ^/ http://www.amber.com/error.jpg;
#return 403;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
七、对FPM模块进行参数优化
Nginx的PHP解析功能实现如果是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数调整。
FPM优化参数:
pm 使用哪种方式启动fpm进程,可以说static和dynamic,前者将产生 固定数量的fpm进程,后缀将以动态的方式产生fpm进程
pm.max_children static方式下开启的fpm进程数
pm.start_servers 动态方式下初始的fpm进程数量
pm.min_spare_servers 动态方式下最小的fpm空闲进程数
pm.max_spare_servers 动态方式下最大的fpm空闲进程数
注:以上调整要根据服务器的内存与服务器负载进行调整
示例:
服务器为云服务器,运行了个人论坛,内存为1.5G,fpm进程数为20,内存消耗近1G,处理比较慢
# vim /usr/local/php5/etc/php-fpm.conf
优化参数调整:
pm = dynamic
pm=start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
[root@www conf]# mkdir /usr/local/nginx/html/www.amber.com
[root@www conf]# mkdir /www.movies.com
[root@www conf]# chown nginx:nginx /www.movies.com/
[root@www conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@www conf]# ulimit -n 65000
[root@www conf]# echo "ulimit -n 65000" >>/etc/rc.local
[root@www conf]# 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
[root@www conf]# /etc/init.d/nginx restart
7、创建测试页面,进行测试
[root@www ~]# echo "<h1>www.amber.com</h1>" >/usr/local/nginx/html/www.amber.com/index.html
[root@www ~]# echo "<h1>www.movies.com</h1>" >/www.movies.com/index.html
修改宿主机host文件:
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DSYSCONFDIR=/etc && make && make install
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -Dnetc && make && make install
二、搭建Mysql数据 库
1、安装支持软件
[root@www ~]# rpm -q ncurses-devel
package ncurses-devel is not installed
[root@www ~]# yum -y install ncurses-devel
[root@www ~]# tar xf cmake-2.8.6.tar.gz -C /usr/src/
[root@www ~]# cd /usr/src/cmake-2.8.6/
[root@www cmake-2.8.6]# ./configure && gmake && gmake install
……
2、编译安装Mysql数据库
[root@www cmake-2.8.6]# cd
[root@www ~]# tar xf mysql-5.5.22.tar.gz -C /usr/src/
[root@www ~]# cd /usr/src/mysql-5.5.22/
[root@www mysql-5.5.22]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -D_netc && make && make install
……
3、安装后调整优化
[root@www ~]# echo "PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
[root@www ~]# . /etc/profile
[root@www ~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
[root@www ~]# /bin/cp -p /usr/src/mysql-5.5.22/support-files/my-medium.cnf /etc/my.cnf
[root@www ~]# /bin/cp -p /usr/src/mysql-5.5.22/support-files/mysql.server /etc/init.d/mysqld
[root@www ~]# chmod +x /etc/init.d/mysqld
[root@www ~]# chkconfig --add mysqld
[root@www ~]# chkconfig --list mysqld
mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
4、初始化数据库
[root@www ~]# useradd -M -s /sbin/nologin mysql
[root@www ~]# chown -R mysql:mysql /usr/local/mysql/
[root@localhost ~]# vim /etc/init.d/mysqld
46 basedir=/usr/local/mysql
47 datadir=/usr/local/mysql/data
[root@www ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql
。。。。。。
You can start the MySQL daemon with:
cd /usr/local/mysql/ ; /usr/local/mysql//bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql//mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/local/mysql//scripts/mysqlbug script!
5、启动Mysql服务
[root@www ~]# /etc/init.d/mysqld start
Starting MySQL... [确定]
[root@www ~]# netstat -anpt| grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 25190/mysqld
6、创建root用户密码
[root@www ~]# mysqladmin -uroot password "123123";history -c
三、安装PHP服务
1、安装支持软件
gd
libxml2-devel
libjpeg-devel
libpng-devel
[root@www ~]# yum -y install gd libxml2-devel libjpeg-devel libpng-devel
2、编译安装PHP
[root@www ~]# tar xf php-5.3.28.tar.gz -C /usr/src/
[root@www ~]# cd /usr/src/php-5.3.28/
[root@www php-5.3.28]# ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql/ --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm --with-jpeg-dir=/usr/lib --disable-fileinfo&& make && make install
:https://blog.51cto.com/vaedit/1983603
3、安装后优化调整
[root@www ~]# cp -p /usr/src/php-5.3.28/php.ini-development /usr/local/php5/php.ini
[root@www ~]# ln -s /usr/local/php5/bin/* /usr/local/bin/
[root@www ~]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/
4、安装ZendGuardLoader(PHP的优化模块)
[root@www ~]# tar xf ZendGuardLoader-php-5.3-linux-glibc23-x86_64.tar.gz -C /usr/src/
[root@www ~]# cp /usr/src/ZendGuardLoader-php-5.3-linux-glibc23-x86_64/php-5.3.x/ZendGuardLoader.so /usr/local/php5/lib/php/
[root@www ~]# echo -e "zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so\nzend_loader.enable=1" >> /usr/local/php5/php.ini
[root@www ~]# tail -2 /usr/local/php5/php.ini
zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so
zend_loader.enable=1
5、启用php-fpm进程
[root@www ~]# cd /usr/local/php5/etc/
[root@www etc]# cp -p php-fpm.conf.default php-fpm.conf
[root@www etc]# vim php-fpm.conf
25 pid = run/php-fpm.pid //确认pid文件位置
140 user = nginx //程序用户
141 group = nginx //程序组
217 pm.max_children = 50 //子进程的最大数
222 pm.start_servers = 20 //启动时开启的进程数
227 pm.min_spare_servers = 5 //最少空闲进程数
232 pm.max_spare_servers = 35 //最大空闲进程数
[root@www etc]# php-fpm
[root@www etc]# netstat -anpt |grep php-fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 55040/php-fpm
6、修改/etc/init.d/nginx服务脚本
将下文脚本覆盖原脚本:
[root@www etc]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Server Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
PROG_FPM="/usr/local/sbin/php-fpm"
PIDF_FPM="/usr/local/php5/var/run/php-fpm.pid"
case "$1" in
start)
$PROG
$PROG_FPM
;;
stop)
kill -s QUIT $(cat $PIDF)
kill -s QUIT $(cat $PIDF_FPM)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 (start|stop|restart|reload)"
exit 1
esac
exit 0
[root@www ~]# chkconfig --del nginx
[root@www ~]# chkconfig --add nginx
[root@www ~]# /etc/init.d/nginx stop
[root@www ~]# /etc/init.d/nginx start
[root@www ~]# netstat -anpt |egrep "nginx|php-fpm"
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 55076/nginx
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 55080/php-fpm
7、配置Nginx支持PHP解析
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
……
server {
listen 80;
server_name www.movies.com;
charset utf-8;
access_log logs/www.movies.com.access.log main;
location / {
root /www.movies.com;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /www.movies.com;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
……
[root@www ~]# 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
[root@www ~]# /etc/init.d/nginx restart
8、PHP页面访问测试
[root@www ~]# vim /www.movies.com/test.php
<?php
$link=mysql_connect('localhost','root','123123');
if($link) echo "<h1>successful</h1>";
mysql_close();
?>
9、如果解析的PHP页面为空白那么修改
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params;
}
10、如果页面显示的是find not file 那么 可以将fastcgi_param SCRIPT_FIL ENAME 后面的字段改为其访问的绝对路径
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /www.movies.com/$fastcgi_script_name;
include fastcgi_params;
}
NGINX日志切割多虚拟机
# Created Time: Sun 03 2019年 12月 05日 星期四 14:51:252 AM UTC
#########################################################################
#!/bin/bash
# nginx日志分割
YU_NAME_ONE="www.amber.com"
YU_NAME_TWO="www.movies.com"
ACCESS_LOG_ONE="/usr/local/nginx/logs/${YU_NAME_ONE}.access.log"
ACCESS_LOG_TWO="/usr/local/nginx/logs/${YU_NAME_TWO}.access.log"
ERROR_LOG="/usr/local/nginx/logs/error.log"
NGINX="/usr/local/nginx/sbin/nginx"
DATE=`date -d "-1 day" +%Y-%m-%d`
BACKUP_DIR_ONE="/backup/logs/nginx/${YU_NAME_ONE}"
BACKUP_DIR_TWO="/backup/logs/nginx/${YU_NAME_TWO}"
STATUS=`netstat -anpt | grep :80 |wc -l` # >1表示nginx已启动
[ -d ${BACKUP_DIR_ONE} ] || mkdir -p ${BACKUP_DIR_ONE} && [ -d ${BACKUP_DIR_TWO} ] || mkdir -p ${BACKUP_DIR_TWO}
if [ $STATUS -gt 0 ];then
tar zcf ${BACKUP_DIR_ONE}/${YU_NAME_ONE}-$(date -d "-1 day" +%Y-%m-%d).tar.gz ${ACCESS_LOG_ONE} --remove &>/dev/null
tar zcf ${BACKUP_DIR_TWO}/${YU_NAME_TWO}-$(date -d "-1 day" +%Y-%m-%d).tar.gz ${
ACCESS_LOG_TWO} --remove &>/dev/null
$NGINX -s reopen
else
echo "Error: Nginx is stopped." | tee -a $ERROR_LOG
fi
11、实现Nginx的日志切割
#########################################################################
# File Name: nginx_cut_logs.sh
# Author: Amber
# Created Time: Sun 03 Mar 2019 02:29:22 AM UTC
#########################################################################
#!/bin/bash
# nginx日志分割
ACCESS_LOG="/usr/local/nginx/logs/access.log"
ERROR_LOG="/usr/local/nginx/logs/error.log"
NGINX="/usr/local/nginx/sbin/nginx"
DATE=`date -d "-1 day" +%Y%m%d`
BACKUP_DIR="/backup/logs/nginx/"
STATUS=`ps aux | grep "master process nginx" | wc -l` # >1表示nginx已启动
[ -d $BACKUP_DIR ] || mkdir -p $BACKUP_DIR
if [ $STATUS -gt 1 ];then
mv $ACCESS_LOG ${BACKUP_DIR}/access.log-$DATE
$NGINX -s reopen
gzip -9 ${BACKUP_DIR}/access.log-$DATE
else
echo "Error: Nginx is stopped." | tee -a $ERROR_LOG
fi
find $BACKUP_DIR -name "*.log*" -mtime +30 | xargs rm -f
12、执行.sh脚本时,报错 "start.sh /bin/bash^M: 坏的解释器:没有那个文件或目录”,因为 .sh文件是从windows拷贝过来的,所以多了\r,执行:
13、sed -i 's/\r$//' start.sh
四、在LNMP平台,部署SKYUC应用
1、解压SKYUC,部署程序代码
[root@www ~]# rpm -q unzip
unzip-6.0-1.el6.x86_64
[root@www ~]# unzip SKYUC.v3.4.2.SOURCE.zip
[root@www ~]# cd SKYUC.v3.4.2.SOURCE/
[root@www SKYUC.v3.4.2.SOURCE]# ls
Change Log.txt Readme.txt -??+??+?.txt URLRewrite.txt wwwroot
[root@www SKYUC.v3.4.2.SOURCE]# cp -rp wwwroot/ /www.movies.com/skyuc
[root@www SKYUC.v3.4.2.SOURCE]# cd /www.movies.com/skyuc/
[root@www skyuc]# chown -R nginx:nginx admincp/ data/ templates/ upload/
2、创建数据库
[root@www ~]# mysql -uroot -p123123;history -c
……
mysql> create database skyucdb;
Query OK, 1 row affected (0.04 sec)
mysql> grant all on skyucdb.* to runskyuc@localhost identified by 'admin123';
Query OK, 0 rows affected (0.04 sec)
mysql> quit
Bye
3、安装Web应用
[root@www ~]# cd /www.movies.com/skyuc/
[root@www skyuc]# mv install/ install.bak/
[root@www skyuc]# chmod 600 install.bak/
再刷新网页