C C++ and Java Program in Linux and Lamp Server Installation

C C++ and Java Program in Linux and Lamp Server Installation

7.How to install GCC (C/C++) Compiler in Linux Create a sample C program then compile and execute the code.
[email protected]:~$ sudo apt install gcc
[email protected]:~$ sudo apt install build-essential
[email protected]:~$ gcc –version
C Program
Example-1:
Write the following code using any text editor and save the file with the extension ‘.c’.
#include <stdio.h>
int main()
{
printf(“Learning C in Linux Kernal”);
}
To compile and execute the code. The source file name is first.c and executable filename is first here.
$ gcc first.c -o first
$ ./first
Learning C in Linux Kernal
Example-2:
#include <stdio.h>
int main()
{
char name[100];
int age;
printf(“Enter your Name: “);
scanf(“%s”,name);
printf(“Enter your Age: “);
scanf(“%d”,&age);
printf(“Hello, %s,You are %d years old”, name, age);
}
Example-3:
#include <stdio.h>
int main(int argc,char* argv[]){
printf(“Total number of Arguments = %d\n”,argc);
printf(“Argument No. 1 = %s\n”,argv[0]);
printf(“Argument No. 2 = %s\n”,argv[1]);
printf(“Argument No. 3 = %s\n”,argv[2]);
}
8. Create a sample C++ program then compile and execute the code.
Example-1:
Write the following code using any text editor and save the file with the extension ‘.cpp’
[email protected]:~$ sudo nano hello.cpp

#include <iostream>
using namespace std;
int main()
{
cout<<“Hello World!” <<endl;
return 0;
}

[email protected]:~$ g++ hello.cpp
[email protected]:~$ ./a.out
Hello World!
[email protected]:~$ g++ hello.cpp -o hello
[email protected]:~$./hello
Hello World!
Example-2:
#include <bits/stdc++.h>
using namespace std;
class Gyancs
{
// Access specifier
public:
// Data Members
string csname;
// Member Functions()
void printname()
{
cout<< “Csname is: “<<csname;
}
};
int main() {
// Declare an object of class Gyancs
Gyancs obj1;
// accessing data member
obj1.csname = “Open Source”;
// accessing member function
obj1.printname();
return0;
}
Output:
Csname is: Open Source

9. How to install Java in Linux Creating a sample java Program.

[email protected]:~$ sudo apt install openjdk-8-jdk
[email protected]:~$ nano /etc/environment

JAVA_HOME=”/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin”

Java Programming in Linux
Example-1:
Write the following code using any text editor and save the file with the extension ‘.java’
[email protected]:~$ sudo nano Simple.java
class Simple{
public static void main(String args[]){
System.out.println(“Hello Java”);
}
}
[email protected]:~$ javac Simple.java
[email protected]:~$ java Simple
Hello Java
Example-2:
[email protected]:~$ sudo nano Student.java
public class Student {
public static void main(String[] args) {
int x=10;
int y=12;
if(x+y> 20)
{
System.out.println(“x+y is Greater Than 20”);
}
}
}

[email protected]:~$ javac Student.java
[email protected]:~$ java Student
x+y is Greater Than 20
10.How To Install Linux, Apache, MySQL, PHP (LAMP)
Step 1: Install Apache
[email protected]:~$ sudo apt-get update
[email protected]:~$ sudo apt-get install apache2
Step 2: Install MySQL
[email protected]:~$ sudo apt-get install mysql-server
Finish up by running the MySQL set up script:
[email protected]:~$ sudo /usr/bin/mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: yes

There are three levels of password validation policy:

LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
The prompt will ask you for your current root password.
Type it in.
Enter current password for root (enter for none):

OK, successfully used password, moving on…
Step 3: Install PHP
[email protected]:~$ sudo apt-get install php
may also be useful to add php to the directory index, to serve the relevant php index files:
[email protected]:~$ sudo nano /etc/apache2/mods-enabled/dir.conf
Add index.php to the beginning of index files. The page should now look like this:
<IfModulemod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>

Note: Green place is add index.php, Strikethrough is remove it there.

PHP Modules
[email protected]:~$ apt-cache search php
Step 4: RESULTS — See PHP on your Server
To set this up, first create a new file:
[email protected]:~$ sudo nano /var/www/html/info.php
Add in the following line:
<?php
phpinfo();
?>
Then Save and Exit.
Restart apache so that all of the changes take effect:
[email protected]:~$ sudo service apache2 restart
How to Find your Server’s IP address
[email protected]:~$ sudo ifconfig
The address you want to visit will be:
http://your_server_IP_address/info.php

http://192.168.7.81/
http://192.168.7.81/info.php

Start the Command-line tool MySql and select a database:

[email protected]:~$sudo mysql -u root -p
[sudo] password for user:
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 46
Server version: 8.0.26-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>

or

[email protected]:~$sudo mysql -u root
[sudo] password for user:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 47
Server version: 8.0.26-0ubuntu0.20.04.3 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>SHOW DATABASES;
mysql>use mysql;
mysql>show tables;

To create and populate the example table, use these statements:
mysql>CREATE TABLE item (
article INT UNSIGNED DEFAULT’0000NOT NULL,
dealer CHAR(20)DEFAULT”NOT NULL,
price DECIMAL(16,2)DEFAULT’0.00NOT NULL,
PRIMARYKEY(article, dealer));
mysql>DESCRIBE item;
mysql>INSERT INTO item VALUES
(1,’A’,3.45),(1,’B’,3.99),(2,’A’,10.99),(3,’B’,1.45),
(3,’C’,1.69),(3,’D’,1.25),(4,’D’,19.95);

After issuing the statements, the table should have the following contents:

mysql>SELECT*FROM item ORDERBY article;

+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
|       1 | A      |  3.45 |
|       1 | B      |  3.99 |
|       2 | A      | 10.99 |
|       3 | B      |  1.45 |
|       3 | C      |  1.69 |
|       3 | D      |  1.25 |
|       4 | D      | 19.95 |
+---------+--------+-------+

Usage of Basic Linux Commands, File Utilities Commands

Theory Question:

  1. What is the difference between Linux and Unix?
  2. What are the Features of the Linux Operating System?
  3. What is Linux Kernel?
  4. How many types of Shells are there in Linux? explain.
  5. Explain File Permissions types in Linux?
  6. What are the symbolic links?
  7. Explain about chmod command?
  8. Where is password file located in Linux and how can you improve the security of password file?
  9. How do you create a new user account and set the password for a user from a shell prompt in Linux?
  10. What are the different types of Kernels? Explain
  11. What is the difference between “rm” and “rm –r”?


Open Source Software Lab Manual

About me