Linux 系統為 CentOS 7.2
1. 安裝 Nginx
- 安裝 Nginx 依賴包:
# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
- 安裝 Nginx:
# tar -xzvf nginx-1.10.1.tar.gz # cd nginx-1.6.2 # ./configure --prefix=/usr/local/nginx # make && make install
- 啟動 Nginx:
# /usr/local/nginx/sbin/nginx
-
瀏覽器訪問服務器地址看是否成功,如果不能訪問則關閉防火牆:
# systemctl start firewalld.service #啟動 # systemctl stop firewalld.service #停止 # systemctl disable firewalld.service #禁止 firewall 開機啟動
再次訪問成功:
nginx 安裝成功 -
設置開機啟動:
新建 Nginx 服務文件;# vim /lib/systemd/system/nginx.service
保存以下內容,並設置權限為 754;
[Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx start # 如果報錯(nginx: invalid option: "start"), 則不加 start 參數 ExecReload=/usr/local/nginx/sbin/nginx restart ExecStop=/usr/local/nginx/sbin/nginx stop PrivateTmp=true [Install] WantedBy=multi-user.target # chmod 754 /lib/systemd/system/nginx.service # systemctl enable nginx.service
重啟服務器,訪問瀏覽器成功。查看 Nginx 狀態:
# systemctl status nginx.service
active nginx 服務正在運行
2. 安裝 PHP
參考自: http://blog.csdn.net/u010861514/article/details/51926575
- 解壓文件
# tar -xzf php-7.0.11.tar.gz # cd php-7.0.11/
-
安裝依賴
# yum -y install gcc gcc-c++ libxml2 libxml2-devel bzip2 bzip2-devel libmcrypt libmcrypt-devel openssl openssl-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel readline readline-devel libxslt-devel perl perl-devel psmisc.x86_64 recode recode-devel libtidy libtidy-devel
如果 yum 安裝不了,則下載無法安裝的包手動編譯安裝,如下:
libmcrypt 下載地址:http://sourceforge.net/projects/mcrypt/files/Libmcrypt/# tar -xzvf libmcrypt-2.5.8.tar.gz # cd libmcrypt-2.5.8 # ./configure # make && make install
libtidy 下載地址:http://fr.rpmfind.net/linux/rpm2html/search.php?query=libtidy&submit=Search+...
rpm 包安裝:# rpm -ivh libtidy-5.1.25-1.fc25.i686.rpm
-
編譯與配置
--prefix=/usr/local/php7 主程序文件路徑
--sysconfdir=/etc/php7 配置文件路徑
--with-config-file-path=/etc/php7 php.ini 文件路徑# ./configure --prefix=/usr/local/php7 --sysconfdir=/etc/php7 --with-config-file-path=/etc/php7 --enable-fpm --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mhash --with-openssl --with-zlib --with-bz2 --with-curl --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-zlib --enable-mbstring --with-mcrypt --enable-sockets --with-iconv-dir --with-xsl --enable-zip --with-pcre-dir --with-pear --enable-session --enable-gd-native-ttf --enable-xml --with-freetype-dir --enable-gd-jis-conv --enable-inline-optimization --enable-shared --enable-bcmath --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-mbregex --enable-pcntl --with-xmlrpc --with-gettext --enable-exif --with-readline --with-recode --with-tidy # make # make install
如果編譯出錯,則網上查找解決方案。
-
安裝成功后拷貝源碼包中的 php.ini-development 文件到 PHP 配置文件目錄
# cp php.ini-development /etc/php7/php.ini
- fastcgi php-fpm
# cp /etc/php7/php-fpm.conf.default /etc/php7/php-fpm.conf # cp /etc/php7/php-fpm.d/www.conf.default /etc/php7/php-fpm.d/www.conf # vi /etc/php7/php-fpm.d/www.conf
# 監聽地址 listen = 127.0.0.1:9000 # 允許的客戶端 listen.allowed_clients = 127.0.0.1
-
啟動 PHP
# vi /etc/php7/php-fpm.conf
// 打開注釋:(不打開注釋僅能使用 killall php-fpm 關閉 php) pid = run/php-fpm.pid // 啟動: # /usr/local/php7/sbin/php-fpm // 立刻終止 # kill -INT `cat /usr/local/php7/var/run/php-fpm.pid` # kill -TERM `cat /usr/local/php7/var/run/php-fpm.pid` # killall php-fpm // 平滑終止 # kill -QUIT `cat /usr/local/php7/var/run/php-fpm.pid` // 平滑重啟 # kill -USR2 `cat /usr/local/php7/var/run/php-fpm.pid`
-
加入環境變量
# vim /etc/profile 末尾添加: export PATH=$PATH:/usr/local/php7/sbin:/usr/local/php7/bin # source /etc/profile 立即生效
-
設置開機啟動
# vim /lib/systemd/system/php-fpm.service # chmod 754 /lib/systemd/system/php-fpm.service # systemctl enable php-fpm.service
文件內容:
[Unit] Description=php-fpm After=syslog.target network.target [Service] Type=forking ExecStart=/usr/local/php7/sbin/php-fpm ExecReload=/bin/kill -USR2 $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
PHP 服務相關命令:
# systemctl daemon-reload 修改 serivce 文件后,需要刷新配置文件 # systemctl (start | restart | reload | stop | enable | disable | status) php-fpm.service
若果報錯(ERROR: unable to bind listening socket for address ...)先把所有 PHP 進程殺掉,然后重啟:
# killall php-fpm # systemctl start php-fpm.service
-
配置 Nginx 訪問 PHP
# vim /usr/local/nginx/conf/nginx.conf ### 編輯 server 讓 Nginx 支持php: server { listen 80; server_name localhost; location / { root html; index index.html index.htm index.php; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
訪問 PHP 文件,在 Nginx 的 html 目錄下創建一個 PHP 文件,訪問該文件查看配置信息:

3. 安裝 PostgreSql
- 安裝依賴包
# yum install -y gcc.x86_64 glibc.x86_64 glibc-devel.x86_64 vim-enhanced.x86_64 gcc-java apr apr-devel openssl openssl-devel libgcc.x86_64 java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 perl-Module-Install.noarch readline-devel.x86_64
- 創建 postgres 用戶(一定要創建,不然 root 用戶不能啟動數據庫)
# adduser postgres
- 解壓編譯安裝
# tar -xzvf postgresql-9.6.0.tar.gz # cd postgresql-9.6.0 # ./configure --prefix=/usr/local/pgdb --enable-thread-safety # make # make install
- 設置權限
# chown -R postgres.postgres /usr/local/pgdb/
-
配置 PostgreSql
======= 以下操作必須在 postgres 用戶下完成=======
切換用戶# su postgres $ cd ~ 切換到用戶目錄
編輯 .bash_profile 添加以下內容
$ vim .bash_profile # 添加以下內容,配置 PGDATA 變量 PGHOME=/home/postgres export PGHOM PGDATA=$PGHOME/data export PGDATA
初始化數據文件
$ cd /usr/local/pgdb $ mkdir data $ cd /usr/local/pgdb/bin $ ./initdb -D ../data 初始化數據文件 $ ./pg_ctl -D ../data start 啟動數據庫服務,其中-D選項指定文件存放目錄為上一步中生成的data目錄
數據庫操作
$ ./createdb test 創建數據庫 test $ ./psql test 訪問 test
創建 test 成功