用yum源配合源码包安装openresty、mariadb、php7服务
在开始部署之前,有必要先简单介绍一下!
OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。
MariaDB服务器是一个在世界上最流行的数据库服务器。这是由原始开发者的MySQL,并保证保持开放源码。MariaDB是因为它速度快,可扩展性和鲁棒性,具有丰富的生态系统,存储引擎,插件和许多其他工具,使各种各样的用例非常灵活。
PHP,我想就不必再絮叨多少了吧。
那我们就开始新的学习之旅吧,欢迎大神进群相互探讨交流,交流群:《运维部落 348384728》!!!
1、 前期准备(最小化安装的系统)
1.更新系统
yum makecache && yum -y update
安装常用基础软件
yum -y install bash-completion vim net-tools bind-utils wget screenyum -y install yum-utils
关闭SELINUX
[[email protected] ~]# vi /etc/sysconfig/selinux
SELINUX=disabled
最后别忘了重启
2、 安装mariadb
1.添加mariadb yum资源库
vi /etc/yum.repos.d/MariaDB.repo
[mariadb]
name= MariaDB
baseurl= http://yum.mariadb.org/10.1/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
2.可以使用yum search MariaDB 查看安装包,官方给出的只要安装server和client就行,其他的根据
自己的需要来
yum install MariaDB-server MariaDB-client
网上也有帖子说是要初始化一下,我也试了,无非就是设置数据库的一些配置,像是否删除测试数据库,是否禁止远程登录数据库,是否删除用命用户什么的,后面可以跟配置项—defaults-file、--datadir、--user等,感觉没多大卵用(废话有点多,回到原题)
[[email protected] my.cnf.d]# mysql_secure_installation
回车——y(设置密码)——y(允许匿名账户登录)——n(禁止root远程登录)——y(删除test测试数据库)——y(重新加载特权表)
mysql -u root -p
create database datadb;
create user [email protected]'%' identified by '密码';
grant all on datadb.* to 'dataguanli'@'%';
flush privileges;
show databases;
exit;
3.加入系统服务
[[email protected] ~]# systemctl enable mariadb
[[email protected] ~]# systemctl start mariadb.service
[[email protected] ~]# systemctl stop mariadb.service
提醒:通过yum装很多东西都是默认的,不像源码编译或者自己DOWN安装包可以各种指定,列
一些比较重要的路径出来
yum 安装会自动创建mysql 用户和用户组
Yum安装会自动创建 /etc/my.cnf 以及/etc/my.cnf.d 文件架(并存放 server.conf client.conf tokudb.conf 等文件)
默认rpm路径
basedir =/usr/bin
datadir=/var/lib/mysql
.h=/usr/include/mysql
plung=/usr/lib64/mysql
share=/usr/share/mysql(confcharset etc)
4、 安装PHP7
1.删除之前的PHP版本 但我是最小化安装的系统,就不需要这一步了。
[[email protected] ~]#yum remove php* php-common
2.rpm 安装Php7 相应的 yum源:
[[email protected] ~]# wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[[email protected] ~]# wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[[email protected] ~]# rpm -Uvh remi-release-7.rpm epel-release-latest-7.noarch.rpm
[[email protected] ~]# yum-config-manager --enable remi-php70
4.直接yum安装php7:
[[email protected] ~]# yum install php70-php php70-php-cli php70-php-fpm php70-php-cgi php70-php-mysql php70-php-xmlrpc php70-php-curl php70-php-gd php70-php-apcu php70-php-imap php70-php-mcrypt php70-php-memcached php70-php-mbstring
注意:
1、php.ini是php运行核心配置文件:
2、php-fpm.conf是 php-fpm 进程服务的配置文件:
3、配置www.conf(在php-fpm.d目录下)
www.conf这是 php-fpm 进程服务的扩展配置文件:
php服务的一些文件路径在/
5、 安装openresty (编译安装)
1.依赖关系
openresty依赖于perl5.6.1+, libreadline, libpcre, libssl,首先安装依赖关系。
[[email protected]~]# yum -y install readline-devel pcre-devel openssl-devel gcc
2下载源码
https://openresty.org/download/openresty-1.11.2.3.tar.gz(17年4月份)
https://openresty.org/download/openresty-1.11.2.2.tar.gz (16年11月)
使用wget下载(可以登录http://openresty.org查看最新版本)
[[email protected]~]# wget https://openresty.org/download/openresty-1.11.2.3.tar.gz
3.解压下载好的压缩包
[[email protected] ~]# tar zxf openresty-1.11.2.3.tar.gz
4.配置./configure
[[email protected] openresty-1.11.2.3]# cd openresty-1.11.2.3/
[[email protected] openresty-1.11.2.3]# ./configure --prefix=/opt/openresty --with-luajit
默认, –prefix=/usr/local/openresty程序会被安装到/usr/local/openresty目录。
我们可以指定各种选项,比如
./configure --prefix=/opt/openresty \
--with-luajit \
--without-http_redis2_module \
--with-http_iconv_module \
--with-http_postgres_module
具体用法参考./configure–help 或者官方文档
5. 编译、安装
[[email protected]]# gmake && gmake install
6、Nginx启动脚本
yum -y install nano ##想必有人知道这跟vim,vi一样,是一种编辑工具
nano /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/opt/openresty/nginx/logs/nginx.pid
ExecStartPre=/opt/openresty/nginx/sbin/nginx -t
ExecStart=/opt/openresty/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
7.默认项目路径
/opt/openresty/nginx/html
8、启动Nginx并加入系统服务
systemctl enable nginx.service
systemctl start nginx.service
9.给nginx做语法检测
[[email protected] openresty-1.11.2.3]# /opt/openresty/nginx/sbin/nginx -t
nginx: the configuration file/opt/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file/opt/openresty/nginx/conf/nginx.conf test is successful
启动nginx服务
[[email protected] openresty-1.11.2.3]# systemctl start nginx.service
6、用include指令实现nginx多虚拟主机配置 (nginx文件设置)
1.创建nginx账户和组
[[email protected]~]# groupadd www-data
[[email protected]~]# useradd -g www-data www-data
2.配置nginx.conf文件: 内容如下(程序代码)
user www-data;
worker_processes 1;
error_log/opt/openresty/nginx/logs/error.log crit;
pid /opt/openresty/nginx/logs/nginx.pid;
events {
#使用的网络I/)模型,Linux系统推荐采用epoll模型,FreeBSD系统推荐采用kqueue模型
use epoll;
worker_connections 1024;
}
http {
include /opt/openresty/nginx/conf/mime.types;
default_type application/octet-stream;
charset utf-8;
server_token off;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
keepalive_timeout 60;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
client_body_temp_path/opt/openresty/nginx/client_body_temp;
proxy_temp_path/opt/openresty/nginx/proxy_temp;
fastcgi_temp_path/opt/openresty/nginx/fastcgi_temp;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascripttext/css application/xml;
gzip_vary on;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#virtual host
include /host/nginx/conf/vhost/www_test_com.conf;
include /host/nginx/conf/vhost/www_test1_com.conf;
include /host/nginx/conf/vhost/www_test2_com.conf;
#也可以使用 include /host/nginx/conf/vhost/*.conf 来代替的,这里支持通配符.
}
3.在Nginx的虚拟主机配置(www_test_com.conf, www_test1_com.conf, www_test2_com.conf)文件里,设置access.log,error.log
[[email protected]~]# mkdir -p /host/nginx/logs/test{"",1,2}
4.在conf目录下创建虚拟主机配置文件目录vhost,在vhost目录下分别根据域名建立相应的
www_test_com.conf,www_test1_com.conf,www_test2_com.conf3个文件.
##我把虚拟主机配置文件放在了这个目录下,只不过需要先手动创建目录和.conf文件
[[email protected] test]#ls /host/nginx/conf/vhost/
www_test1_com.conf www_test2_com.conf www_test_com.conf
www_test_com.conf配置代码:
vi /host/nginx/conf/vhost/www_test_com.conf
server {
listen 80; ###换成自个的IP地址(我是基于域名的虚拟主机,所以是相同IP不同端口。
client_max_body_size 100M;
server_name www.test.com; ###换成自个要用的域名
charset utf-8; ###字符集:utf-8
root /var/www/test/; ###站点路径——存放网页的目录
index index.php index.html index.htm; ###追加index.php让nginx服务器默认支持index.php为首页:
autoindex on;
access_log /host/nginx/logs/test/test.access.log;
error_log /host/nginx/logs/test/test.error.log;
if (-d $request_filename) {
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
error_page 404 /404.html;
location = /40x.html {
root /var/www/test/; #你的站点路径
charset on;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/test/; ###自个的站点路径——网页存放路径
charset on;
}
#将客户端请求装交给fastcgi,老大要求用fpm模块(修改)
location ~ \.php$ {
root /var/www/test/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
注意:配置.php请求被传送到后端的php-fpm模块,默认情况下php配置块是被注释的,此时去
掉注释并修改,这里面很多都是默认的,root是配置php程序放置的根目录,主要修改
的就是fastcgi_param中的/scripts为$document_root
例如:fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
location~ .*\.(php|php5|php4|shtml|xhtml|phtml)?$ {
fastcgi_pass 127.0.0.1:9000;
include /opt/openresty/nginx/conf/fastcgi_params;
}
#网站的图片较多,更改较少,将它们在浏览器本地缓存15天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 15d;
}
#网站会加载很多JS、CSS,将它们在浏览器本地缓存1天
location ~ .*\.(js|css)?$ {
expires 1d;
}
location /(WEB-INF)/ {
deny all;
}
}
注意:www_test1_com.conf配置代码和www_test2_com.conf配置代码跟www_test_com.conf(除了
和网页存放路径不一样外)一样。
5.创建/var/www/test,/var/www/test1,/var/www/test2这三个目录,并在这三个目录下分别创建 index.php,还要给它们属主和属组权限。
[[email protected] ~]# ls /var/www/test
test/ test1/ test2/
[[email protected] ~]# vi/var/www/test/index.php
<?php
phpinfo();
?>
给网页文档目录/var/www/test{“”,1,2}添加属主和属组权限。
[[email protected]]# chown -R www-data:www-data /var/www/test{"",1,2}
其他两个测试文件(注意目录)一样。
7、php文件配置:
1.php-fpm.conf配置文件
[[email protected]~]# vi /etc/php-fpm.conf
在[global]配置项下,把pid = /var/run/php-fpm/php-fpm.pid前的分号注释去掉,还有就是include=/etc/php-fpm.d/*.conf这一项,设置配置文件路径
2.php-fpm.d/www.conf配置文件
[[email protected]~]# vi /etc/php-fpm.d/www.conf
在[www]配置项下有user = ,group= 两配置项,都改了
user= www-data
group= www-data
8、检查nginx配置是否有错
nginx -t
启动Nginx服务
[[email protected]~]# systemctl start nginx.service ##nginx启动命令
启动PHP服务
[[email protected]~]# systemctl start php70-php-fpm.service ##PHP启动命令
启动mariadb服务
[[email protected]~]# systemctl enable mariadb
systemctl start mariadb.service
9、配置防火墙规则:
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --reload
10、在windows系统中C:\WINDOWS\system32\drivers\etc下的hosts文件中做域名映射
11、可以查看访问了
欢迎大家来给我提意见,大家相互学习!