源代碼編譯安裝 lnmp

lnmp環境搭建

前置條件

  1. 操做系統安裝:CentOS 6.8 64位最小化安裝。php

  2. 配置好IP、DNS、網關、主機名html

  3. 配置防火牆,開啓80、3306端口mysql

 
 
 
 
 
vim /etc/sysconfig/iptables
關閉訪問牆
service iptables stop
/etc/init.d/iptables restart #最後重啓防火牆使配置生效
 
  1. 關閉SELinuxlinux

    vi /etc/selinux/configSELINUX=enforcing #註釋掉nginx

    SELINUXTYPE=targeted #註釋掉web

    SELINUX=disabled #增長:wq! #保存退出setenforce 0 #使配置當即生效sql

系統約定

 
 
 
 
 
軟件源代碼包存放位置:/lnmp/src
源碼包編譯安裝位置:/usr/local/軟件名
數據庫數據文件存儲路徑/data/mysql
 

系統軟件包版本

#已經附加在文件夾中

安裝編譯工具及庫文件

 
 
 
 
 
使用CentOS yum命令一鍵安裝
yum install -y make apr* autoconf automake curl curl-devel gcc gcc-c++ cmake gtk+-devel zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl kernel-headers compat* cpp glibc libgomp libstdc++-devel keyutils-libs-devel libarchive   libsepol-devel libselinux-devel krb5-devel libXpm* freetype freetype-devel freetype* fontconfig fontconfig-devel libjpeg* libpng* php-common php-gd gettext gettext-devel ncurses* libtool* libxml2 libxml2-devel patch policycoreutils bison
 

軟件安裝篇

 
 
 
 
 
一、安裝cmake
cd  /lnmp/src/cmake-2.8.7
tar -zxvf cmake-2.8.7.tar.gz
cd cmake-2.8.7
./configure --prefix=/usr/local/cmake
make #編譯
make install #安裝
vim /etc/profile 在path路徑中增長cmake執行文件路徑
export PATH=$PATH:/usr/local/cmake/bin
source /etc/profile使配置當即生效
 
 
 
 
 
 
二、安裝pcre
cd /lnmp/src/
tar -zxvf pcre-8.39.tar.gz
cd pcre-8.39
mkdir /usr/local/pcre #建立安裝目錄
./configure --prefix=/usr/local/pcre 
make && make install
三、安裝libmcrypt
cd /lnmp/src/libmcrypt-2.5.8
tar -zxvf libmcrypt-2.5.8.tar.gz
./configure  && make && make install#配置
make #編譯
make install #安裝
四、安裝gd庫
cd /lnmp/src/
tar zxvf gd-2.0.36RC1.tar.gz
cd gd-2.0.36RC1
./configure --enable-m4_pattern_allow --prefix=/usr/local/gd --with-jpeg=/usr/lib --with-png=/usr/lib --with-xpm=/usr/lib --with-freetype=/usr/lib --with-fontconfig=/usr/lib 
make #編譯
make install #安裝
五、安裝Mysql
groupadd mysql #添加mysql組
useradd -g mysql mysql -s /sbin/nologin #建立用戶mysql並加入到mysql組,不容許mysql用戶直接登陸系統
mkdir -p /var/mysql/data #建立MySQL數據庫存放目錄
chown -R mysql:mysql /var/mysql/data #設置MySQL數據庫目錄權限
tar -zxvf mysql-5.5.28.tar.gz #解壓
 
 
 
 
 
 
cd mysql-5.5.28
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/var/mysql/data \
-DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_DATADIR=/var/mysql/data -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306
make
make install
cd /usr/local/mysql
cp ./support-files/my-huge.cnf /etc/my.cnf #拷貝配置文件(注意:若是/etc目錄下面默認有一個my.cnf,直接覆蓋便可)
vi /etc/my.cnf #編輯配置文件,在 [mysqld] 部分增長
datadir = /var/mysql/data #添加MySQL數據庫路徑
./scripts/mysql_install_db --user=mysql #生成mysql系統數據庫
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld #把Mysql加入系統啓動
chmod 755 /etc/init.d/mysqld #增長執行權限
chkconfig mysqld on #加入開機啓動
vi /etc/rc.d/init.d/mysqld #編輯
basedir=/usr/local/mysql #MySQL程序安裝路徑
datadir=/var/mysql/data #MySQl數據庫存放目錄
service mysqld start #啓動,可能沒法寫入pid文件,注意將mysql用戶權限加入至/usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
vi /etc/profile #把mysql服務加入系統環境變量:在最後添加下面這一行
export PATH=$PATH:/usr/local/cmake/bin:/usr/local/mysql/bin
source /etc/profile #使配置當即生效
mkdir /var/lib/mysql #建立目錄
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock #添加軟連接
mysql_secure_installation #設置Mysql密碼,根據提示按Y 回車輸入2次密碼
/usr/local/mysql/bin/mysqladmin -u root -p password "123456" #或者直接修改密碼
到此,mysql安裝完成!
 
 
 
 
 
 
六、安裝 nginx
cd /lnmp/src
tar -zxvf nginx-1.11.5.tar.gz
groupadd www #添加www組
useradd -g www www -s /sbin/nologin #建立nginx運行帳戶www並加入到www組,不容許www用戶直接登陸系統
cd /lnmp/src/
tar -zxvf openssl-1.1.0b.tar.gz
cd nginx-1.11.5
./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www   --with-http_stub_status_module --with-openssl=/lnmp/src/openssl-1.1.0b --with-pcre=/lnmp/src/pcre-8.39   --with-http_ssl_module
注意:--with-pcre=/lnmp/src/pcre-8.39指向的是源碼包解壓的路徑,而不是安裝的路徑,不然會報錯
 
 
 
 
 
 
make
make install
#/usr/local/nginx/sbin/nginx #啓動nginx
設置nginx開啓啓動
vi /etc/rc.d/init.d/nginx #編輯啓動文件添加下面內容
=======================================================
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginxc daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
=======================================================
:wq! #保存退出
chmod 775 /etc/rc.d/init.d/nginx #賦予文件執行權限
chkconfig nginx on #設置開機啓動
/etc/rc.d/init.d/nginx restart #從新啓動Nginx
service nginx restart
=======================================================
 
 
 
 
 
 
七、安裝php
cd /lnmp/src
tar -jxvf php-7.0.7.tar.bz2
 cd php-7.0.7
