yum -y install \ |
pcre-devel \ |
zlib-devel //安装Nginx必备插件 |
useradd -M -s /sbin/nologin nginx //创建程序用户nginx,不能登录,没有家目录 |
|
tar xzvf nginx-1.13.9.tar.gz |
cd nginx-1.13.9 |
|
./configure \ |
--prefix=/usr/local/nginx \ |
--user=nginx \ |
--group=nginx \ |
--with-http_stub_status_module //日志分析模块 |
|
make && make install 编译及安装 |
|
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //为nginx可执行程序做软连接,让系统可识别 |
nginx -t //检查nginx配置文件语法 |
nginx //启动服务 |
killall -1 nginx //安全重启 |
killall -3 nginx //停止服务 |
|
下面制作nginx系统服务管理脚本 |
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 |
|
chmod +x /etc/init.d/nginx //给管理脚本添加执行权限 |
chkconfig --add nginx //将nginx添加到chkconfig系统管理工具中 |
|
下面对nginx配置文件进行初始配置 |
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; //新增此行,使用epoll模式运行,默认使用的是select/poll模式 |
worker_connections 1024; //表示1个进程允许1024个连接,如果设置超过1024则需要修改ulimit上限,否则报错 |
} |
log_format main //定义日志格式 把前面的#号去掉 |
配置完之后重启,现在nginx服务器已经可以支持访问
|
|
|
|
|
|
|
|
|
|
ulimit -n 65500//查看和更改系统本地打开资源数 |
ulimit -n 65500 >> /etc/rc.local |
|
下面我们可以配置nginx自带的日志统计模块 |
vi /usr/local/nginx/conf/nginx.conf |
location ~ /status { |
stub_status on; //日志统计模块开启 |
access_log off; |
} //在server 模块里的error_page 上面增加 |
service nginx reload |
http://IP/status //当前的活动连接数,已处理的连接数,成功的TCP握手次数,已处理的请求数。 |
|
|
|
|
|
|
可以配置nginx的访问控制功能了 |
yum install httpd-tools //安装apache的工具,借助工具创建访问用户 |
htpasswd -c /usr/local/nginx/passwd.db jack //创建数据库验证文件,并把jack用户添加进去 |
chmod 400 /usr/local/nginx/passwd.db //提高数据安全性 |
chown nginx /usr/local/nginx/passwd.db //更改属主 |
|
vi /usr/local/nginx/conf/nginx.conf |
location / { |
root html; |
index index.html index.htm; |
allow 192.168.80.0/24; //允许80段的访问该主页 |
deny all; |
auth_basic "secret"; |
auth_basic_user_file /usr/local/nginx/passwd.db; //注意这边的文件必须与上面创建的数据库验证文件统一 |
} |
|
service nginx restart |
|
|
|
|
|
|
|
|
|
|
|
|
|
再次访问需要输入用户名和密码 |
|
下面搭建虚拟主机 |
1.基于域名的虚拟主机 |
vi /usr/local/nginx/conf/nginx.conf //在最后一行上面插入 |
server { |
listen 80; |
server_name www.aa.com; //虚拟主机名 |
charset utf-8; |
access_log logs/aa.access.log main; |
location / { |
root /var/www/aa; |
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; |
} |
} |
} |
mkdir /var/www/aa -p |
mkdir /var/www/bc |
vi /var/www/aa/index.html |
vi /var/www/bc/index.html //编辑主页测试 |
在两个虚拟主机的网站目录添加主页 |
下面搭一个DNS服务器进行域名解析,这边自己搭过,不做详细说明 |
下面进行测试 |
|
|
|
|
|
|
|
|
|
|
|
|
|
2.基于端口的虚拟主机 |
vi /usr/local/nginx/conf/nginx.conf |
listen 192.168.80.181:80; //修改虚拟主机的配置文件 |
listen 192.168.80.181:8080; |
http://192.168.80.181:80 //访问 |
|
|
|
|
|
|
http://192.168.80.181:8080/ |
|
|
|
|
|
|
|
安装PHP |
yum -y install \ |
libjpeg \ |
libjpeg-devel \ |
libpng libpng-devel \ |
freetype freetype-devel \ |
libxml2 \ |
libxml2-devel \ |
zlib zlib-devel \ |
curl curl-devel \ |
openssl openssl-devel //安装需要支持的插件 |
|
|
tar xjvf php-7.1.10.tar.bz2 |
cd 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功能 |
|
cp php.ini-development /usr/local/php/lib/php.ini //复制php配置文件模板作为php配置文件 |
vi /usr/local/php/lib/php.ini //编辑php配置文件 |
mysqli.default_socket = /usr/local/mysql/mysql.sock //编辑mysql的sock文件位置 |
date.timezone = Asia/Shanghai //选择时区 |
|
/usr/local/php/bin/php -m //验证安装的模块 |
|
-----------配置及优化FPM模块-------- |
cd /usr/local/php/etc/ |
cp php-fpm.conf.default php-fpm.conf //复制php的fpm模块的配置文件模板为配置文件。php只识别php-fpm.conf为配置文件 |
cd /usr/local/php/etc/php-fpm.d/ |
cp www.conf.default www.conf //复制fpm的www配置文件模板为配置文件,fpm只识别www.conf的配置文件 |
|
cd /usr/local/php/etc/ |
vi php-fpm.conf //配置fpm模块 |
pid = run/php-fpm.pid //将分号去掉 |
;user = nginx |
;group = nginx //添加user和group |
|
/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini //启动fpm模块 |
netstat -anpt | grep 9000 |
ln -s /usr/local/php/bin/* /usr/local/bin/ //将php的可执行程序放到系统可识别环境中便于执行 |
ps aux | grep -c "php-fpm" //统计进程数 |
|
-----更新启动脚本,让fpm模块也可进行启动管理 |
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功能-------- |
vi /usr/local/nginx/conf/nginx.conf |
|
location ~ \.php$ { |
root /var/www/aa; |
fastcgi_pass 127.0.0.1:9000; |
fastcgi_index index.php; |
fastcgi_param SCRIPT_FILENAME /var/www/aa$fastcgi_script_name; //注意目录名称,为网站根目录必须为绝对路径 |
include fastcgi_params; |
} |
|
vi /var/www/aa/index.php |
<?php |
phpinfo(); |
?> |
service nginx restart 注意,为了防止干扰,需要把虚拟主机配置删除 |
在网页测试“http://192.168.80.193/index.php” |
|
--------下面测试数据库工作是否正常----- |
mysql -u root -p |
|
CREATE DATABASE bbs; //创建数据库为bbs |
GRANT all ON bbs.* TO 'bbsadm'@'%' IDENTIFIED BY 'admin123'; //将数据库所有权限给bbsadm,密码为admin123 |
GRANT all ON bbs.* TO 'bbsadm'@'localhost' IDENTIFIED BY 'admin123'; //允许 |
flush privileges; //刷新权限 |
|
vi /var/www/aa/index.php |
|
<?php |
$link=mysqli_connect('192.168.80.181','bbsadm','admin123'); |
if($link) echo "<h1>Success!!</h1>"; |
else echo "Fail!!"; |
?> |
|
在网页测试“http://192.168.80.193/index.php” |
|
|
|
|
|
--以下安装论坛-- |
unzip Discuz_X3.4_SC_UTF8_0101.zip //解压缩 |
cd dir_SC_UTF8/ //进入解压目录 |
cp -r upload/ /var/www/aa/bbs |
cd /var/www/aa/bbs |
chown -R root:nginx ./config/ |
chown -R root:nginx ./data/ |
chown -R root:nginx ./uc_client/ |
chown -R root:nginx ./uc_server/ |
|
chmod -R 777 ./config/ |
chmod -R 777 ./data/ |
chmod -R 777 ./uc_client/ |
chmod -R 777 ./uc_server/ |
|
访问http://IP/bbs/install/index.php //安装论坛 |
|
|
|
|
|
|
|
|
|
|
按照提示安装即可 |
rm -rf install/ 删除安装页面才能正常访问 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
访问地址为http://IP/bbs/index.php |
http://IP/bbs/admin.php //管理后台 |
这样访问很不友好,我们可以做一个首页跳转 |
vi /usr/local/nginx/html/index.html |
|
|
|
|
|
service nginx restart |
再去访问www.aa.com直接能跳转了 |
|
|
|
|
|
|
|
|
|
|