centos7.5 下LNMP搭建
Posted wendada
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7.5 下LNMP搭建相关的知识,希望对你有一定的参考价值。
一、概述
系统版本:centos7.5
软件版本:nginx 1.14.0 ;mysql 8.0.12 ;php :php70ww
安装方式:yum安装
所需yum源:
1.阿里云yum源:centos7 和epel7(系统yum源)
[[email protected] yum.repos.d]# cat CentOS-Base.repo
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
[[email protected] yum.repos.d]# cat epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
[[email protected] yum.repos.d]# cat epel-testing.repo
[epel-testing]
name=Extra Packages for Enterprise Linux 7 - Testing - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/testing/7/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=testing-epel7&arch=$basearch
failovermethod=priority
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
2.nginx yum源
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
3.php yum源
rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
[[email protected] yum.repos.d]# cat webtatic.repo
[webtatic]
name=Webtatic Repository EL7 - $basearch
#baseurl=https://repo.webtatic.com/yum/el7/$basearch/
mirrorlist=https://mirror.webtatic.com/yum/el7/$basearch/mirrorlist
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-webtatic-el7
4.MySQL8 yum源
官网地址:https://dev.mysql.com/downloads/repo/yum/ && rpm -ivh mysql80-community-release-el7-1.noarch.rpm
[[email protected] yum.repos.d]# cat mysql-community.repo
[mysql-connectors-community-source]
name=MySQL Connectors Community - Source
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-tools-preview]
name=MySQL Tools Preview
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/$basearch/
enabled=1
gpgcheck=0
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[[email protected] yum.repos.d]# cat mysql-community-source.repo
[mysql-connectors-community-source]
name=MySQL Connectors Community - Source
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/SRPMS
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-tools-community-source]
name=MySQL Tools Community - Source
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/SRPMS
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql56-community-source]
name=MySQL 5.6 Community Server - Source
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql57-community-source]
name=MySQL 5.7 Community Server - Source
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/SRPMS
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community-source]
name=MySQL 8.0 Community Server - Source
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/SRPMS
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-tools-preview-source]
name=MySQL Tools Preview - Source
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/SRPMS
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
二.安装并配置
1.nginx 安装和配置文件
安装: yum install -y nginx
创建nginx用户所属组
groupadd www
usermod nginx -g www
nginx主配置文件:nginx.conf
[[email protected] yum.repos.d]# cat /etc/nginx/nginx.conf
user nginx www;
pid /var/run/nginx.pid;
google_perftools_profiles /tmp/tcmalloc; (需重新编译替换,yum安装的不支持此模块)
worker_processes 2;
worker_cpu_affinity 00000001 00000010;
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
multi_accept on;
use epoll;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
error_log /var/log/nginx/error.log crit;
keepalive_timeout 6;
client_header_timeout 6;
client_body_timeout 6;
reset_timedout_connection on;
send_timeout 6;
include /etc/nginx/mime.types;
charset UTF-8;
gzip on;
gzip_disable "msie6";
gzip_min_length 2k;
gzip_buffers 4 128k;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css text/htm application/xml application/json;
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
open_file_cache_errors on;
#limit_conn_zone $binary_remote_addr zone=perip:10m;
#limit_req_zone $binary_remote_addr zone=allips:10m rate=3r/s;
log_format access ‘$remote_addr|$remote_user|$time_local|"$status"|$request|$body_bytes_sent|$request_time|$upstream_response_time|$http_host|$server_port|$upstream_addr|$http_referer|$msec|[[$http_user_agent]]|$http_x_forwarded_for‘;
log_format main ‘$remote_addr|$remote_user|$time_local|"$status"|$request|$body_bytes_sent|$request_time|$upstream_response_time|$http_host|$server_port|$upstream_addr|$http_referer|$msec|[[$http_user_agent]]|$uri$is_args$args|$document_root|$fastcgi_script_name|$http_x_forwarded_for‘;
upstream wm_socket{
server unix:/dev/shm/php_fpm_wm1.sock;
server unix:/dev/shm/php_fpm_wm2.sock;
server unix:/dev/shm/php_fpm_wm3.sock;
server unix:/dev/shm/php_fpm_wm4.sock;
}
#include /etc/nginx/conf.d/*.conf;
include /etc/nginx/vhost/*.conf;
include /etc/nginx/vhost_test/*.conf;
}
虚拟主机配置文件:
[[email protected] yum.repos.d]# cat /etc/nginx/vhost/wmend.conf
server {
listen 80;
server_name www.xxxxx.com;
charset utf-8;
rewrite ^(.*)$ https://$server_name$1 permanent;
}
server {
listen 443;
server_name www.xxxxx.com;
charset utf-8;
index index.php;
root /usr/local/code/www/public;
#ssl证书需购买
ssl on;
ssl_certificate cert/cert.pem;
ssl_certificate_key cert/cert.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
include fastcgi.conf;
#error_page 403 404 500 503 /letv/maintain/404.html;
location / {
if (!-e $request_filename){
rewrite ^/([a-zA-Z0-9]*)/(.*)$ /index.php/$1/$2 last;
rewrite ^/([a-zA-Z0-9]*) /index.php/$1 last;
}
}
location ~ .*.php {
fastcgi_pass unix:/dev/shm/php_fpm_wm2.sock;
include fastcgi_params;
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
fastcgi_intercept_errors on;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_param HTTPS off;
}
}
进入root目录,配置开发人员提供的代码
cd /usr/local/code/www/
svn -co $svn_URL ./
chown -R nginx:www ./*
nginx编译参数:
--prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-google_perftools_module --with-debug --with-cc-opt=‘-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC‘ --with-ld-opt=‘-Wl,-z,relro -Wl,-z,now -pie‘ --add-module=/home/soft/ngx_cache_purge-2.3 --add-module=/home/soft/echo-nginx-module-0.61
检查并启动nginx
[[email protected] ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[[email protected] ~]# systemctl start nginx
2.php 安装
yum install -y php70w-pear php70w php70w-mysql php70w-fpm php70w-cli php70w-xml php70w-pecl-memcached php70w-mbstring php70w-common php70w-process php70w-mcrypt php70w-gd php70w-pdo php70w-pecl-igbinary php70w-devel php70w-opcache php70w-bcmath
[[email protected] yum.repos.d]# rpm -qa|grep php
php70w-pear-1.10.4-1.w7.noarch
php70w-7.0.31-1.w7.x86_64
php70w-mysql-7.0.31-1.w7.x86_64
php70w-fpm-7.0.31-1.w7.x86_64
php70w-cli-7.0.31-1.w7.x86_64
php70w-xml-7.0.31-1.w7.x86_64
php70w-pecl-memcached-3.0.4-1.w7.x86_64
php70w-mbstring-7.0.31-1.w7.x86_64
php70w-common-7.0.31-1.w7.x86_64
php70w-process-7.0.31-1.w7.x86_64
php70w-mcrypt-7.0.31-1.w7.x86_64
php70w-gd-7.0.31-1.w7.x86_64
php70w-pdo-7.0.31-1.w7.x86_64
php70w-pecl-igbinary-2.0.5-1.w7.x86_64
php70w-devel-7.0.31-1.w7.x86_64
php70w-opcache-7.0.31-1.w7.x86_64
php70w-bcmath-7.0.31-1.w7.x86_64
php-fpm 配置文件:
[[email protected] yum.repos.d]# cd /etc/php-fpm.d/
[[email protected] php-fpm.d]# ls
wm1.conf wm2.conf wm3.conf wm4.conf www.conf_bak20180918
[[email protected] php-fpm.d]# cat wm1.conf
[wm1]
listen=/dev/shm/php_fpm_wm1.sock
listen.allowed_clients = any
listen.owner = nginx
listen.group = www
user = nginx
group = www
pm = dynamic
pm.max_children = 20
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.max_requests = 500
request_slowlog_timeout = 6
slowlog = /usr/local/log/wm-slow.log
php_admin_value[error_log] = /usr/local/log/wm-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /dev/shm/session_wm
php_value[soap.wsdl_cache_dir] = /dev/shm/wsdlcache_wm
启动:
systemctl start php-fpm
3.MySQL安装配置
yum install -y php70w-mysql mysql-community-common mysql-community-server mysql-community-client mysql-community-libs mysql-community-libs-compat
[[email protected] php-fpm.d]# rpm -qa|grep mysql
php70w-mysql-7.0.31-1.w7.x86_64
mysql-community-common-8.0.12-1.el7.x86_64
mysql-community-server-8.0.12-1.el7.x86_64
mysql-community-client-8.0.12-1.el7.x86_64
mysql-community-libs-8.0.12-1.el7.x86_64
mysql-community-libs-compat-8.0.12-1.el7.x86_64
mysql配置文件:/etc/my.cnf
[[email protected] php-fpm.d]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
[mysqld]
port=3306
character_set_server=utf8
init_connect=‘SET NAMES utf8‘
innodb_buffer_pool_size = 5120M
innodb_dedicated_server=ON
# Remove the leading "# " to disable binary logging
# Binary logging captures changes between backups and is enabled by
# default. It‘s default setting is log_bin=binlog
# disable_log_bin
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
join_buffer_size = 2048M
sort_buffer_size = 64M
read_rnd_buffer_size = 64M
# default-authentication-plugin=mysql_native_password
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/lib/mysql/mysql.sock
default-character-set=utf8
[mysql]
socket=/var/lib/mysql/mysql.sock
default-character-set=utf8
启动
systemctl start mysqld
查询初始密码:
[[email protected]4567ah ~]# cat /var/log/mysqld.log|grep ‘temporary password‘
2018-10-29T02:02:19.937204Z 5 [Note] [MY-010454] [Server] A temporary password is generated for [email protected]: 8,EO4mmdi=AK
登陆:mysql -h127.0.0.1 -uroot -p 输入密码
更改root用户密码,必须改密码后才可建库
mysql> alter user "root"@"localhost" identified with mysql_native_password by "123456" ;
Query OK, 0 rows affected (0.08 sec)
创建新用户
mysql> create user "wmts"@"localhost" identified with mysql_native_password by "12345678";
create user "wmts"@"localhost" identified by "123456";
授权:
Grant all privileges on dbname.* to “username”@”hostname”;
三、浏览器访问测试
以上是关于centos7.5 下LNMP搭建的主要内容,如果未能解决你的问题,请参考以下文章