安裝wget:yum install wget -yphp
安裝gcc及g++:yum install gcc gcc-c++ -yhtml
後續全部源代碼都下載到/usr/local/src目錄mysql
防火牆更改配置及關閉selinux見另外一篇文章《LAMP環境搭建》linux
Nginx依賴pcre(重寫rewrite)、zlib(網頁gzip壓縮)及openssl(加密傳輸)。nginx
[root]wget http://pilotfiber.dl.sourceforge.net/project/pcre/pcre/8.38/pcre-8.38.tar.gzc++
[root]tar -xvzf pcre-8.38.tar.gzweb
[root]cd pcre-8.38sql
[root] ./configure --prefix=/usr/local/pcre數據庫
[root]make && make installapi
[root]wget http://zlib.net/zlib-1.2.8.tar.gz
[root]tar -xvzf zlib-1.2.8.tar.gz
[root]cd zlib-1.2.8
[root] ./configure --prefix=/usr/local/zlib
[root]make && make install
[root]wget http://www.openssl.org/source/openssl-1.0.2h.tar.gz
[root]tar -xvzf openssl-1.0.2h.tar.gz
[root]cd openssl-1.0.2h
[root] ./config --prefix=/usr/local/openssl
[root]make && make install
爲了安全起見,建立一個nginx帳號專門用於運行nginx,固然爲了簡便直接用root帳號運行的話(不推薦),就不須要建立nginx帳號及將nginx相關文件開放權限給nginx帳號。
[root]groupadd nginx
[root]useradd -g nginx nginx -s /bin/false#該帳號只用於運行nginx及相關軟件,不能登陸
[root]wget http://nginx.org/download/nginx-1.10.1.tar.gz
[root]tar -xvzf nginx-1.10.1.tar.gz
[root]cd nginx-1.10.1
[root] ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre=/usr/local/src/pcre-8.38 --with-zlib=/usr/local/src/zlib-1.2.8 --with-openssl=/usr/local/src/openssl-1.0.2h
[root]make && make install
提示:./configure --help能夠查看編譯選項
注意這裏:
./configure: error: the HTTP image filter module requires the GD library. You can either do not enable the module or install the libraries.
解決方法:
yum -y install gd-devel
[root]vi /usr/local/nginx/conf/nginx.conf
若須要,則將http -> server -> server_name改成服務器的外網ip地址,或你的網站域名
方法一:在/etc/rc.d/rc.local文件最後增長一行腳本
[root]/usr/local/nginx/sbin/nginx
方法二:將Nginx加入服務,新增/etc/init.d/nginx腳本,內容請見nginx腳本,而後設置開機自啓動:
[root]chmod +x /etc/init.d/nginx
[root]chkconfig nginx on #設置開啓自啓動後會自動將其加入服務
若Nginx已加入服務,則用service命令啓動服務
[root]service nginx start
不然運行Nginx程序
[root]/usr/local/nginx/sbin/nginx
[root]yum install ncurses-devel -y
[root]wget https://cmake.org/files/v3.5/cmake-3.5.2.tar.gz
[root]tar -xvzf cmake-3.5.2.tar.gz
[root]cd cmake-3.5.2
[root] ./configure --prefix=/usr/local/cmake
[root]make && make install
[root]export PATH=$PATH:/usr/local/cmake/bin#臨時加入PATH環境變量
同nginx同樣,建立一個mysql帳號專門用於運行mysql
[root]groupadd mysql
[root]useradd -g mysql mysql -s /sbin/false
[root]wget http://dev.mysql.com/Downloads/MySQL-5.6/mysql-5.6.31.tar.gz
[root]tar -xvzf mysql-5.6.31.tar.gz
[root]cd mysql-5.6.31
[root]
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DSYSCONFDIR=/etc -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \#mysql安裝到的路徑
-DSYSCONFDIR=/etc \ #mysql配置文件(my.cnf)路徑
-DMYSQL_DATADIR=/usr/local/mysql/data \ #data目錄路徑
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ #sock存放路徑
-DDEFAULT_CHARSET=utf8 \ #默認字符集
-DDEFAULT_COLLATION=utf8_general_ci#默認字符集校驗
[root]make && make install
如下全部操做都在/usr/local/mysql路徑下執行。
[root]scripts/mysql_install_db --user=mysql --datadir=/usr/local/mysql/data
[root]cp support-files/my-default.cnf /etc/my.cnf
[root]cp support-files/mysql.server /etc/init.d/mysqld#服務名也能夠取作mysql,隨你
[root]chkconfig mysqld on
[root]service mysqld start
[root]bin/mysql_secure_installation
而後設置密碼,並進行一些配置
[root]mysql -uroot -p
而後輸入密碼登陸。若運氣很差的話(好比我),輸入密碼登陸,裏面關閉,並輸出segment fault提示,那麼就須要修改源代碼並從新編譯安裝了。打開/usr/local/src/php.5.6.31/cmd-line-utils/libedit/terminal.c,在terminal_set函數中:
a、註釋char buf[TC_BUFSIZE];一行
b、將area = buf改爲area = NULL
而後從新編譯安裝。
本文最小安裝php。
[root]wget http://cn2.php.net/distributions/php-5.6.22.tar.bz2
[root]tar -xvjf php-5.6.22.tar.bz2
[root]cd php-5.6.22
[root] ./configure --prefix=/usr/local/php \ #php安裝路徑
--with-libdir=lib64 \ #64位操做系統須要
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-mysql_sock=/var/lib/mysql/mysql.sock \
--enable-fpm \
--enable-opcache \
--with-mhash \
--with-ldap#本人的項目用到,需yum install openldap-devel
[root]make && make install
如下命令都是在/usr/local/php路徑下執行。
[root]bin/php --ini
Configuration File (php.ini) Path: /usr/local/php/lib
[root]cp /usr/local/src/php-5.6.22/php.ini-production lib/php.ini
a.關閉在http頭中顯示php版本信息
expose_php = Off
b. 設置時區
date.timezone = PRC
如下命令都是在/usr/local/php路徑下執行。
[root]cp etc/php-fpm.conf.default etc/php-fpm.conf
1)去掉25行 ;pid = run/php-fpm.pid 前面的分號,使之生效
2)第148行改成 user = nginx 設置php-fpm運行帳號爲nginx
3)第149行改成 group = nginx #設置php-fpm運行組爲nginx
4)可選。php-fpm默認採用tcp通訊,若須要採用unix socket通信,則配置以下
listen = /dev/shm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
[root]cp /usr/local/src/php-5.6.22/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root]chmod +x /etc/init.d/php-fpm
[root]chkconfig php-fpm on
[root]service php-fpm start
[root]vi /usr/local/nginx/conf/nginx.conf
1)頂部行改爲 user nginx nginx;
2)將
location / {
root html;
index index.html index.htm
}
改成:
location / {
root /www;
index index.php index.html index.htm
}
注:須要將web根目錄/www開放權限給nginx帳號:chown nginx:nginx /www
3)取消location ~ \.php$ { 一段的註釋,以下:
location ~ \.php$ {
root /www;
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採用tcp通訊時的配置,若其採用unix socket通訊,則fastcgi_pass一行需該爲:
fastcgi_pass unix:/dev/shm/php-fpm.sock;
其實到這一步,php已經支持mysqli及pdo_mysql了(因爲mysql_connect等函數已經廢棄,因此在編譯php時沒有--with-mysql)。可是用mysqli_connect鏈接本機時,只能使用'127.0.0.1',而不能使用'localhost'來鏈接,緣由是:mysql經過tcp鏈接到127.0.0.1,經過unix socket鏈接到localhost。只要在php.ini設置mysqli及pdo_mysql的default_socket爲/var/lib/mysql/mysql.sock便可。貌似在編譯php時帶上--with-mysql-sock=/var/lib/mysql/mysql.sock選項就不用配置php.ini中的default_socket了。
[root]service php-fpm restart
[root]service nginx restart
新建/www/info.php文件,內容以下:
<?php phpinfo(); ?>
在瀏覽器中查看:localhost/info.php
新建/www/mysql.php文件,測試mysqli時內容爲:
<?php var_dump(mysqli_connect('localhost', 'root', '111111')); ?>
測試pdo_mysql時內容爲:
<?php var_dump(new PDO('mysql:host=localhost;db=mysql', 'root', '111111')); ?>
上面的root和111111爲mysql帳號和密碼。
分別在瀏覽器中查看:localhost/mysql.php,正常時,顯示內容分別含」object(mysqli)「和」object(PDO)「。
-------------------------------
安裝完mysql-server
會提示能夠運行mysql_secure_installation。運行mysql_secure_installation會執行幾個設置:
a)爲root用戶設置密碼
b)刪除匿名帳號
c)取消root用戶遠程登陸
d)刪除test庫和對test庫的訪問權限
e)刷新受權表使修改生效
經過這幾項的設置可以提升mysql庫的安全。建議生產環境中mysql安裝這完成後必定要運行一次mysql_secure_installation,詳細步驟請參看下面的命令:
複製代碼
代碼以下:
[root@server1 ~]#
mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS
RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP
CAREFULLY!
In order to log into MySQL to secure it, we'll need the
current
password for the root user. If you've just installed MySQL,
and
you haven't set the root password yet, the password will be blank,
so
you should just press enter here.
Enter current password for root (enter for
none):<–初次運行直接回車
OK, successfully used
password, moving on…
Setting the root password ensures that nobody can log
into the MySQL
root user without the proper authorisation.
Set root
password? [Y/n] <–
是否設置root用戶密碼,輸入y並回車或直接回車
New password: <– 設置root用戶的密碼
Re-enter new password: <– 再輸入一次你設置的密碼
Password updated
successfully!
Reloading privilege tables..
… Success!
By default, a
MySQL installation has an anonymous user, allowing anyone
to log into MySQL
without having to have a user account created for
them. This is intended only
for testing, and to make the installation
go a bit smoother. You should
remove them before moving into a
production environment.
Remove anonymous
users? [Y/n] <–
是否刪除匿名用戶,生產環境建議刪除,因此直接回車
… Success!
Normally, root should only be
allowed to connect from 'localhost'. This
ensures that someone cannot guess
at the root password from the network.
Disallow root login remotely?
[Y/n]
<–是否禁止root遠程登陸,根據本身的需求選擇Y/n並回車,建議禁止… Success!By default, MySQL comes with a database named 'test' that anyone canaccess. This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [Y/n] <– 是否刪除test數據庫,直接回車- Dropping test database…… Success!