LNMP環境配置,MySQL部分
LNMP代表的就是:Linux系統下Nginx+MySQL+PHP這種網站服務器架構,由Linux,Nginx,PHP,MySQL這四種軟體均為免費開源軟體,組合到一起,成為一個免費、高效、擴展性強的網站服務系統,
1.安裝MySQL
1.1下載安裝包
我的系統是CentOS 7 ,使用的是二進制編譯包,所以選擇的是64位的包,(x86_64),
查看自己Linux是多少位的
# uname -i x86_64
然后下載原始碼包,
# wget http://mirrors.sohu/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
1.2初始化
# tar -xzvf mysql-5.6.43-linux-glibc2.12-x86_64.tar.gz //解壓
# mv mysql-5.6.43-linux-glibc2.12-x86_64 /usr/local/mysql //挪動位置
# useradd -s /sbin/nologin mysql //建立MySQL用戶,
# cd /usr/local/mysql/ //切換目錄
# mkdir -p /data/mysql //創建datadir,資料庫檔案會放到這里
# chown -R mysql.mysql /data/mysql //更改權限,不更改后面會出錯
# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper //報錯缺少 perl-Module-Install包
# yum install -y perl-Module-Install //下載perl-Module-Install包
在重新執行./scripts/mysql_install_db --user=mysql-test/ --datadir=/data/mysql,
# echo $? //執行該命令看輸出是否為0.為0則成功
0
# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆寫"/etc/my.cnf"? y
1.3配置MySQL
首先復制組態檔
# cp support-files/my-default.cnf /etc/my.cnf //復制組態檔 cp:是否覆寫"/etc/my.cnf"? y
# vim /etc/my.cnf
找到下面內容,刪掉前面#符號
# basedir = /usr/local/mysql //mysql包所在路徑
# datadir = /data/mysql //定義的存放資料的地方
# port = 3306 //MySQL服務監聽的埠,默認為3306
# server_id =140 //定義該MySQL服務的ID號,
# socket =/tmp/mysql.sock //定義MySQL服務監聽的套接字地址
然后復制啟動腳本檔案并修改其屬性,
# cp support-files/mysql.server /etc/init.d/mysqld //復制腳本檔案
# chmod 755 /etc/init.d/mysqld //修改腳本檔案權限
# vim /etc/init.d/mysqld //修改腳本檔案
修改內容如下:
basedir=/usr/local/myqsl
datadir=/data/mysql
把啟動腳本加入系統服務項,設定開機啟動并啟動MySQL,
# chkconfig --add mysqld //把MySQL服務加入到系統服務串列中
# chkconfig mysqld on //使其開機就啟動
# service mysqld start //啟動服務
Starting MySQL.Logging to '/data/mysql/localhost.localdomain.err'.
SUCCESS!
如果啟動不了到/data/mysql/目錄下查看錯誤日志,
檢查MySQL是否啟動命令:
# ps aux |grep mysqld
# netstat -lnp | grep 3306 //如果沒有netstat命令 ,需下載,# yum install net-tools -y
2.安裝php
2.1.1下載php原始碼包,命令如下:
# cd /usr/local/src/ //切換到目錄下
# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz //獲取php5.6版本的原始碼包
2.1.2解壓原始碼包,創建賬號,命令如下:
# tar -xzvf rm -rf php-5.6.30.tar.gz //解壓原始碼包
# useradd -s /sbin/nologin php-fpm //創建用戶php-fpm
2.1.3配置編譯選項,命令如下:
# cd php-5.6.27 //切換至目錄
#./configure \ //配置編譯選項 //內容如下
--prefix=/usr/local/php-fpm \
--with-config-file-path=/usr/local/php-fpm/etc \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-mysql=/usr/local/mysql \
--with-mysql-sock=/tmp/mysql.sock \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-ftp \
--enable-mbstring \
--enable-exif \
--disable-ipv6 \
--with-pear \
--with-curl \
--with-openssl
編譯所需的包有:
# yum install -y gcc-c++
# yum install -y libxml2-devel
# yum install openssl-devel
# yum install -y curl-devel
# yum install -y libjpeg-devel
# yum install -y libpng-devel
# yum install freetype-devel -y
CentOS的yum源默認沒有libmcrypt-devel這個包,只能借助epel yum擴展源獲取,
# yum install -y epel-release
# yum install -y libmcrypt-devel
2.1.4編譯php,并安裝,命令如下:
# make //編譯
# make && install //安裝
每一步執行后都可以使用echo $? 驗證是否成功,
2.1.5修改組態檔,命令如下:
# cp php.ini-production /usr/local/php-fpm/etc/php.ini //復制檔案
# vim /usr/local/php-fpm/etc/php-fpm.conf //修改組態檔內容如下
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 50
rlimit_files = 1024
# /usr/local/php-fpm/sbin/php-fpm -t //保存組態檔后檢查配置是否正確“test is successful” 則說明配置沒問題
啟動php-fpm,命令如下:
# cp /usr/local/src/php-5.6.27/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm //復制檔案
# chmod 755 /etc/init.d/php-fpm //修改檔案權限
# service php-fpm start //開啟服務
Starting php-fpm done //開啟服務成功
# chkconfig php-fpm on //設定php-fpm開機啟動的命令
# ps aux | grep php-fpm //檢測php-fpm是否啟動的命令 大概可以看到二十多個行程
3.安裝Nginx
3.1.1下載和解壓Nginx,命令如下
# cd /usr/local/src/ //切換至目錄
# wget http://nginx.org/download/nginx-1.12.2.tar.gz //獲取Nginx原始碼包
# tar -xzvf nginx-1.12.2.tar.gz //將原始碼包解壓
3.1.2配置編譯選項,命令如下:
# cd nginx-1.12.2
# ./configure --prefix=/usr/local/nginx //配置編譯
3.1.3編譯和安裝nginx,命令如下:
# make //編譯
# make install //安裝
3.1.4撰寫Nginx啟動腳本并加入服務系統
# vim /etc/init.d/nginx //寫入如下內容
#!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start() { echo -n $"Starting $prog: " mkdir -p /dev/shm/nginx_temp daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo return $RETVAL } stop() { echo -n $"Stopping $prog: " killproc -p $NGINX_PID $NGINX_SBIN -TERM rm -rf /dev/shm/nginx_temp RETVAL=$? echo return $RETVAL } reload() { echo -n $"Reloading $prog: " killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo return $RETVAL } restart() { stop start } configtest() { $NGINX_SBIN -c $NGINX_CONF -t return 0 } case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo $"Usage: $0 {start|stop|reload|restart|configtest}" RETVAL=1 esac exit $RETVAL
保存該腳本后更改權限,命令如下:
# chmod 755 /etc/init.d/nginx //更改權限
# chkconfig --add nginx //加入系統服務串列中
# chkconfig nginx on //設定開機啟動
3.1.5更改Nginx組態檔,
首先將原來的組態檔清空,
# > /usr/local/nginx/conf/nginx.conf //清空原來的組態檔
# vim /usr/local/nginx/conf/nginx.conf //寫入如下內容
user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
client_body_temp_path /usr/local/nginx/client_body_temp;
proxy_temp_path /usr/local/nginx/proxy_temp;
fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
server
{
listen 80;
server_name localhost;
index index.html index.htm index.php;
root /usr/local/nginx/html;
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
}
}
}
保存組態檔后,需要檢驗一下是否有錯誤,命令如下:
# /usr/local/nginx/sbin/nginx -t //檢驗是否有錯誤
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful //即成功
3.1.6啟動Nginx,命令如下:
# service nginx start //啟動Nginx Starting nginx (via systemctl): [ 確定 ]
3.1.7測驗是否正確決議PHP,
首先創建測驗檔案,操作方法如下:
# vim /usr/local/nginx/html/test.php //創建測驗檔案,輸入以下內容
<?php echo "test php scripts."; ?>
執行如下命令,測驗檔案:
# curl localhost/test.php
test php scripts.[root@localhost nginx-1.12.2]# //即成功,說明PHP決議正常
3.2Nginx配置
3.2.1默認虛擬主機
修改主組態檔,
# vim /usr/local/nginx/conf/nginx.conf //修改主組態檔,在最后一個}前加上include vhost /*.conf;
include vhost/*.conf; }
意思是,/usr/local/nginx/conf/vhost/下面所有以.conf結尾的檔案都會加載,這樣就可以把所有的虛擬主機組態檔放到vhost目錄下了,
# mkdir /usr/local/nginx/conf/vhost
# cd /usr/local/nginx/conf/vhost/
# vim default.conf //創建默認虛擬住組態檔,寫入以下內容
server
{
listen 80 default_server;
server_name aaa.com;
index index.html index.htm index.php;
root /data/nginx/default;
}
# /usr/local/nginx/sbin/nginx -t //驗證組態檔是否正確
# /usr/local/nginx/sbin/nginx -s reload //重新加載
# mkdir -p /data/nginx/default/ //創建目錄
# echo "default_server" > /data/nginx/default/index.html //創建索引頁
# curl -x127.0.0.1:80 aaa.com //訪問默認虛擬主機
default_server
# curl -x127.0.0.1:80 123.com //訪問一個沒有定義過的域名,也會轉到aaa.com
default_server
3.2.2用戶認證
創建一個新的虛擬主機:
# cd /usr/local/nginx/conf/vhost/ //進入vhost目錄
# vim yuhuai.com.conf //創建新的虛擬主機組態檔,輸入以下內容
server
{
listen 80;
server_name yuhuai.com;
index index.html index.htm index.php;
root /data/nginx/yuhuai.com;
location /
{
auth_basic "Auth"; //打開認證
auth_basic_user_file /usr/local/nginx/conf/htpasswd; //指定用戶密碼檔案
}
}
# yum install -y httpd //安裝httpd
# htpasswd -c /usr/local/nginx/conf/htpasswd yuhuai //創建yuhuai用戶
New password: //輸入密碼000000
Re-type new password: //確認密碼000000
Adding password for user yuhuai //創建成功
# /usr/local/nginx/sbin/nginx -t //檢查組態檔
# /usr/local/nginx/sbin/nginx -s reload //重新載入
# mkdir /data/nginx/yuhuai.com
# echo "yuhuai.com" > /data/nginx/yuhuai.com/index.html
# curl -I -x 127.0.0.1:80 yuhuai.com
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.2
Date: Wed, 16 Dec 2020 02:26:27 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth" 說明:狀態碼401表示該網站需要驗證,
打開電腦,C:\Windows\System32\drivers\etc 目錄下的hosts微博華北,在最后加上以上 192.168.134.140 yuhuai.com (虛擬機IP和虛擬網路),然后再瀏覽器中訪問yuhuai.com !!! hosts檔案不能隨意刪減,

出現如上圖,輸入用戶名和密碼,就可以訪問了,
如果是針對某個目錄做用戶認證,需要修改location后面的路徑:
location /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/ngin/conf/htpasswd;
}
3.2.3域名重定向
Nginx的域名重定向,如下:
# vim yuhuai.com.conf //修改內容如下
server { listen 80; server_name yuhuai.com yuhuai1.com yuhuai2.com; index index.html index.htm index.php; root /data/nginx/yuhuai.com; if ($host != 'yuhuai.com' ){ rewrite ^/(./*)$ http://yuhuai.com/$1 permanent; } }
在Nginx中server_name后面可以跟多個域名,permanent為永久重定向,相當于httpd的R=301,還有一個常用的redirect,相當于httpd的R=302.
測驗程序如下:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# curl -x127.0.0.1:80 yuhuai1.com/123.txt -I HTTP/1.1 301 Moved Permanently Server: nginx/1.12.2 Date: Wed, 16 Dec 2020 03:04:13 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://yuhuai.com/123.txt
3.2.4Nginx的訪問日志
# grep -A2 log_format /usr/local/nginx/conf/nginx.conf
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' //combined_realip為日志格式的名字,remote_addr為訪問網站的用戶的出口IP;http_x_forwarded_for為代理服務器的IP,time_local為當前的時間
' $host "$request_uri" $status' //host為訪問的主機名;request_uri為訪問的URL地址,status為狀態碼;
' "$http_referer" "$http_user_agent"' //http_referer為referer地址;http_user_agent為user_agent
到虛擬主機組態檔中指定訪問日志的路徑:
# vim yuhuai.com.conf
server { listen 80; server_name yuhuai.com yuhuai1.com yuhuai2.com; index index.html index.htm index.php; root /data/nginx/yuhuai.com; if ($host != 'yuhuai.com' ) { rewrite ^/(.*$) http://yuhuai.com/$1 permanent; } access_log /tmp/1.log combined_realip; //使用access_log 來指定日志的儲存路徑,最后面指定日志的格式名字, }
測驗程序如下:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# curl -x127.0.0.1:80 yuhuai.com/111 //生成訪問日志
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>
# cat /tmp/1.log //查看日志
127.0.0.1 - [16/Dec/2020:11:45:48 +0800] yuhuai.com "/111" 404 "-" "curl/7.29.0"
想要切割Nginx日志需要借助系統的切割工具或者自定義腳本,如:
# vim /usr/local/sbin/nginx_log_rotate.sh //寫入以下內容
#! /bin/bash ## /data/log d=`date -d "-1 day" +%/Y%m%d` logdir ="/data/logs" nginx_pid ="/usr/local/nginx/logs/nginx.pid" cd $logdir for log in `ls *.log` do mv $log $log-$d done /bin/kill -HUP `cat $nginx_pid`
寫完腳本后,還需要增加任務計劃:
# crontab -e //寫入以下內容
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
3.3配置靜態檔案不記錄日志并添加過期時間
虛擬主機組態檔改寫如下:
# vim yuhuai.com.conf //改寫成以下內容
server { listen 80; server_name yuhuai.com yuhuai1.com yuhuai2.com; index index.html index.htm index.php; root /data/nginx/yuhuai.com; if ($host != 'yuhuai.com' ) { rewrite ^/(.*$) http://yuhuai.com/$1 permanent; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ //使用location~可以指定對應的靜態檔案, { expires 7d; //expires配置過期時間 access_log off; //access_log配置為off就可以不記錄日志了 } location ~.*\.(js|css)$ { expires 12h; access_log off; } access_log /tmp/1.log combined_realip; }
測驗:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# echo "111" > /data/nginx/yuhuai.com/1.js //創建js檔案
# echo "222" > /data/nginx/yuhuai.com/1.jpg //創建jpg檔案
# touch /data/nginx/yuhuai.com/1.jss //創建一個對比檔案
# curl -x127.0.0.1:80 yuhuai.com/1.js -I //訪問js型別的檔案,快取過期時間為12小時
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 16 Dec 2020 06:15:00 GMT
Content-Type: application/javascript
Content-Length: 4
Last-Modified: Wed, 16 Dec 2020 03:59:45 GMT
Connection: keep-alive
ETag: "5fd98631-4"
Expires: Wed, 16 Dec 2020 18:15:00 GMT
Cache-Control: max-age=43200
Accept-Ranges: bytes
# curl -x127.0.0.1:80 yuhuai.com/2.jpg -I //訪問jpg型別的檔案,快取過期時間為7小時
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 16 Dec 2020 06:16:05 GMT
Content-Type: image/jpeg
Content-Length: 4
Last-Modified: Wed, 16 Dec 2020 04:00:00 GMT
Connection: keep-alive
ETag: "5fd98640-4"
Expires: Wed, 23 Dec 2020 06:16:05 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
# curl -x127.0.0.1:80 yuhuai.com/1.jss -I
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 16 Dec 2020 06:17:14 GMT
Content-Type: application/octet-stream
Content-Length: 0
Last-Modified: Wed, 16 Dec 2020 04:00:27 GMT
Connection: keep-alive
ETag: "5fd9865b-0"
Accept-Ranges: bytes
可以很清楚的看到Cache-Control對應的時間大小(秒為單位),還可以查看日志,
# cat /tmp/1.log 127.0.0.1 - [16/Dec/2020:11:45:48 +0800] yuhuai.com "/111" 404 "-" "curl/7.29.0" 127.0.0.1 - [16/Dec/2020:12:02:36 +0800] yuhuai.com "/1.jss" 200 "-" "curl/7.29.0" 127.0.0.1 - [16/Dec/2020:12:02:42 +0800] yuhuai.com "/1.jss" 200 "-" "curl/7.29.0"
3.4Nginx防盜鏈
把防盜鏈,過期時間、不記錄日志組合在一起,
# vim yuhuai.com.conf
location ~* ^.+\.(gif|jpg|jpeg|png|bmp|swf|flv|rar|zip|doc|pdf|gz|bz2|xls)$
{
expires 7d;
valid_referers none blocked server_names *.yuhuai.com;
if ($invalid_referer ) {
return 403;
}
測驗:
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reload
# curl -x127.0.0.1:80 -I -e "httpd://yuhuai.com/1.txt" yuhuai.com/2.jpg
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 16 Dec 2020 07:18:22 GMT
Content-Type: image/jpeg
Content-Length: 4
Last-Modified: Wed, 16 Dec 2020 04:00:00 GMT
Connection: keep-alive
ETag: "5fd98640-4"
Expires: Wed, 23 Dec 2020 07:18:22 GMT
Cache-Control: max-age=604800
Accept-Ranges: bytes
可也i看到不僅僅有過期時間還有防盜鏈功能,
3.5訪問控制
例:“是訪問yu目錄得請求只允許192.168.134.140和127.0.0.1訪問”組態檔如下:
# vim yuhuai.com.conf
location /yu/ { allow 192.168.134.140; allow 127.0.0.1; deny all; }
測驗:
# mkdir /data/nginx/yuhuai.com/yu
# echo "123" > /data/nginx/yuhuai.com/yu/1.html
# curl -x127.0.0.1:80 yuhuai.com/yu/1.html
123
組態檔中的IP也可以為IP段,比如可以寫成allow192.168.134.0/24.如果拒絕幾個IP可以寫成:
location /yu/
{
deny 192.168.134.140
deny 127.0.0.1;
}
除了簡單的限制目錄還可以根據正則匹配來限制“
location ~.*(abc|image)/.*\.php$
{
deny all; //管道符在他們之間是”或者“的意思,這樣就能把訪問你得URL中帶有abc或者image字串,而且是php的請求拒絕訪問,
}
把上傳檔案的目錄禁止決議php,目的是保證安全,在Nginx配置里,可以針對user_agent做一些限制,
if ($http_user_agent ~`Spider/3.0|YoudaoBot|Tomato')
{
return 403;
}
3.6Nginx決議PHP
location ~ \.php$
{
include fastcgi_params;
fastcgi_pass unix:/tmp/php-fcgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/nginx/yuhuai.com$fastcgi_script_name;
}
其中fastcgi_pass用來指定php-fpm的地址,
factcgi_param SCRIPT_FILENAME后面跟的路徑為該站點的根目錄,和前面定義的root那個路徑保持一致,
如果配置不對,訪問php界面會出現404,
Nginx代理和配置SSL因為特殊原因就沒有在這進行闡述了,
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/236857.html
標籤:Linux
上一篇:linux使用dbus
下一篇:vmware安裝linux教程
