1 安裝前準備
爲了安裝額外的擴展庫, 最好安裝EPEL額外源, 我的系統是centos7
rpm -Uvh http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
2 安裝 nginx
yum instal nginx
- 開啓nginx服務
systemctl start nginx
- 查看nginx狀態
systemctl status nginx
有如下信息表示服務正常開啓
Active: active (running) since Wed 2017-01-18 05:37:48 UTC; 19h ago
設置開啓啓動
systemctl enable nginx
- 開啓防火牆
firewall-cmd --add-service=http --permanent
查看下端口有沒打開
iptables -n -L
如果看到如下信息, 表示服務開啓正常
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 ctstate NEW
3 安裝apache
yum install httpd
- 因爲nginx佔用了80端口, 給apache換個88端口
vi /etc/httpd/conf/httpd.conf
將Listen 的端口改成 88
開啓apache服務
systemctl start httpd
查看apache狀態
systemctl status httpd
看到這個表明服務器開啓正常
Active: active (running) since Tue 2017-01-17 02:48:38 UTC; 1 day 23h ago
然後你就可以看到apache的歡迎頁面了.
設置開機啓動
systemctl enable httpd
3 安裝mariadb
mariadb是mysql的一個開源分支
yum install mariadb mariadb-server
開啓mariadb服務
systemctl start mariadb
查看服務器狀態
systemctl status mariadb
設置root密碼
mysql_secure_installation
按照提示設置就可以了, 沒什麼特別
然後重啓mariadb服務
systemctl restart mariadb
4 安裝 php 及其擴展
yum install -y php php-fpm php-cli php-apc php-mysql php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-magickwand php-mbstring php-mcrypt php-mssql php-soap libjpeg* php-bcmath php-mhash php-pecl-memcache
安裝了一堆插件, 看自己想要增減
- 安裝完之後, apache可以直接解析php了, 主要是在 /etc/httpd/conf.d 多了php.conf的配置文件, 然後照例寫個測試
<?php
phpinfo();
?>
5 用php-fpm解析php
- 首先開啓php-fpm服務
systemctl start php-fpm
- 配置nginx
{
listen 80;
server_name www.foo.com;
root /data/www/foo;
index index.html index.php;
location / {
if(!-e $request_filename) {
rewrite . /index.php last;
}
}
location ~\.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
location ~/.ht {
deny all;
}
access_log /data/logs/foo.access.log;
}
使用 nginx -t, 發現出現如下錯誤
nginx: [emerg] unknown directive "if(!-e" in /etc/nginx/conf.d/foo.conf:9
度娘一下發現, if 和 ( 之間必須有個空格, oh, may god.
重新修改了一下
{
listen 80;
server_name www.foo.com;
root /data/www/foo;
index index.html index.php;
location / {
if (!-e $request_filename) {
rewrite . /index.php last;
}
}
location ~\.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
location ~/.ht {
deny all;
}
access_log /data/logs/foo.access.log;
}
重啓nginx, 沒再發現錯誤了, 打開網站, 竟然出現502 502 502
502 Bad Gateway
再次度娘, 發現很多都說連接數不夠的之類, 我網頁都沒打開過怎麼可能出現連接數的問題~~
直到找到這位仁兄的文章, 才明瞭, 再次感謝這個困擾我那麼舊的問題
http://www.tuicool.com/articles/jURRJf
竟然是yum安裝時, 只允許127.0.0.1的地址接入, 而轉發的是公網地址, 導致直接被deny, 可以通過日誌查看
tail -f /var/log/php-fpm/*.log
如果看不到錯誤信息, 還要修改配置www.conf, 將
catch_workers_output = yes
再看看錯誤信息
WARNING: [pool www] child 22741 said into stderr: "ERROR: Connection disallowed: IP address '172.16.9.59' has been dropped."
只要將配置文件www.conf中的
listen.allowed_clients = 127.0.0.1
註銷或者改成你的公網ip即可
- 其實還可以配置php-fprm使用unix套接字來處理php, 這樣就可以避免了tcp的開銷, 只需將
listen = 127.0.0.1:9000
改成
listen = /run/php-fpm/php5-fpm.sock
還要更改nginx的配置文件
{
listen 80;
server_name www.foo.com;
root /data/www/foo;
index index.html index.php;
location / {
if (!-e $request_filename) {
rewrite . /index.php last;
}
}
location ~\.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php-fpm/php5-fpm.sock;
fastcgi_index index.php;
}
location ~/.ht {
deny all;
}
access_log /data/logs/foo.access.log;
}
據說nginx配置文件還可以精簡下, 嘗試下改成這樣
server
{
listen 80;
server_name www.foo.com;
index index.html index.php;
root /data/www/foo;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~.php$ {
try_files $uri = 404;
include fastcgi.conf;
fastcgi_pass unix:/run/php-fpm/php5-fpm.sock;
}
location ~/.ht {
deny all;
}
access_log /data/logs/foo.access.log;
}
是因爲fastcgi.conf已經包含了fastcgi_params裏面的所有配置, 而且還包含了
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
所以清爽了不少.