源碼安裝
安裝條件:
事前準備
1.關閉防火牆centos7 使用firewalld
systemctl stop firewalld
2.關閉SELinux
vim /etc/selinux/configurations
SELINUX=enforcing #註釋掉
SELINUXTYPE=targeted #註釋掉
SELINUX=disabled #增加
:wq #保存退出
setenforce 0 #使配置立即生效
源碼包放置位置 /lnmp/
鏈接:https://pan.baidu.com/s/11M72TTjFDxrRTjqei7Uv3A
提取碼:ff6c
源碼編譯位置 /opt/
裝軟件
1、cmake
cd /lnmp/src/cmake-2.8.7
tar -zxvf cmake-2.8.7.tar.gz
cd cmake-2.8.7
./configure --prefix=/opt/cmake
make #編譯
make install #安裝
vim /etc/profile 在path路徑中增加cmake執行文件路徑
export PATH=$PATH:/opt/cmake/bin
source /etc/profile使配置立即生效
問題:tar (child): lbzip2: Cannot exec: No such file or directory 解決方法
安裝bzip2 yum -y install bzip2
2、pcre
cd /lnmp/src/
tar -zxvf pcre-8.39.tar.gz
cd pcre-8.39
./configure
mkdir /opt/pcre #創建安裝目錄
make && make install
3.gd
cd /lnmp/src/
tar zxvf gd-2.0.36RC1.tar.gz
cd gd-2.0.36RC1
./configure --enable-m4_pattern_allow --prefix=/opt/gd --with-jpeg=/usr/lib --with-png=/usr/lib --with-xpm=/usr/lib --with-freetype=/usr/lib --with-fontconfig=/usr/lib
make #編譯
make install #安裝
4.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.tar.gz
編譯
cmake -DCMAKE_INSTALL_PREFIX=/opt/mysql -DMYSQL_UNIX_ADDR=/opt/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
問題1:運行cmake命令是出現CMake Error: The source directory does not appear to contain CMakeLists.txt.的錯誤,是當前文件夾錯了,切換到mysql的源代碼文件夾
問題2:[mysql]編譯安裝報錯CMake Error: your C compiler: "CMAKE_C_COMPILE
報錯大致都是缺少依賴包,安裝這些就差不多了 yum install -y git gcc gcc-c++ ncurses-devel bison
rm CMakeCache.txt
問題3:— Could NOT find Curses (missing: CURSES_LIBRARY CURSES_INCLUDE_PATH)
CMake Error at cmake/readline.cmake:82 (MESSAGE):
Curses library not found. Please install appropriate package,
remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu, package name is libncurses5-dev, on Redhat and derivates it is ncurses-devel.
Call Stack (most recent call first):
cmake/readline.cmake:126 (FIND_CURSES)
cmake/readline.cmake:216 (MYSQL_USE_BUNDLED_LIBEDIT)
CMakeLists.txt:250 (MYSQL_CHECK_READLINE)
— Configuring incomplete, errors occurred!
解決方法:
[root@localhost mysql-5.5.11]# rm CMakeCache.txt
[root@localhost mysql-5.5.11]# yum install ncurses-devel
[root@localhost mysql-5.5.11]# yum install bison
[root@localhost mysql-5.5.11]# make && make install
修my.cnf 文件
cd /opt/mysql
cp ./support-files/my-huge.cnf /etc/my.cnf #拷貝配置文件(注意:如果/etc目錄下面默認有一個my.cnf,直接覆蓋即可)
vim /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 #加入開機啓動
vim /etc/rc.d/init.d/mysqld #編輯
basedir=/opt/mysql #MySQL程序安裝路徑
datadir=/var/mysql/data #MySQl數據庫存放目錄
service mysqld start #啓動,可能無法寫入pid文件,注意將mysql用戶權限加入至/usr/local/mysql
chown -R mysql:mysql /opt/mysql
添加環境變量
vim /etc/profile #把mysql服務加入系統環境變量:在最後添加下面這一行
export PATH=$PATH:/opt/cmake/bin:/opt/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次密碼
/opt/mysql/bin/mysqladmin -u root -p password "123456" #或者直接修改密碼
到此,mysql安裝完成!
測試mysql(最後全部裝完的)
<?php
header("Control-type:application/json;charset='utf-8'");
#連接數據庫
$conn = mysqli_connect("localhost","root","123456");
#判斷是否連接成功
if(!$conn){
echo "失敗";
}
//選擇數據庫
mysqli_select_db($conn,"oper");
//準備sql語句
$sql = "select * from test";
//發送sql語句
$obj = mysqli_query($conn,$sql);
echo "<center>";
echo "<table border = 1 cellspacing = '0' cellpadding = '10'>";
echo "<th>編號</th><th>姓名</th><th>性別</th><th>狀態</th><th>操作</th>";
while($row = mysqli_fetch_assoc($obj)){
echo "<tr>";
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['name'].'</td>';
echo '<td>'.$row['sex'].'</td>';
echo '<td>'.$row['state'].'</td>';
echo '<td><a href = "del.php?id='.$row['id'].'">刪除</a>/<a href = "update.php?id='.$row['id'].'">修改</a></td>';
echo "</tr>";
}
echo "</table>";
echo "<a href = 'add_user.php'>添加</a>";
echo "<center>";
//關閉連接
mysqli_close($conn);
?>
5.nginx
cd /lnmp/src
tar -zxvf nginx-1.11.5.tar.gz
cd /lnmp/src/
tar -zxvf openssl-1.1.0b.tar.gz
cd nginx-1.11.5
./configure --prefix=/opt/nginx --without-http_memcached_module --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
/opt/nginx/sbin/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: /opt/nginx/conf/nginx.conf
nginxd=/opt/nginx/sbin/nginx
nginx_config=/opt/nginx/conf/nginx.conf
nginx_pid=/opt/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 /opt/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
=======================================================
問題:手動重啓nginx()
ps -ef|grep nginx 查看nginx進程
強制殺死進程
kill -9 pid
6.php
cd /lnmp/src
tar -jxvf php-7.0.7.tar.bz2
cd php-7.0.7
./configure --prefix=/opt/php7 --with-config-file-path=/opt/php7/etc --with-mysqli=/opt/mysql/bin/mysql_config --enable-mysqlnd --with-mysql-sock=/opt/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=/opt/mysql/
make #編譯,,若遇到make: *** [ext/fileinfo/libmagic/apprentice.lo] 錯誤 ,這加參數–-disable-fileinfo
make install #安裝
cd /lnmp/src/php-7.0.7
cp php.ini-production /opt/php7/etc/php.ini #複製php配置文件到安裝目錄
rm -rf /etc/php.ini #刪除系統自帶配置文件
ln -s /opt/php7/etc/php.ini /etc/php.ini #添加軟鏈接
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp /opt/php7/etc/php-fpm.conf.default
/opt/php7/etc/php-fpm.conf #拷貝模板文件爲php-fpm配置文件
#cp /opt/php7/etc/php-fpm.d/www.conf.default /opt/php7/etc/php-fpm.d/www.conf
#vim /opt/php7/etc/php-fpm.d/www.conf #編輯
#user = www #設置php-fpm運行賬號爲www
#group = www #設置php-fpm運行組爲www
vim /opt/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 #設置開機啓動
7.配置nginx支持php
vim /opt/nginx/conf/nginx.conf
#user www www; #首行user去掉註釋,修改Nginx運行組爲www www;必須與/opt/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;
#charset koi8-r;
#access_log logs/host.access.log main;
root html;
index index.php;
##註釋去掉 改$document_root
location ~ [^/]\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#mkdir -p /data/www
#chown www:www /data/www/ -R #設置目錄所有者
#chmod 700 /data/www -R #設置目錄權限
參考 https://www.cnblogs.com/zhyunfe/p/6737124.html
安裝腳本(配置文件未配置)
##############################defind functions that install lnmp....######################
#######nginx##########################
Install_nginx()
{
yum -y install wget
echo -e "\033[33m Install nginx....\033[0m"
if [ -d /opt/nginx ];then
echo -e "\033[33m Nginx alerady exists in your system......\033[0m"
exit
fi
echo -e "\033[33m Install base environment of nginx......\033[0m"
yum -y install pcre* openssl*
echo -e "\033[33mDownload nginx(1.10.1)....\033[0m"
if [ ! -f /opt/src/nginx-1.10.1.tar.gz ];then
wget http://nginx.org/download/nginx-1.10.1.tar.gz -P /opt/src
fi
cd /opt/src/
tar -zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1
echo -e "\033[33m Compose nginx......\033[0m"
./configure --prefix=/opt/nginx --conf-path=/opt/conf/nginx/nginx.conf --error-log-path=/opt/logs/nginx/error.log --http-log-path=/opt/logs/nginx/access.log --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-http_realip_module --http-client-body-temp-path=/opt/nginx/client_body_temp --http-fastcgi-temp-path=/opt/nginx/fastcgi_temp --http-proxy-temp-path=/opt/nginx/proxy_temp --http-uwsgi-temp-path=/opt/nginx/uwsgi_temp --http-scgi-temp-path=/opt/nginx/scgi_temp
make
make install
if [ $? -eq "0" ];then
echo -e "\033[32m Nginx is installed successfully......\033[0m"
else
echo -e "\033[31m Something was wrong during installing nginx,please check and try again......\033[0m"
exit
fi
}
######mysql############################
Install_mysql()
{
yum -y install wget
#Removing default rpm packages of mysql if they have been installed....
if [ -f /usr/bin/mysqld_safe ];then
echo -e "\033[33m Remove default mysql-server RPM packages......\033[0m"
yum -y remove mysql-server
fi
if [ -f /usr/bin/mysqldump ];then
echo -e "\033[33m Remove default mysql-client RPM packages......\033[0m"
yum -y remove mysql
fi
#Adding mysql's user and group..
groupadd mysql #添加mysql組
useradd -g mysql mysql -s /sbin/nologin #創建用戶mysql並加入到mysql組,不允許mysql用戶直接登錄系統
if [ ! -d /opt/data/mysql ];then
mkdir -p /opt/data/mysql
fi
echo -e "\033[33m Setting Environment variables......\033[0m"
#Download mysql and install
if [ ! -d /opt/src/mysql-5.6.32/ ];then
if [ ! -f /opt/src/mysql-5.6.32.tar.gz ];then
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.32.tar.gz -P /opt/src/
fi
cd /opt/src/
tar -zxvf /opt/src/mysql-5.6.32.tar.gz
fi
cd /opt/src/mysql-5.6.32
yum install -y gcc gcc-c++ make automake ncurses-devel
if [ ! -d /opt/src/cmake-3.12.0-rc1/ ];then
if [ ! -f /opt/src/cmake-3.12.0-rc1.tar.gz ];then
wget https://cmake.org/files/v3.12/cmake-3.12.0-rc1.tar.gz -P /opt/src/
fi
cd /opt/src/
tar -zxvf cmake-3.12.0-rc1.tar.gz
cd cmake-3.12.0-rc1
./configure --prefix=/opt/cmake
make
make install
sed -i '$a\export PATH=$PATH:/opt/cmake/bin' /etc/profile
source /etc/profile #使配置立即生效
cd /opt/src/mysql-5.6.32
rm CMakeCache.txt
fi
cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql/ -DMYSQL_DATADIR=/opt/data/mysql/ -DMYSQL_UNIX_ADDR=/opt/data/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_TCP_PORT=3306 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0
if [ "$?" -ne "0" ];then
echo -e 'Configure mysql failure,Please check compile Environment...'
exit 2
fi
make -j 3
if [ "$?" -ne "0" ];then
echo 'Error,please look config.log for more information...'
exit 2
fi
make install
if [ "$?" -eq "0" ];then
echo -e 'Mysql install complete done...'
else
echo -e 'Something wrong between Installation...'
fi
#Initialization mysql..
echo -e 'Initialization mysql...'
cd /opt/src/mysql-5.6.32
cp support-files/my-default.cnf /opt/data/mysql/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
sed -i -e '46s/basedir=/basedir=\/opt\/mysql\//' -e '47s/datadir=/datadir=\/opt\/data\/mysql\//' /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
chkconfig --add mysqld
sed -i -e "s#@HOSTNAME@#hostname#" ./scripts/mysql_install_db.sh
sh ./scripts/mysql_install_db.sh --user=mysql --basedir=/opt/mysql/ --datadir=/opt/data/mysql/ > /dev/null 2>&1
chown -R mysql:mysql /opt/mysql/
chown -R mysql:mysql /opt/data/mysql/
#Start mysql...
ulimit -s unlimited
service mysqld start
}
#######php###########################################################
Install_php()
{
yum -y install wget
if [ ! -f /opt/src/php-5.6.36.tar.gz ];then
echo -e "\033[33m Download php-5.6.10.tar.bz......\033[0m"
wget 192.168.0.85/lnmp/php-5.6.36.tar.gz -P /opt/src
cd /opt/src
else
cd /opt/src
fi
echo -e "\033[33m Complising and installing php-5.6.36 ......\033[0m"
tar -zxvf php-5.6.36.tar.gz
cd php-5.6.36
yum -y install epel-release #擴展包更新包
yum update #更新yum源
yum -y install freetype-devel libpng libpng-devel php-mcrypt libmcrypt-devel libjpeg-turbo-devel.x86_64 libxml2 libxml2-devel curl curl-devel
#./configure --prefix=/opt/php --with-config-file-path=/opt/php/etc --with-mcrypt=/usr/include --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-xml --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-curl --with-jpeg-dir --with-freetype-dir --enable-opcache
./configure --prefix=/opt/php --with-config-file-path=/opt/php/etc --with-mysqli=/opt/mysql/bin/mysql_config --enable-mysqlnd --with-mysql-sock=/opt/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=/opt/mysql/
if [ "$?" -ne "0" ];then
echo -e "\033[31m Php install faild....Please check and try again....\033[0m"
exit
fi
make && make install
cp php.ini-production /opt/php/etc/php.ini
rm -rf /etc/php.ini
ln -s /opt/php/etc/php.ini /etc/php.ini
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp /opt/php/etc/php-fpm.conf.default /opt/php/etc/php-fpm.conf
cp /opt/php/etc/php-fpm.d/www.conf.default /opt/php/etc/php-fpm.d/www.conf
echo "zend_extension=/opt/php/lib/php/extensions/no-debug-non-zts-20151012/opcache.so" >> /opt/php/etc/php.ini
chmod 775 /etc/init.d/php-fpm
echo -e "\033[33m Starting php-frm service......\033[0m"
/etc/init.d/php-fpm start
if [ $? -eq "0" ];then
echo -e "\032[33m PHP installed and started successfully......\033[0m"
else
echo -e "\033[31m PHP installed successfully,but start failed,please check the configuration of php......\033[0m"
fi
}
###############################Main program######################################
mkdir -p /opt/{conf,src,data}
read -p "Which part of lnmp you want to install?(nginx|mysql|php|all):" softwar
case ${softwar} in
nginx)
Install_nginx
;;
mysql)
Install_mysql
;;
php)
Install_php
;;
all)
if [ -d /opt/nginx ];then
echo -e "\033[33m Nginx was already installed in your system...\033[0m"
else
Install_nginx
fi
if [ -d /opt/mysql ];then
echo -e "\033[33m Mysql was already installed in your system...\033[0m"
else
Install_mysql
fi
if [ -d /opt/php ];then
echo -e "\033[33m PHP was already installed in your system...\033[0m"
else
Install_php
sed -i '$a\export PATH=$PATH:/opt/cmake/bin:/opt/php/bin:/opt/mysql/bin' /etc/profile
source /etc/profile #使配置立即生效
fi
;;
*)
echo -e "\033[33m Sorry,please input a right choice\033[0m"
esac