下文内容主要给大家带来相关linux7.4+nginx1.13.9+mysql5.7.20+php7.1.10的讲义,这里所讲到的知识,与书籍略有不同,都是亿速云专业技术人员在与用户接触过程中,总结出来的,具有一定的经验分享价值,希望给广大读者带来帮助。
一、 环境
安装包下载地址: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
[root@lss02 ~]# useradd -M -s /sbin/nologin nginx //创建程序用户nginx,不能登录,没有家目录
[root@lss02 ~]# tar xzvf nginx-1.13.9.tar.gz
[root@lss02 ~]# cd nginx-1.13.9
[root@lss02 nginx-1.13.9]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module //日志分析模块
[root@lss02 nginx-1.13.9]# make && make install //编译及安装
[root@lss02 nginx-1.13.9]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //为nginx可执行程序做软连接,让系统可识别
nginx -t //配置文件语法检查
nginx //启动服务
killall -1 nginx //安全重启
killall -3 nginx //停止服务
----------------------------------------制作管理角本---------------------------
[root@lss02 nginx-1.13.9]# vi /etc/init.d/nginx
#!/bin/bash
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
[root@lss02 nginx-1.13.9]# chmod +x /etc/init.d/nginx //给管理脚本添加执行权限
[root@lss02 nginx-1.13.9]# chkconfig --add nginx //将nginx添加到chkconfig系统管理工具中
------------------------下面对nginx配置文件进行初始配置-------------------
[root@lss02 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 //定义日志格式 把前面的#号去掉
[root@lss02 nginx-1.13.9]# service nginx restart
配置完之后重启,现在nginx云服务器已经可以支持访问了
------------------------------------配置nginx自带的日志统计模块------------------------------------
[root@lss02 nginx-1.13.9]# vi /usr/local/nginx/conf/nginx.conf
location ~ /status {
stub_status on;
access_log off;
}
[root@lss02 nginx-1.13.9]# service nginx reload
[root@lss02 nginx-1.13.9]# cat /usr/local/nginx/logs/access.log
浏览器:http://IP/status //当前的活动连接数,已处理的连接数,成功的TCP握手次数,已处理的请求数。
------------------------以下配置nginx的验证功能----------------------------
[root@lss02 nginx-1.13.9]# yum install httpd-tools -y //安装apache的工具,借助工具创建访问用户
[root@lss02 nginx-1.13.9]# htpasswd -c /usr/local/nginx/passwd.db jack //创建数据库验证文件,并把jack用户添加进去
New password:输密码
Re-type new password:确认密码
Adding password for user jack
[root@lss02 nginx-1.13.9]# chmod 400 /usr/local/nginx/passwd.db //提高数据安全性
[root@lss02 nginx-1.13.9]# chown nginx /usr/local/nginx/passwd.db //更改属主
[root@lss02 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; //注意这边的文件必须与上面创建的数据库验证文件统一
}
[root@lss02 nginx-1.13.9]# service nginx restart
再次访问需要输入用户名和密码
三、 以下配置虚拟主机功能
-----------基于域名----------IP和端口都相同但是域名不相同----------
[root@lss02 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"';
[root@lss02 nginx-1.13.9]# mkdir /var/www/aa -p
[root@lss02 nginx-1.13.9]# mkdir /var/www/bc
在两个虚拟主机的网站目录添加主页
[root@lss02 nginx-1.13.9]# vi /var/www/aa/index.html
<h3>www.aa.com</h3>
[root@lss02 nginx-1.13.9]# vi /var/www/bc/index.html
<h3>www.bc.com</h3>
修改微软的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
[root@lss02 nginx-1.13.9]# nginx –t
[root@lss02 nginx-1.13.9]# service nginx restart
[root@lss02 nginx-1.13.9]# killall -1 nginx
四、 安装MySQL
[root@lss02 ~]# yum -y install ncurses ncurses-devel bison cmake
[root@lss02 ~]# useradd -s /sbin/nologin mysql //手动建一个账号
[root@lss02 ~]# tar xf mysql-boost-5.7.20.tar.gz -C /opt/ //解压的目录要足够大,不然会报错
[root@lss02 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
[root@lss02 mysql-5.7.20]# make && make install
[root@lss02 mysql-5.7.20]# chown -R mysql:mysql /usr/local/mysql/
[root@lss02 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
[root@lss02 mysql-5.7.20]# chown mysql:mysql /etc/my.cnf
[root@lss02 mysql-5.7.20]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile //把这两个路径添加到环境变量中,并放到profile文件中使之开机自运行,否则不生效
[root@lss02 mysql-5.7.20]# echo 'export PATH' >> /etc/profile //也可以软链接
[root@lss02 mysql-5.7.20]# source /etc/profile //立即生效
[root@lss02 mysql-5.7.20]# cd /usr/local/mysql/
bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data //mysql初始化
[root@lss02 mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[root@lss02 mysql]# systemctl daemon-reload
[root@lss02 mysql]# systemctl start mysqld
[root@lss02 mysql]# netstat -anpt | grep 3306
[root@lss02 mysql]# systemctl enable mysqld
[root@lss02 mysql]# mysqladmin -u root -p password "123"
Enter password:(初始密码是空,直接回车就好)
[root@lss02 mysql]# mysql -u root –p
mysql>
mysql>
mysql> quit
Bye
五、 安装PHP
[root@lss02 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 //安装需要支持的插件
[root@lss02 ~]# yum -y install bzip2
[root@lss02 ~]# tar xjvf php-7.1.10.tar.bz2 -C /opt/
[root@lss02 ~]# cd /opt/php-7.1.10
[root@lss02 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功能 (注意这个要手打,直接复制容易失败)
[root@lss02 php-7.1.10]# make && make install
[root@lss02 php-7.1.10]# cp php.ini-development /usr/local/php/lib/php.ini //复制php配置文件模板作为php配置文件
[root@lss02 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 //选择时区
[root@lss02 php-7.1.10]# /usr/local/php/bin/php –m //验证安装的模块
-----------配置及优化FPM模块--------
[root@lss02 php-7.1.10]# cd /usr/local/php/etc/
[root@lss02 etc]# cp php-fpm.conf.default php-fpm.conf //复制php的fpm模块的配置文件模板为配置文件。php只识别php-fpm.conf为配置文件
[root@lss02 etc]# cd /usr/local/php/etc/php-fpm.d/
[root@lss02 php-fpm.d]# cp www.conf.default www.conf //复制fpm的www配置文件模板为配置文件,fpm只识别www.conf的配置文件
[root@lss02 php-fpm.d]# cd /usr/local/php/etc/
[root@lss02 etc]# vi php-fpm.conf //配置fpm模块
pid = run/php-fpm.pid //将分号去掉
;user = nginx
;group = nginx //添加user和group
[root@lss02 etc]# /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini //启动fpm模块
[root@lss02 etc]# netstat -anpt | grep 9000
[root@lss02 etc]# ln -s /usr/local/php/bin/* /usr/local/bin/ //将php的可执行程序放到系统可识别环境中便于执行
[root@lss02 etc]# ps aux | grep -c "php-fpm" //统计进程数
-----更新启动脚本,让fpm模块也可进行启动管理----------
[root@lss02 etc]# vi /etc/init.d/nginx
#!/bin/bash
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功能--------------
[root@lss02 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;
}
[root@lss02 etc]# vi /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
[root@lss02 etc]# nginx -t
[root@lss02 etc]# service nginx restart
注意,为了防止干扰,需要把虚拟主机配置删除
在网页测试“http://192.168.80.193/index.php
-----------------------------------下面测试数据库工作是否正常---------------------
[root@lss02 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
[root@lss02 etc]# vi /usr/local/nginx/html/index.php
<?php
$link=mysqli_connect('192.168.80.102','bbsadm','admin123');
if($link) echo "<h3>Success!!</h3>";
else echo "Fail!!";
?>
在网页测试“http://IP/index.php”
---------------------以下安装论坛---------------------------
[root@lss02 etc]# yum install -y unzip
[root@lss02 etc]# unzip Discuz_X3.4_SC_UTF8_0101.zip -d /opt //解压缩
[root@lss02 ~]# cd /opt/dir_SC_UTF8/ //进入解压目录
[root@lss02 dir_SC_UTF8]# cp -r upload/ /usr/local/nginx/html/bbs
[root@lss02 dir_SC_UTF8]# cd /usr/local/nginx/html/bbs
[root@lss02 bbs]# chown -R root:nginx ./config/
[root@lss02 bbs]# chown -R root:nginx ./data/
[root@lss02 bbs]# chown -R root:nginx ./uc_client/
[root@lss02 bbs]# chown -R root:nginx ./uc_server/
[root@lss02 bbs]#
[root@lss02 bbs]# chmod -R 777 ./config/
[root@lss02 bbs]# chmod -R 777 ./data/
[root@lss02 bbs]# chmod -R 777 ./uc_client/
[root@lss02 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传到服务器
[root@lss02 html]# vi /usr/local/nginx/conf/nginx.conf
location ~ .php$ {
proxy_pass http://192.168.80.101;
} //把PHP动态请求转给192.168.80.101
[root@lss02 html]# rm index.php
[root@lss02 html]# service nginx restart
Apache服务器上面配置:
[root@lss01 ~]# cd /usr/local/httpd/htdocs/
[root@lss01 htdocs]# vi index.html
<html>
<body>
<img src="http://192.168.80.102/b.jpg">
</body>
</html>
[root@lss01 htdocs]# service httpd restart
浏览器输nginx服务器ip测试:
Nginx服务器修改:
[root@lss02 html]# vi /usr/local/nginx/conf/nginx.conf
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
root html;
expires 1d;
} //这些静态内容从本地读取
[root@lss02 html]# service nginx restart
Apache服务器上面配置:
[root@lss01 htdocs]# vi /usr/local/httpd/htdocs/index.php
<html>
<body>
<img src=" b.jpg">
</body>
</html>
[root@lss01 htdocs]# service httpd restart
抓包测试:
对于以上相关linux7.4+nginx1.13.9+mysql5.7.20+php7.1.10的讲义,如果大家还有更多需要了解的可以持续关注我们亿速云的行业推新,如需获取专业解答,可在官网联系售前售后的,希望该文章可给大家带来一定的知识更新。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。