本篇主要讲解nginx、tomcat、mysql的详细配置,相信很多人也跟本站长一样,在centos内配置环境又没有基础的情况下简直是抓虾。在经历各种摸黑滚打之后,站长已经在配置环境方面有一定的专长,尤其擅长nginx、php、tomcat、mysql等方面的配置和维护。不卖瓜了开始讲解,为了给同样刚开始接触建站的新人一点帮助。
在讲解配置前的工作:
1、 确定selinux已关闭
2、 开启80端口、443端口、8080端口
3、 配置ssh无密码证书登录 //这项可以省略,本地无所谓
一、tomcat配置
安装jre
1 |
yum install jre –y |
查看jre版本
1 |
java -version |
进入tomcat配置文件目录
1 |
cd /usr/local/tomcat/conf/ |
备份server.xml文件
1 |
cp server.xml server.xml.bak |
编辑server.xml文件
1 |
vi server.xml |
修改/opt/website/的路径,这个路径是你放置站点的路径,请仔细设置
1 2 3 4 5 6 |
<Host name=”localhost” appBase=”webapps” unpackWARs=”true” autoDeploy=”true”> <Context path=”” docBase=”/opt/website/” debug=”0″ reloadable=”true”></Context> <Valve className=”org.apache.catalina.valves.AccessLogValve” directory=”logs” prefix=”localhost_access_log” suffix=”.txt” pattern=”%h %l %u %t "%r" %s %b” /> </Host> |
按ESC键,wq保存退出,在不开启https的情况下,tomcat配置完成。关于配置https项以后会讲。
二、nginx配置
nginx配置给出的写入案例,均是经过优化和官方建议的,可以根据自己的需求自行修订
进入nginx目录
1 |
cd /etc/nginx/ |
备份nginx.conf文件
1 |
cp nginx.conf nginx.conf.bak |
配置nginx.conf文件
1 |
vi nginx.conf |
写入以下内容到nginx.conf 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf;
events { worker_connections 1024; }
http { 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 /var/log/nginx/access.log main;
client_header_buffer_size 32k; large_client_header_buffers 4 32k;
sendfile on; tcp_nopush on; tcp_nodelay on; server_tokens off; keepalive_timeout 60; types_hash_max_size 2048; client_max_body_size 8M;
gzip on; gzip_vary on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_disable “MSIE [1-6]\.”; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
include /etc/nginx/mime.types; default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information.
include /etc/nginx/conf.d/*.conf;
} |
进入conf目录
1 |
cd conf |
创建网站需要的conf文件
1 |
vi site.conf |
写入以下内容到site.conf文件,此写入方式是未开启https的写法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
upstream cms { server localhost:8080 weight=1; }
server { listen 80; server_name www.rednn.com;
location / { alias /opt/website/web/site_1/; index index.html; add_header Access-Control-Allow-Origin *; } }
server { listen 80; server_name cms.rednn.com search.rednn.com;
location / { proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_connect_timeout 5; proxy_send_timeout 30; proxy_read_timeout 10; proxy_pass http://cms; }
location /webfile/ { alias /opt/website/web/site_1/webfile/; index index.html; }
location /include/ { alias /opt/website/web/site_1/include/; } } |
重启nginx
1 |
systemctl restart nginx.service |
三、mariadb/mysql配置
配置数据库编码,关于数据库编码怎样选择的问题,请自行查档。本站采用utf8mb4_unicode_ci编码,也推荐使用这个。
编辑或创建vi /etc/my.cnf文档,修改utf8mb4_unicode_ci默认编码。
1 |
vi /etc/my.cnf |
拷贝以下内容到my.cnf
1 2 3 4 5 6 7 8 9 10 |
[client] default-character-set=utf8mb4
[mysql] default-character-set=utf8mb4
[mysqld] collation-server = utf8mb4_unicode_ci init-connect=’SET NAMES utf8mb4′ character-set-server = utf8mb4 |
重启mariadb
1 |
Systemctl restart mariadb |
到此,环境已经配置完成