一、 环境
安装包下载地址:http://nginx.org/en/download.html
service firewalld stop
systemctl disable firewalld
将安装包通过WinSCP传到虚拟机
二、 nginx安装
yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
zlib-devel
[[email protected] ~]# useradd -M -s /sbin/nologin nginx //创建程序用户nginx,不能登录,没有家目录
[[email protected] ~]# tar xzvf nginx-1.13.9.tar.gz
[[email protected] ~]# cd nginx-1.13.9
[[email protected] nginx-1.13.9]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module //日志分析模块
[[email protected] nginx-1.13.9]# make && make install //编译及安装
[[email protected] nginx-1.13.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //为nginx可执行程序做软连接,让系统可识别
nginx -t //配置文件语法检查
nginx //启动服务
killall -1 nginx //安全重启
killall -3 nginx //停止服务
----------------------------------------制作管理角本---------------------------
[[email protected] nginx-1.13.9]# vi /etc/init.d/nginx
#!/bin/bash
chkconfig: 35 99 20 //3,5级别自动运行,第99个进程开启,第20个进程结束
description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
:x
[[email protected] nginx-1.13.9]# chmod +x /etc/init.d/nginx //给管理脚本添加执行权限
[[email protected] nginx-1.13.9]# chkconfig --add nginx //将nginx添加到chkconfig系统管理工具中
------------------------下面对nginx配置文件进行初始配置-------------------
[[email protected] nginx-1.13.9]# vi /usr/local/nginx/conf/nginx.conf
user nginx nginx; //修改nginx启动进程的属主和属组
worker_processes 1; //CPU核数,几核便设为几
error_log logs/error.log info; //修改错误日志的级别
注释:日志级别共有这几种:debug info notice warn error crit 其中规则是向上记录,即info级别的日志会涵盖notice warn error crit,低级的包涵高级的,不放在配置文件里面
events {
use epoll; //新增此行 默认使用select/poll
worker_connections 1024; //表示1个进程允许1024个连接,如果设置超过1024则需要修改ulimit上限,否则报错(ulimit -n 65500//查看和更改系统本地打开资源数
ulimit -n 65500 >> /etc/rc.local)
log_format main //定义日志格式 把前面的#号去掉
[[email protected] nginx-1.13.9]# service nginx restart
配置完之后重启,现在nginx服务器已经可以支持访问了
------------------------------------配置nginx自带的日志统计模块------------------------------------
[[email protected] nginx-1.13.9]# vi /usr/local/nginx/conf/nginx.conf
location ~ /status {
stub_status on;
access_log off;
}
[[email protected] nginx-1.13.9]# service nginx reload
[[email protected] nginx-1.13.9]# cat /usr/local/nginx/logs/access.log
浏览器:http://IP/status //当前的活动连接数,已处理的连接数,成功的TCP握手次数,已处理的请求数。
------------------------以下配置nginx的验证功能----------------------------
[[email protected] nginx-1.13.9]# yum install httpd-tools -y //安装apache的工具,借助工具创建访问用户
[[email protected] nginx-1.13.9]# htpasswd -c /usr/local/nginx/passwd.db jack //创建数据库验证文件,并把jack用户添加进去
New password:输密码
Re-type new password:确认密码
Adding password for user jack
[[email protected] nginx-1.13.9]# chmod 400 /usr/local/nginx/passwd.db //提高数据安全性
[[email protected] nginx-1.13.9]# chown nginx /usr/local/nginx/passwd.db //更改属主
[[email protected] nginx-1.13.9]# vi /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
allow 192.168.80.0/24;
deny all;
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db; //注意这边的文件必须与上面创建的数据库验证文件统一
}
[[email protected] nginx-1.13.9]# service nginx restart
再次访问需要输入用户名和密码
三、 以下配置虚拟主机功能
-----------基于域名----------IP和端口都相同但是域名不相同----------
[[email protected] nginx-1.13.9]# vi /usr/local/nginx/conf/nginx.conf
“在最后一行上面插入,最后一行是个},结尾共4个}”
server {
listen 80;
server_name www.aa.com;
charset utf-8;
access_log logs/aa.access.log main;
location / {
root /usr/local/nginx/html;
index index.html index.htm;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
server {
listen 80;
server_name www.bc.com;
charset utf-8;
access_log logs/bc.access.log main;
location / {
root /var/www/bc;
index index.html index.htm;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
下面的去掉注释
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"‘;
[[email protected] nginx-1.13.9]# mkdir /var/www/aa -p
[[email protected] nginx-1.13.9]# mkdir /var/www/bc
在两个虚拟主机的网站目录添加主页
[[email protected] nginx-1.13.9]# vi /var/www/aa/index.html
<h1>www.aa.com</h1>
[[email protected] nginx-1.13.9]# vi /var/www/bc/index.html
<h1>www.bc.com</h1>
修改微软的hosts文件进行虚拟主机测试:
windows xp/2003/vista/2008/7/8用户HOSTS文件是在“c:\windows\system32\drivers\etc
hosts文件加上此句:192.168.80.102 www.aa.com www.bc.com
[[email protected] nginx-1.13.9]# nginx –t
[[email protected] nginx-1.13.9]# service nginx restart
[[email protected] nginx-1.13.9]# killall -1 nginx
四、 安装MySQL
[[email protected] ~]# yum -y install ncurses ncurses-devel bison cmake
[[email protected] ~]# useradd -s /sbin/nologin mysql //手动建一个账号
[[email protected] ~]# tar xf mysql-boost-5.7.20.tar.gz -C /opt/ //解压的目录要足够大,不然会报错
[[email protected] mysql-5.7.20]# cd /opt/mysql-5.7.20/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ //指定sock文件的路劲
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \ //数据文件夹
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
[[email protected] mysql-5.7.20]# make && make install
[[email protected] mysql-5.7.20]# chown -R mysql:mysql /usr/local/mysql/
[[email protected] mysql-5.7.20]# vi /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES //固定格式
:x
[[email protected] mysql-5.7.20]# chown mysql:mysql /etc/my.cnf
[[email protected] mysql-5.7.20]# echo ‘PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH‘ >> /etc/profile //把这两个路径添加到环境变量中,并放到profile文件中使之开机自运行,否则不生效
[[email protected] mysql-5.7.20]# echo ‘export PATH‘ >> /etc/profile //也可以软链接
[[email protected] mysql-5.7.20]# source /etc/profile //立即生效
[[email protected] mysql-5.7.20]# cd /usr/local/mysql/
bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data //mysql初始化
[[email protected] mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[[email protected] mysql]# systemctl daemon-reload
[[email protected] mysql]# systemctl start mysqld
[[email protected] mysql]# netstat -anpt | grep 3306
[[email protected] mysql]# systemctl enable mysqld
[[email protected] mysql]# mysqladmin -u root -p password "123"
Enter password:(初始密码是空,直接回车就好)
[[email protected] mysql]# mysql -u root –p
mysql>
mysql>
mysql> quit
Bye
五、 安装PHP
[[email protected] mysql]# yum -y install \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 \
libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel //安装需要支持的插件
[[email protected] ~]# yum -y install bzip2
[[email protected] ~]# tar xjvf php-7.1.10.tar.bz2 -C /opt/
[[email protected] ~]# cd /opt/php-7.1.10
[[email protected] php-7.1.10]# ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \ //配置协作
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip \ //支持压缩
--enable-fpm //支持动态页面fpm功能 (注意这个要手打,直接复制容易失败)
[[email protected] php-7.1.10]# make && make install
[[email protected] php-7.1.10]# cp php.ini-development /usr/local/php/lib/php.ini //复制php配置文件模板作为php配置文件
[[email protected] php-7.1.10]# vi /usr/local/php/lib/php.ini //编辑php配置文件
mysqli.default_socket = /usr/local/mysql/mysql.sock //编辑mysql的sock文件位置
date.timezone = Asia/Shanghai //选择时区
[[email protected] php-7.1.10]# /usr/local/php/bin/php –m //验证安装的模块
-----------配置及优化FPM模块--------
[[email protected] php-7.1.10]# cd /usr/local/php/etc/
[[email protected] etc]# cp php-fpm.conf.default php-fpm.conf //复制php的fpm模块的配置文件模板为配置文件。php只识别php-fpm.conf为配置文件
[[email protected] etc]# cd /usr/local/php/etc/php-fpm.d/
[[email protected] php-fpm.d]# cp www.conf.default www.conf //复制fpm的www配置文件模板为配置文件,fpm只识别www.conf的配置文件
[[email protected] php-fpm.d]# cd /usr/local/php/etc/
[[email protected] etc]# vi php-fpm.conf //配置fpm模块
pid = run/php-fpm.pid //将分号去掉
;user = nginx
;group = nginx //添加user和group
[[email protected] etc]# /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini //启动fpm模块
[[email protected] etc]# netstat -anpt | grep 9000
[[email protected] etc]# ln -s /usr/local/php/bin/* /usr/local/bin/ //将php的可执行程序放到系统可识别环境中便于执行
[[email protected] etc]# ps aux | grep -c "php-fpm" //统计进程数
-----更新启动脚本,让fpm模块也可进行启动管理----------
[[email protected] etc]# vi /etc/init.d/nginx
#!/bin/bash
chkconfig: 35 99 20
description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
PROG_FPM="/usr/local/php/sbin/php-fpm"
PIDF_FPM="/usr/local/php/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
-------------------以下是让nginx支持PHP功能--------------
[[email protected] etc]# vi /usr/local/nginx/conf/nginx.conf
location ~ .php$ {
root /usr/local/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; //注意目录名称,为网站根目录必须为绝对路径
include fastcgi_params;
}
[[email protected] etc]# vi /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
[[email protected] etc]# nginx -t
[[email protected] etc]# service nginx restart
注意,为了防止干扰,需要把虚拟主机配置删除
在网页测试“http://192.168.80.193/index.php
-----------------------------------下面测试数据库工作是否正常---------------------
[[email protected] etc]# mysql -u root -p
Enter password:密码
mysql> CREATE DATABASE bbs; //创建数据库为bbs
mysql> GRANT all ON bbs. TO ‘bbsadm‘@‘%‘ IDENTIFIED BY ‘admin123‘; //将数据库所有权限给bbsadm,密码为admin123
mysql> GRANT all ON bbs.* TO ‘bbsadm‘@‘localhost‘ IDENTIFIED BY ‘admin123‘; //允许
mysql> flush privileges; //刷新权限
mysql> quit
[[email protected] etc]# vi /usr/local/nginx/html/index.php
<?php
$link=mysqli_connect(‘192.168.80.102‘,‘bbsadm‘,‘admin123‘);
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
在网页测试“http://IP/index.php”
---------------------以下安装论坛---------------------------
[[email protected] etc]# yum install -y unzip
[[email protected] etc]# unzip Discuz_X3.4_SC_UTF8_0101.zip -d /opt //解压缩
[[email protected] ~]# cd /opt/dir_SC_UTF8/ //进入解压目录
[[email protected] dir_SC_UTF8]# cp -r upload/ /usr/local/nginx/html/bbs
[[email protected] dir_SC_UTF8]# cd /usr/local/nginx/html/bbs
[[email protected] bbs]# chown -R root:nginx ./config/
[[email protected] bbs]# chown -R root:nginx ./data/
[[email protected] bbs]# chown -R root:nginx ./uc_client/
[[email protected] bbs]# chown -R root:nginx ./uc_server/
[[email protected] bbs]#
[[email protected] bbs]# chmod -R 777 ./config/
[[email protected] bbs]# chmod -R 777 ./data/
[[email protected] bbs]# chmod -R 777 ./uc_client/
[[email protected] bbs]# chmod -R 777 ./uc_server/
访问http://IP/bbs/install/index.php //安装论坛
访问地址为http://IP/bbs/index.php
http://IP/bbs/admin.php //管理后台
-------------------------------------配置动静分离---------------------------
删除访问控制
Nginx主机上面配置:将b.jpg传到服务器
[[email protected] html]# vi /usr/local/nginx/conf/nginx.conf
location ~ .php$ {
proxy_pass http://192.168.80.101;
} //把PHP动态请求转给192.168.80.101
[[email protected] html]# rm index.php
[[email protected] html]# service nginx restart
Apache服务器上面配置:
[[email protected] ~]# cd /usr/local/httpd/htdocs/
[[email protected] htdocs]# vi index.html
<html>
<body>
<img src="http://192.168.80.102/b.jpg">
</body>
</html>
[[email protected] htdocs]# service httpd restart
浏览器输nginx服务器ip测试:
Nginx服务器修改:
[[email protected] html]# vi /usr/local/nginx/conf/nginx.conf
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
root html;
expires 1d;
} //这些静态内容从本地读取
[[email protected] html]# service nginx restart
Apache服务器上面配置:
[[email protected] htdocs]# vi /usr/local/httpd/htdocs/index.php
<html>
<body>
<img src=" b.jpg">
</body>
</html>
[[email protected] htdocs]# service httpd restart
抓包测试:
原文地址:http://blog.51cto.com/13572519/2083783