記錄:CentOS7.2配置LNMP環(huán)境記錄
CentOS7.2配置LNMP環(huán)境記錄
php 5.6+ nginx 1.10+ mysql 5.5+
LNMP是Linux、Nginx、MySQL(MariaDB)和PHP的縮寫,這個組合是最常見的WEB服務器的運行環(huán)境之一。本文將帶領大家在CentOS 7操作系統(tǒng)上搭建一套LNMP環(huán)境。
本教程適用于CentOS 7.x版本。
準備工作
更新 yum 源,自帶的源沒有 PHP5.6 :
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
安裝 epel:
yum install epel-release
升級系統(tǒng)
yum update
準備工作完成,開始安裝!
安裝Nginx
CentOS系統(tǒng)模板中配置了內(nèi)網(wǎng)源,下載速度較快,推薦使用yum安裝Nginx:
sudo yum install nginx
按照提示,輸入yes后開始安裝。安裝完畢后,Nginx的配置文件在/etc/nginx目錄下。使用以下命令啟動Nginx:
sudo systemctl start nginx
檢查系統(tǒng)中firewalld防火墻服務是否開啟,如果已開啟,我們需要修改防火墻配置,開啟Nginx外網(wǎng)端口訪問。
sudo systemctl status firewalld
如果顯示active (running),則需要調整防火墻規(guī)則的配置。
修改/etc/firewalld/zones/public.xml文件,在zone一節(jié)中增加:
<zone>
...
<service name="nginx"/>
<zone>
保存后重新加載firewalld服務:
sudo systemctl reload firewalld
您可以通過瀏覽器訪問 http://外網(wǎng)IP地址 來確定Nginx是否已經(jīng)啟動。
【注意:很重要?。。?/strong>】
部分童鞋發(fā)現(xiàn)按照教程操作最后無法訪問,這是云服務器默認關閉了80端口。這個請移步云服務里控制臺打開80端口的外網(wǎng)訪問,如需步驟,請自行百度!
最后將Nginx設置為開機啟動:
sudo systemctl enable nginx.service
這么Nginx就安裝成功了!
安裝MySQL(MariaDB)
MariaDB是MySQL的一個分支,主要由開源社區(qū)進行維護和升級,而MySQL被Oracle收購以后,發(fā)展較慢。在CentOS 7的軟件倉庫中,將MySQL更替為了MariaDB。
我們可以使用yum直接安裝MariaDB:
sudo yum install mariadb-server
安裝完成之后,執(zhí)行以下命令重啟MariaDB服務:
sudo systemctl start mariadb
MariaDB默認root密碼為空,我們需要設置一下,執(zhí)行腳本:
sudo /usr/bin/mysql_secure_installation
這個腳本會經(jīng)過一些列的交互問答來進行MariaDB的安全設置。
首先提示輸入當前的root密碼:
- Enter current password for root (enter for none):
初始root密碼為空,我們直接敲回車進行下一步。 - Set root password? [Y/n]
設置root密碼,默認選項為Yes,我們直接回車,提示輸入密碼,在這里設置您的MariaDB的root賬戶密碼。 - Remove anonymous users? [Y/n] 是否移除匿名用戶,默認選項為Yes,建議按默認設置,回車繼續(xù)。
- Disallow root login remotely? [Y/n]
是否禁止root用戶遠程登錄?如果您只在本機內(nèi)訪問MariaDB,建議按默認設置,回車繼續(xù)。 如果您還有其他云主機需要使用root賬號訪問該數(shù)據(jù)庫,則需要選擇n。 - Remove test database and access to it? [Y/n] 是否刪除測試用的數(shù)據(jù)庫和權限?
建議按照默認設置,回車繼續(xù)。 - Reload privilege tables now? [Y/n]
是否重新加載權限表?因為我們上面更新了root的密碼,這里需要重新加載,回車。
完成后你會看到Success!的提示,MariaDB的安全設置已經(jīng)完成。我們可以使用以下命令登錄MariaDB:
mysql -uroot -p
按提示輸入root密碼,就會進入MariaDB的交互界面,說明已經(jīng)安裝成功。
最后我們將MariaDB設置為開機啟動。
sudo systemctl enable mariadb
安裝PHP
我們可以直接使用yum安裝PHP:
sudo yum install php56w-fpm php56w-mysql php56w-mysqli php56w php56w-opcache php56w-gd php56w-intl php56w-mbstring php56w-exif php56w-mcrypt php56w-openssl
//把該安裝的一次性裝到位
安裝完成后我們將php-fpm啟動:
sudo systemctl start php-fpm
將php-fpm設置為開機啟動:
sudo systemctl enable php-fpm
接下來需要注意了!配置Nginx--多個站點
我給大家提供一個范本作為參考:
nginx.conf
//里面我會詳細的給予中文注釋
vi /etc/nginx/nginx.conf
//編輯nginx.conf的命令
以下為conf文件內(nèi)容:
# 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; #nginx.pid為記錄nginx主進程pid文件;切勿修改、移動
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
#引入/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;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
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;
#這句很重要,引入所有etc/nginx/conf.d/目錄下的.conf文件
#***etc/nginx/conf.d/目錄存放的就是分站點的文件(下面會給出實例代碼)***
server {
#由于我們的nginx需要配置多站點,所以在此就需要注釋一些東西
listen 80 default_server;
listen [::]:80 default_server;
#保留監(jiān)聽的端口
# server_name _;
# root /usr/share/nginx/php;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
# location / {
# }
# error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# location ~ \.php$ {
# root /usr/share/php;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include fastcgi_params;
# }
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
#注意:此份nginx.conf可以直接復制了去使用!~好用了就給博主打個賞錢!謝謝!
配置完nginx之后我們該干啥、?當然是重啟nginx唄
service nginx start #啟動nginx
service nginx stop #停止nginx
service nginx restart #重啟nginx
sudo systemctl reload nginx #或者執(zhí)行這條
重啟完畢,繼續(xù)打開 http://外網(wǎng)IP地址 來確定Nginx是否已經(jīng)啟動。
此時,服務器啟動的是nginx和apache。
而且php-fpm默認發(fā)送到apache。
所以咱們還得繼續(xù)修改一下php-fpm。
配置 php-fpm
vi /etc/php-fpm.d/www.conf
#編輯php-fpm配置文件
修改user和group (源代碼為:user = apache group = apache)
user = nginx
group = nginx
修改完了之后,還是老樣子,重啟php-fpm服務
service php-fpm start #啟動php-fpm
service php-fpm stop #停止php-fpm
service php-fpm restart #重啟php-fpm
最后,咱們需要為nginx添加站點了
添加站點這我先給大家一個截圖,以幫助大家迅速的了解是怎么回事
大家應該看的很清楚了,猜都可以猜到,博主這一共配置了三個站點,這三個站點是怎么被nginx引入的呢?
我給大家貼出nginx的配置文件的里面應該有這么一句(注意圖中的紅框,上面的是地址)
include /etc/nginx/conf.d/*.conf;
#這句很重要,引入所有etc/nginx/conf.d/目錄下的.conf文件
#***etc/nginx/conf.d/目錄存放的就是分站點的文件(下面會給出實例代碼)***
好的,大家應該能準確理解了,如果還是理解不了的話只能缺你回去喝點三鹿了!
下面我給大家貼出nginx站點配置文件的代碼,修改修改就可以用
看代碼的時候請注意看里面的路徑,當然我也還是會給一定的中文注釋
#這個文件是上面的qopmall.com.conf
server {
server_name qopmall.com www.qopmall.com;#這里就是你要綁定的域名了,空格分開
location / {
root /usr/share/php/weixin; #這里是你站點存放的文件夾名稱(也就是說,你當前這個站點的文件全部都丟在這個路徑的weixin文件夾里面)
index index.php index.html index.htm; #這里照抄即可
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/php/weixin; #這里的配置等同于上面的那個root配置
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/php/weixin/$fastcgi_script_name; #這里的配置也是和上面的root配置一樣
include fastcgi_params;
}
}
代碼非常簡單,我沒注釋到的不用修改就行。
上面的路徑,比如/usr/share/php/weixin 這就是你站點的根目錄,我給大家截圖參考:
各位童鞋,創(chuàng)建好站點了,先寫個簡單的php程序測試一下是否正常,比如info。