./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mysqlnd --with-mysql-sock=/usr/local/mysql/mysql.sock --with-gd --with-iconv --with-zlib --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --with-jpeg-dir --with-freetype-dir   --with-pdo-mysql=/usr/local/mysql/
make #編譯,,若遇到make: *** [ext/fileinfo/libmagic/apprentice.lo] 錯誤 ,這加參數–-disable-fileinfo
make install #安裝
cd /lnmp/src/php-7.0.7
cp php.ini-production /usr/local/php7/etc/php.ini #複製php配置文件到安裝目錄
rm -rf /etc/php.ini #刪除系統自帶配置文件
ln -s /usr/local/php7/etc/php.ini /etc/php.ini #添加軟連接
 
 
 
 
 
 
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf #拷貝模板文件爲php-fpm配置文件
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf  
vi /usr/local/php7/etc/php-fpm.d/www.conf  #編輯
user = www #設置php-fpm運行帳號爲www
group = www #設置php-fpm運行組爲www
vim /usr/local/php7/etc/php-fpm.conf
pid = run/php-fpm.pid #取消前面的分號
加入服務並開機啓動 ,設置 php-fpm開機啓動
#cp /lnmp/src/php-7.0.7/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm #拷貝php-fpm到啓動目錄
chmod +x /etc/rc.d/init.d/php-fpm #添加執行權限
chkconfig php-fpm on #設置開機啓動
vi /usr/local/php7/etc/php.ini #編輯配置文件
 
 
 
 
 
 
這裏暫時不給禁用
找到:disable_functions =
修改成:disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
 

列出PHP能夠禁用的函數,若是某些程序須要用到這個函數,能夠刪除,取消s禁用shell

找到:;date.timezone =
修改成:date.timezone = PRC #設置時區
找到:expose_php = On
修改成:expose_php = OFF #禁止顯示php版本的信息
找到:short_open_tag = Off
修改成:short_open_tag = ON #支持php短標籤

<?= ?>

8、配置nginx支持php
vi /usr/local/nginx/conf/nginx.conf
修改/usr/local/nginx/conf/nginx.conf 配置文件,需作以下修改
 
 
 
 
 
    user www www; #首行user去掉註釋,修改Nginx運行組爲www www;必須與/usr/local/php/etc/php-fpm.conf中的user,group配置相同,不然php運行出錯
    user www www; #重點修改
    worker_processes 1;
    events {
    worker_connections 1024;
    }
    http {
     include mime.types;
     default_type application/octet-stream;
     sendfile on;
     keepalive_timeout 65;
     server {
      listen 80;
      server_name localhost;
       location / {
       root /data/www;
       index index.php index.html index.htm;#添加index.php
       }
       location ~ \.php$ {
       root /data/www; #重點修改
       fastcgi_pass 127.0.0.1:9000;
       fastcgi_index index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;#注意Nginx這裏有個坑
       include fastcgi_params;
       }
     }
    }
    mkdir -p /data/www
    chown www:www /data/www/ -R #設置目錄全部者
    chmod 700 /data/www -R #設置目錄權限
 

 

 
 
 
 
 
服務器相關操做命令
service nginx restart #重啓nginx
service mysqld restart #重啓mysql
/usr/local/php7/sbin/php-fpm #啓動php-fpm
/etc/rc.d/init.d/php-fpm restart #重啓php-fpm
/etc/rc.d/init.d/php-fpm stop #中止php-fpm
/etc/rc.d/init.d/php-fpm start #啓動php-fpm
 

相關文章
相關標籤/搜索