
在Linode , Vultr , Cloudcone 的 VPS上安裝 LEMP的方法,我是以實際安裝CentOS 7.5 Server - 64 Bit (Latest Kernel + BBR)所做的紀錄 ,
本篇皆需要在root帳號下操作,安裝完成後請務必adduser使用sudo來管理系統會比較安全。 使用 putty 登入vps給的 server (ip-based)
用hostnamectl 指令查詢一下主機相關資訊:
指令: hostnamectl
Static hostname: yourhostname
Icon name: computer-vm
Chassis: vm
Machine ID: f0aa2369d99d4150a1b73b773fbcca5c
Boot ID: feb82ec1485945abb571b71608b10434
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 4.19.8-1.el7.elrepo.x86_64
Architecture: x86-64
看看時區是否正確
指令: date
若不是你現在的時間就要設定好,之後看 log 設定 crontab 時才會正確
設定台灣 CST 時區的指令
指令: timedatectl set-timezone Asia/Taipei
先升級 yum , 之後用 yum 來安裝所有的套件
基本套件安裝
yum -y update or yum -y upgrade
安裝 nano 編輯器
yum -y install nano
安裝net-tools (ifconfig,netstat,traceroute) yum -y install net-tools
yum -y install traceroute
安裝常用工具 yum -y install wget
yum -y install lynx
安裝Epel擴充資源庫(Extra Packages for Enterprise Linux)
yum -y install epel-release
yum -y install yum-utils
安裝LEMP(在Linux上安裝nginx,Mariadb,Php)
1.) nginx:
yum -y install nginx
systemctl enable nginx
systemctl start nginx
2.) mariadb yum -y install mariadb-server
systemctl enable mariadb
systemctl start mariadb
接著馬上更改 mariadb的密碼 mysql
;
USE mysql;
UPDATE user SET Password=password(‘your-new-password’) WHERE User=’root’
或是執行 mysql_secure_installation
也可以。詳細的設定在/etc/my.cnf中。
mariadb or mysqld 的調校可用 mysqltuner 來調整。
3.) PHP-fpm
yum -y install centos-release-scl
設定檔/etc/opt/rh/rh-php72/php-fpm.d/www.conf
yum -y install rh-php72 rh-php72-php-fpm rh-php72-php-mysqlnd
user = nginx
group = nginx systemctl start rh-php72-php-fpm
systemctl enable rh-php72-php-fpm
這時 nginx 與 php-fpm 各自跑各自的,還需要回去設定nginx
設定 nano /etc/nginx/nginx.conf
在 server 的 {} 中加入
server {
….
….
….
index index.php index.html index.htm;
location ~ .php$ {
try_files $uri =404;
#fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
…
…
}
設定好之後,重新啟動 PHP-FPM 與 Nginx 服務
systemctl restart rh-php72-php-fpm
systemctl restart nginx
nano /usr/share/nginx/html/info.php
<?php phpinfo(); ?>
打開chrome or firefox 開啟 http://your-vps-ip/info.php
若看到伺服器相關資訊就表示成功了
若你的php程式會使用到session,請將session的擁有人改成nginx chown nginx:nginx -R /var/opt/rh/rh-php72/lib/php/session
設定name-virtualserver
指令 mkdir /etc/nginx/virtualserver
nano /etc/nginx/nginx.conf
在nginx.conf中加入
include /etc/nginx/virtualserver/*.conf ;
在目錄 /etc/nginx/virtualserver 中
新增 your.domain.name.com.conf (例如: abc.com.conf)
指令 nano abc.com.conf
server {
server_name www.yourname.tw yourname.tw;
root /home/user/sites/site1;
index index.php index.html index.htm;
include /etc/nginx/default.d/*.conf;
location / {
try_files $uri $uri/ /index.php?$args;
}
}
若你要安裝 wordpress , 請注意 nginx.conf 中這幾個要mark掉
這部分花了我不少時間…. 🙁
#rewrite_log on;
#tcp_nopush on;
#tcp_nodelay on;
安裝ftpd
yum -y install pure-ftpd
systemctl enable pure-ftpd
systemctl start pure-ftpd